@marcel91200 / caddy Mirror · read-only / commits / f8a2c602
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 10:45 UTC

caddyhttp: properly sanitize requests for root path (#6360)

SanitizePathJoin protects against directory traversal attacks by checking for requests whose URL path look like they are trying to request something other than a local file, and returns the root directory in those cases. The method is also careful to ensure that requests which contain a trailing slash include a trailing slash in the returned value. However, for requests that contain only a slash (requests for the root path), the IsLocal check returns early before the matching trailing slash is re-added. This change updates SanitizePathJoin to only perform the filepath.IsLocal check if the cleaned request URL path is non-empty. --- This change also updates the existing SanitizePathJoin tests to use filepath.FromSlash rather than filepath.Join. This makes the expected value a little easier to read, but also has the advantage of not being processed by filepath.Clean like filepath.Join is. This means that the exact expect value will be compared, not the result of first cleaning it. Fixes #6352

Will Norris committed Jun 1, 2024 at 20:40 UTC f8a2c602971c2a85747b8eda7e01a40b585b3149
2 files changed +19 -11
modules/caddyhttp/caddyhttp.go
+1 -1
@@ -239,7 +239,7 @@ func SanitizedPathJoin(root, reqPath string) string {
239 }
240
241 relPath := path.Clean("/" + reqPath)[1:] // clean path and trim the leading /
242 - if !filepath.IsLocal(relPath) {
242 + if relPath != "" && !filepath.IsLocal(relPath) {
243 // path is unsafe (see https://github.com/golang/go/issues/56336#issuecomment-1416214885)
244 return root
245 }
modules/caddyhttp/caddyhttp_test.go
+18 -10
@@ -26,22 +26,28 @@ func TestSanitizedPathJoin(t *testing.T) {
26 inputPath: "/",
27 expect: ".",
28 },
29 + {
30 + // fileserver.MatchFile passes an inputPath of "//" for some try_files values.
31 + // See https://github.com/caddyserver/caddy/issues/6352
32 + inputPath: "//",
33 + expect: filepath.FromSlash("./"),
34 + },
35 {
36 inputPath: "/foo",
37 expect: "foo",
38 },
39 {
40 inputPath: "/foo/",
35 - expect: "foo" + separator,
41 + expect: filepath.FromSlash("foo/"),
42 },
43 {
44 inputPath: "/foo/bar",
39 - expect: filepath.Join("foo", "bar"),
45 + expect: filepath.FromSlash("foo/bar"),
46 },
47 {
48 inputRoot: "/a",
49 inputPath: "/foo/bar",
44 - expect: filepath.Join("/", "a", "foo", "bar"),
50 + expect: filepath.FromSlash("/a/foo/bar"),
51 },
52 {
53 inputPath: "/foo/../bar",
@@ -50,32 +56,34 @@ func TestSanitizedPathJoin(t *testing.T) {
56 {
57 inputRoot: "/a/b",
58 inputPath: "/foo/../bar",
53 - expect: filepath.Join("/", "a", "b", "bar"),
59 + expect: filepath.FromSlash("/a/b/bar"),
60 },
61 {
62 inputRoot: "/a/b",
63 inputPath: "/..%2fbar",
58 - expect: filepath.Join("/", "a", "b", "bar"),
64 + expect: filepath.FromSlash("/a/b/bar"),
65 },
66 {
67 inputRoot: "/a/b",
68 inputPath: "/%2e%2e%2fbar",
63 - expect: filepath.Join("/", "a", "b", "bar"),
69 + expect: filepath.FromSlash("/a/b/bar"),
70 },
71 {
72 + // inputPath fails the IsLocal test so only the root is returned,
73 + // but with a trailing slash since one was included in inputPath
74 inputRoot: "/a/b",
75 inputPath: "/%2e%2e%2f%2e%2e%2f",
68 - expect: "/a/b", // inputPath fails the IsLocal test so only the root is returned
76 + expect: filepath.FromSlash("/a/b/"),
77 },
78 {
79 inputRoot: "/a/b",
80 inputPath: "/foo%2fbar",
73 - expect: filepath.Join("/", "a", "b", "foo", "bar"),
81 + expect: filepath.FromSlash("/a/b/foo/bar"),
82 },
83 {
84 inputRoot: "/a/b",
85 inputPath: "/foo%252fbar",
78 - expect: filepath.Join("/", "a", "b", "foo%2fbar"),
86 + expect: filepath.FromSlash("/a/b/foo%2fbar"),
87 },
88 {
89 inputRoot: "C:\\www",
@@ -92,7 +100,7 @@ func TestSanitizedPathJoin(t *testing.T) {
100 // https://github.com/golang/go/issues/56336#issuecomment-1416214885
101 inputRoot: "root",
102 inputPath: "/a/b/../../c",
95 - expect: filepath.Join("root", "c"),
103 + expect: filepath.FromSlash("root/c"),
104 },
105 } {
106 // we don't *need* to use an actual parsed URL, but it