Repository navigation
Expand file tree
/
Copy pathbase_path.go
More file actions
103 lines (94 loc) · 3.89 KB
/
Copy pathbase_path.go
File metadata and controls
103 lines (94 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2026 Super Durable, Inc.
//
// Licensed under the Sustainable Use License 1.0.
// You may not use this file except in compliance with the License.
// See the LICENSE file in the repository root.
//
// SPDX-License-Identifier: LicenseRef-Sustainable-Use-1.0
package web
import (
"fmt"
"net/http"
"path"
"strings"
)
// maxBasePathLength bounds a base path so it stays a small part of every URL Dex Web builds.
const maxBasePathLength = 1024
// ValidateBasePath checks a Dex Web mount prefix and returns its canonical form.
//
// An empty value and "/" both mean the root. A trailing slash is removed, so "/dex/" becomes "/dex". Every
// segment must be non-empty, must not be "." or "..", and may contain only ASCII letters, digits, '.', '_',
// '~', and '-'. Percent-encoding is therefore rejected, which keeps encoded separators out of the prefix.
//
// It returns the canonical base path, or an error that names the rule the value breaks.
func ValidateBasePath(value string) (string, error) {
if value == "" || value == "/" {
return "/", nil
}
if len(value) > maxBasePathLength {
return "", fmt.Errorf("base path must be at most %d bytes", maxBasePathLength)
}
if !strings.HasPrefix(value, "/") {
return "", fmt.Errorf("base path %q must start with /", value)
}
canonical := strings.TrimSuffix(value, "/")
for _, segment := range strings.Split(canonical[1:], "/") {
if segment == "" || segment == "." || segment == ".." {
return "", fmt.Errorf("base path %q must not contain empty, '.', or '..' segments", value)
}
for _, character := range segment {
if !isBasePathCharacter(character) {
return "", fmt.Errorf("base path %q may contain only letters, digits, '.', '_', '~', '-', and '/'", value)
}
}
}
return canonical, nil
}
func isBasePathCharacter(character rune) bool {
return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') ||
(character >= '0' && character <= '9') || character == '.' || character == '_' || character == '~' || character == '-'
}
// joinBasePath prefixes a rooted Dex Web path, such as "/v1/connectors", with a canonical base path.
func joinBasePath(basePath string, rootedPath string) string {
if basePath == "/" {
return rootedPath
}
return basePath + rootedPath
}
// serveUnderBasePath strips a canonical base path from the request and serves the rest with next.
// A request outside the base path, or with dot or empty segments under it, gets HTTP 404.
func serveUnderBasePath(response http.ResponseWriter, request *http.Request, basePath string, next http.Handler) {
if basePath == "/" {
next.ServeHTTP(response, request)
return
}
requestPath := request.URL.Path
if requestPath == basePath && request.URL.EscapedPath() == basePath {
redirectToBasePathRoot(response, request, basePath)
return
}
innerPath, isUnderBasePath := strings.CutPrefix(requestPath, basePath+"/")
_, isEscapedUnderBasePath := strings.CutPrefix(request.URL.EscapedPath(), basePath+"/")
if !isUnderBasePath || !isEscapedUnderBasePath || !isCleanRequestPath("/"+innerPath) {
http.NotFound(response, request)
return
}
http.StripPrefix(basePath, next).ServeHTTP(response, request)
}
// redirectToBasePathRoot sends a page request for "/dex" to "/dex/" so relative browser state stays under it.
func redirectToBasePathRoot(response http.ResponseWriter, request *http.Request, basePath string) {
if request.Method != http.MethodGet && request.Method != http.MethodHead {
http.NotFound(response, request)
return
}
location := basePath + "/"
if request.URL.RawQuery != "" {
location += "?" + request.URL.RawQuery
}
http.Redirect(response, request, location, http.StatusMovedPermanently)
}
// isCleanRequestPath rejects dot and empty segments, which ServeMux would redirect outside the base path.
func isCleanRequestPath(requestPath string) bool {
cleaned := path.Clean(requestPath)
return cleaned == requestPath || cleaned+"/" == requestPath
}