|
| 1 | +package workspaceapps |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// Test_originLocalURL checks that originLocalURL produces a redirect target that |
| 12 | +// stays on the current origin. |
| 13 | +func Test_originLocalURL(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + t.Run("RejectsOffOrigin", func(t *testing.T) { |
| 17 | + t.Parallel() |
| 18 | + |
| 19 | + // Each path models an already-percent-decoded r.URL.Path that tries to |
| 20 | + // smuggle a separate host into the redirect. |
| 21 | + cases := []struct { |
| 22 | + name string |
| 23 | + path string |
| 24 | + }{ |
| 25 | + {name: "DoubleSlash", path: "//evil.com/phish"}, |
| 26 | + {name: "TripleSlash", path: "///evil.com/phish"}, |
| 27 | + {name: "SlashBackslash", path: "/\\evil.com/phish"}, |
| 28 | + {name: "SlashBackslashSlash", path: "/\\/evil.com/phish"}, |
| 29 | + {name: "DoubleBackslash", path: "\\\\evil.com/phish"}, |
| 30 | + {name: "SlashTab", path: "/\t/evil.com/phish"}, |
| 31 | + {name: "SlashTabBackslash", path: "/\t\\evil.com/phish"}, |
| 32 | + {name: "SlashNewline", path: "/\n/evil.com/phish"}, |
| 33 | + {name: "SlashCarriageReturn", path: "/\r/evil.com/phish"}, |
| 34 | + {name: "SlashTabDoubleSlash", path: "/\t//evil.com/phish"}, |
| 35 | + } |
| 36 | + |
| 37 | + for _, tc := range cases { |
| 38 | + t.Run(tc.name, func(t *testing.T) { |
| 39 | + t.Parallel() |
| 40 | + |
| 41 | + loc := originLocalURL(tc.path).String() |
| 42 | + |
| 43 | + // The Location must parse as a relative, same-origin reference. |
| 44 | + require.Falsef(t, strings.HasPrefix(loc, "//"), |
| 45 | + "path %q produced scheme-relative Location %q", tc.path, loc) |
| 46 | + parsed, err := url.Parse(loc) |
| 47 | + require.NoErrorf(t, err, "path %q produced unparseable Location %q", tc.path, loc) |
| 48 | + require.Emptyf(t, parsed.Scheme, "path %q produced Location %q with a scheme", tc.path, loc) |
| 49 | + require.Emptyf(t, parsed.Host, "path %q produced Location %q with a host", tc.path, loc) |
| 50 | + |
| 51 | + // It must also be free of raw bytes a browser would normalize back |
| 52 | + // into an authority before resolving (a backslash becomes "/", and |
| 53 | + // tab/newline/CR are stripped, either of which could re-form |
| 54 | + // "//host"). url.URL.String() guarantees this by percent-encoding |
| 55 | + // them; we assert it here rather than reproducing browser |
| 56 | + // normalization in the code. |
| 57 | + for _, raw := range []string{`\`, "\t", "\n", "\r"} { |
| 58 | + require.NotContainsf(t, loc, raw, |
| 59 | + "path %q produced Location %q containing a raw %q", tc.path, loc, raw) |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + t.Run("EscapesControlCharacters", func(t *testing.T) { |
| 66 | + t.Parallel() |
| 67 | + |
| 68 | + // A redirect built from a path containing a raw control character is an |
| 69 | + // open redirect: http.Redirect emits it verbatim (url.Parse rejects the |
| 70 | + // control byte and skips cleaning) and browsers strip tab/newline/CR |
| 71 | + // before resolving, re-forming "//evil.com". originLocalURL percent-encodes |
| 72 | + // each one. Assert every class is escaped so a future change that breaks |
| 73 | + // encoding for only one class is caught. |
| 74 | + cases := []struct { |
| 75 | + name string |
| 76 | + in string |
| 77 | + want string |
| 78 | + }{ |
| 79 | + {name: "Tab", in: "/\t/evil.com", want: "/%09/evil.com"}, |
| 80 | + {name: "Newline", in: "/\n/evil.com", want: "/%0A/evil.com"}, |
| 81 | + {name: "CarriageReturn", in: "/\r/evil.com", want: "/%0D/evil.com"}, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tc := range cases { |
| 85 | + t.Run(tc.name, func(t *testing.T) { |
| 86 | + t.Parallel() |
| 87 | + |
| 88 | + require.Equalf(t, tc.want, originLocalURL(tc.in).String(), |
| 89 | + "originLocalURL(%q) must percent-encode the control character", tc.in) |
| 90 | + }) |
| 91 | + } |
| 92 | + }) |
| 93 | + |
| 94 | + t.Run("PreservesLegitPaths", func(t *testing.T) { |
| 95 | + t.Parallel() |
| 96 | + |
| 97 | + cases := []struct { |
| 98 | + name string |
| 99 | + in string |
| 100 | + want string |
| 101 | + }{ |
| 102 | + {name: "Empty", in: "", want: "/"}, |
| 103 | + {name: "Root", in: "/", want: "/"}, |
| 104 | + {name: "Simple", in: "/test", want: "/test"}, |
| 105 | + {name: "Nested", in: "/app/sub/page", want: "/app/sub/page"}, |
| 106 | + {name: "PathApp", in: "/@user/ws/apps/app", want: "/@user/ws/apps/app"}, |
| 107 | + } |
| 108 | + |
| 109 | + for _, tc := range cases { |
| 110 | + t.Run(tc.name, func(t *testing.T) { |
| 111 | + t.Parallel() |
| 112 | + |
| 113 | + require.Equalf(t, tc.want, originLocalURL(tc.in).String(), "originLocalURL(%q)", tc.in) |
| 114 | + }) |
| 115 | + } |
| 116 | + }) |
| 117 | +} |
0 commit comments