|
| 1 | +//go:build !integration |
| 2 | + |
| 3 | +package cachekey |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestSanitize(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + rawKey string |
| 16 | + expectedKey string |
| 17 | + wantErr bool |
| 18 | + }{ |
| 19 | + // ── Empty / identity ──────────────────────────────────────── |
| 20 | + {rawKey: ""}, |
| 21 | + {rawKey: "fallback_key", expectedKey: "fallback_key"}, |
| 22 | + {rawKey: "some-job/some-ref", expectedKey: "some-job/some-ref"}, |
| 23 | + {rawKey: ".../....", expectedKey: ".../...."}, |
| 24 | + {rawKey: "...", expectedKey: "..."}, |
| 25 | + |
| 26 | + // ── Trailing whitespace / slashes / backslashes ───────────── |
| 27 | + {rawKey: "fallback_key/", expectedKey: "fallback_key"}, |
| 28 | + {rawKey: "fallback_key ", expectedKey: "fallback_key"}, |
| 29 | + {rawKey: "fallback_key\\", expectedKey: "fallback_key"}, |
| 30 | + {rawKey: "fallback_key/ \\", expectedKey: "fallback_key"}, |
| 31 | + {rawKey: "fallback_key/ / \\ \\", expectedKey: "fallback_key"}, |
| 32 | + {rawKey: "fallback_key/o", expectedKey: "fallback_key/o"}, |
| 33 | + {rawKey: "fallback_key / \\o", expectedKey: "fallback_key / /o"}, |
| 34 | + {rawKey: "\t foo bar \t\r", expectedKey: "\t foo bar"}, |
| 35 | + {rawKey: " foo / bar ", expectedKey: " foo / bar"}, |
| 36 | + {rawKey: "foo\r", expectedKey: "foo"}, |
| 37 | + {rawKey: "foo\t", expectedKey: "foo"}, |
| 38 | + {rawKey: "foo \t \r ", expectedKey: "foo"}, |
| 39 | + |
| 40 | + // ── Completely unsanitisable ──────────────────────────────── |
| 41 | + {rawKey: "\\", wantErr: true}, |
| 42 | + {rawKey: "\\.", wantErr: true}, |
| 43 | + {rawKey: "/", wantErr: true}, |
| 44 | + {rawKey: " ", wantErr: true}, |
| 45 | + {rawKey: ".", wantErr: true}, |
| 46 | + {rawKey: "..", wantErr: true}, |
| 47 | + {rawKey: " / ", wantErr: true}, |
| 48 | + {rawKey: "//", wantErr: true}, |
| 49 | + {rawKey: `//\`, wantErr: true}, |
| 50 | + {rawKey: "../.", wantErr: true}, |
| 51 | + {rawKey: "foo\\bar\\..\\..", wantErr: true}, |
| 52 | + {rawKey: "foo/bar/../..", wantErr: true}, |
| 53 | + {rawKey: " \t\r\n", wantErr: true}, |
| 54 | + |
| 55 | + // ── URL-encoded slashes (%2f / %2F) ──────────────────────── |
| 56 | + {rawKey: "something %2F something", expectedKey: "something / something"}, |
| 57 | + {rawKey: "something %2f something", expectedKey: "something / something"}, |
| 58 | + {rawKey: "some%2f../job/some/ref/.", expectedKey: "job/some/ref"}, |
| 59 | + |
| 60 | + // ── URL-encoded dots (%2e / %2E) ─────────────────────────── |
| 61 | + {rawKey: "%2E", wantErr: true}, |
| 62 | + {rawKey: "%2E%2E", wantErr: true}, |
| 63 | + {rawKey: "%2E%2E%2E", expectedKey: "..."}, |
| 64 | + {rawKey: "%2e", wantErr: true}, |
| 65 | + {rawKey: "%2e%2E", wantErr: true}, |
| 66 | + {rawKey: ".%2E", wantErr: true}, |
| 67 | + {rawKey: "%2e.", wantErr: true}, |
| 68 | + {rawKey: "%2E%2e%2E", expectedKey: "..."}, |
| 69 | + |
| 70 | + // %5C is left as-is (literal percent-encoded backslash is fine). |
| 71 | + {rawKey: "%5C", expectedKey: "%5C"}, |
| 72 | + {rawKey: "%5c", expectedKey: "%5c"}, |
| 73 | + |
| 74 | + // ── Forward-slash path traversal ──────────────────────────── |
| 75 | + {rawKey: "foo/./bar", expectedKey: "foo/bar"}, |
| 76 | + {rawKey: "foo/blipp/../bar", expectedKey: "foo/bar"}, |
| 77 | + {rawKey: "/foo/bar", expectedKey: "foo/bar"}, |
| 78 | + {rawKey: "//foo/bar", expectedKey: "foo/bar"}, |
| 79 | + {rawKey: "./foo/bar", expectedKey: "foo/bar"}, |
| 80 | + {rawKey: "../foo/bar", expectedKey: "foo/bar"}, |
| 81 | + {rawKey: ".../foo/bar", expectedKey: ".../foo/bar"}, |
| 82 | + {rawKey: "foo/bar/..", expectedKey: "foo"}, |
| 83 | + {rawKey: "foo/bar/../../../.././blerp", expectedKey: "blerp"}, |
| 84 | + {rawKey: "a/b/c/../../d", expectedKey: "a/d"}, |
| 85 | + |
| 86 | + // ── Backslash path traversal ──────────────────────────────── |
| 87 | + {rawKey: `job\name/git\ref`, expectedKey: "job/name/git/ref"}, |
| 88 | + {rawKey: "foo\\.\\bar", expectedKey: "foo/bar"}, |
| 89 | + {rawKey: "foo\\blipp\\..\\bar", expectedKey: "foo/bar"}, |
| 90 | + {rawKey: "\\foo\\bar", expectedKey: "foo/bar"}, |
| 91 | + {rawKey: "\\\\foo\\bar", expectedKey: "foo/bar"}, |
| 92 | + {rawKey: ".\\foo\\bar", expectedKey: "foo/bar"}, |
| 93 | + {rawKey: "..\\foo\\bar", expectedKey: "foo/bar"}, |
| 94 | + {rawKey: "...\\foo\\bar", expectedKey: ".../foo/bar"}, |
| 95 | + {rawKey: "foo\\bar\\..", expectedKey: "foo"}, |
| 96 | + {rawKey: "foo\\bar\\..\\..\\..\\..\\.\\blerp", expectedKey: "blerp"}, |
| 97 | + |
| 98 | + // ── Space-only segments & misc ────────────────────────────── |
| 99 | + {rawKey: "foo/ /bar", expectedKey: "foo/ /bar"}, |
| 100 | + {rawKey: "foo/ /", expectedKey: "foo"}, |
| 101 | + {rawKey: "foo/ / /", expectedKey: "foo"}, |
| 102 | + } |
| 103 | + |
| 104 | + for i, tt := range tests { |
| 105 | + name := fmt.Sprintf("%d:%q", i, tt.rawKey) |
| 106 | + t.Run(name, func(t *testing.T) { |
| 107 | + actual, err := Sanitize(tt.rawKey) |
| 108 | + if tt.wantErr { |
| 109 | + assert.Error(t, err) |
| 110 | + } else { |
| 111 | + assert.NoError(t, err) |
| 112 | + } |
| 113 | + assert.Equal(t, tt.expectedKey, actual) |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// TestSanitizeInvariants checks properties that must hold for every sanitised |
| 119 | +// key, regardless of input. |
| 120 | +func TestSanitizeInvariants(t *testing.T) { |
| 121 | + cases := []string{ |
| 122 | + "a", "a/b", "../a", "a/../b", "a/./b", |
| 123 | + "a\\b", `a\..\\b`, "/a/b/", " a ", "...", |
| 124 | + "%2e%2e/%2f", "a/b/c/../../d/e", |
| 125 | + } |
| 126 | + for _, raw := range cases { |
| 127 | + t.Run(raw, func(t *testing.T) { |
| 128 | + key, _ := Sanitize(raw) |
| 129 | + if key == "" { |
| 130 | + return // unsanitisable, nothing to check |
| 131 | + } |
| 132 | + assert.False(t, strings.HasPrefix(key, "/"), "must not start with /") |
| 133 | + assert.False(t, key == ".." || strings.HasPrefix(key, "../"), "must not start with .. segment") |
| 134 | + assert.False(t, strings.Contains(key, `\`), "must not contain backslash") |
| 135 | + assert.False(t, strings.HasSuffix(key, " "), "must not end with space") |
| 136 | + assert.False(t, strings.HasSuffix(key, "/"), "must not end with /") |
| 137 | + |
| 138 | + // No segment should be "." or ".." |
| 139 | + for _, seg := range strings.Split(key, "/") { |
| 140 | + assert.NotEqual(t, ".", seg, "must not contain '.' segment") |
| 141 | + assert.NotEqual(t, "..", seg, "must not contain '..' segment") |
| 142 | + } |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +// TestSanitizeIdempotent verifies that sanitising an already-clean key |
| 148 | +// returns it unchanged with no error. |
| 149 | +func TestSanitizeIdempotent(t *testing.T) { |
| 150 | + inputs := []string{ |
| 151 | + "fallback_key", |
| 152 | + "some-job/some-ref", |
| 153 | + "a/b/c", |
| 154 | + "...", |
| 155 | + ".../foo/bar", |
| 156 | + } |
| 157 | + for _, raw := range inputs { |
| 158 | + t.Run(raw, func(t *testing.T) { |
| 159 | + first, err1 := Sanitize(raw) |
| 160 | + assert.NoError(t, err1) |
| 161 | + |
| 162 | + second, err2 := Sanitize(first) |
| 163 | + assert.NoError(t, err2) |
| 164 | + assert.Equal(t, first, second, "sanitise should be idempotent") |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
0 commit comments