Skip to content

Commit 01f4f07

Browse files
committed
feat(param): remove duplicated allowed hosts in restrict access
1 parent df0400e commit 01f4f07

File tree

3 files changed

+80
-9
lines changed

3 files changed

+80
-9
lines changed

src/param/main.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,25 @@ func (param *Param) normalize() (errs []error) {
131131
}
132132

133133
// restrict access
134-
param.RestrictAccessUrls, es = normalizePathValues(param.RestrictAccessUrls, true, util.NormalizeUrlPath, util.ExtractHostsFromUrls)
135-
errs = append(errs, es...)
134+
param.RestrictAccessUrls, es = normalizeAllPathValues(param.RestrictAccessUrls, true, util.NormalizeUrlPath, util.ExtractHostsFromUrls)
135+
if len(es) == 0 {
136+
dedupAllPathValues(param.RestrictAccessUrls)
137+
} else {
138+
errs = append(errs, es...)
139+
}
136140

137-
param.RestrictAccessDirs, es = normalizePathValues(param.RestrictAccessDirs, true, util.NormalizeFsPath, util.ExtractHostsFromUrls)
138-
errs = append(errs, es...)
141+
param.RestrictAccessDirs, es = normalizeAllPathValues(param.RestrictAccessDirs, true, util.NormalizeFsPath, util.ExtractHostsFromUrls)
142+
if len(es) == 0 {
143+
dedupAllPathValues(param.RestrictAccessDirs)
144+
} else {
145+
errs = append(errs, es...)
146+
}
139147

140148
// headers
141-
param.HeadersUrls, es = normalizePathValues(param.HeadersUrls, false, util.NormalizeUrlPath, normalizeHeaders)
149+
param.HeadersUrls, es = normalizeAllPathValues(param.HeadersUrls, false, util.NormalizeUrlPath, normalizeHeaders)
142150
errs = append(errs, es...)
143151

144-
param.HeadersDirs, es = normalizePathValues(param.HeadersDirs, false, util.NormalizeFsPath, normalizeHeaders)
152+
param.HeadersDirs, es = normalizeAllPathValues(param.HeadersDirs, false, util.NormalizeFsPath, normalizeHeaders)
145153
errs = append(errs, es...)
146154

147155
// upload/mkdir/delete/archive/cors/auth urls/dirs

src/param/strUtil.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func splitAllKeyValue(inputs []string) (results [][2]string) {
7878
return
7979
}
8080

81-
func normalizePathValues(
81+
func normalizeAllPathValues(
8282
inputs [][]string,
8383
keepEmptyValuesEntry bool,
8484
normalizePath func(string) (string, error),
@@ -128,6 +128,35 @@ eachInput:
128128
return
129129
}
130130

131+
func dedupPathValues(inputs []string) []string {
132+
if len(inputs) <= 2 { // path & single value
133+
return inputs
134+
}
135+
136+
values := inputs[1:]
137+
endIndex := 1
138+
eachValue:
139+
for i, length := 1, len(values); i < length; i++ {
140+
for j := 0; j < endIndex; j++ {
141+
if values[i] == values[j] {
142+
continue eachValue
143+
}
144+
}
145+
if endIndex != i {
146+
values[endIndex] = values[i]
147+
}
148+
endIndex++
149+
}
150+
151+
return inputs[:1+endIndex]
152+
}
153+
154+
func dedupAllPathValues(inputs [][]string) {
155+
for i, iLen := 0, len(inputs); i < iLen; i++ {
156+
inputs[i] = dedupPathValues(inputs[i])
157+
}
158+
}
159+
131160
func normalizePathMaps(inputs [][2]string) (results [][2]string, errs []error) {
132161
results = make([][2]string, 0, len(inputs))
133162

src/param/strUtil_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func TestSplitAllKeyValue(t *testing.T) {
130130
}
131131

132132
func TestNormalizePathRestrictAccesses(t *testing.T) {
133-
results, _ := normalizePathValues([][]string{
133+
results, _ := normalizeAllPathValues([][]string{
134134
{"/foo", "host1", "host2"},
135135
{"/foo/", "host3", "host4"},
136136
{"/bar"},
@@ -150,7 +150,7 @@ func TestNormalizePathRestrictAccesses(t *testing.T) {
150150
func TestNormalizePathHeadersMap(t *testing.T) {
151151
var result [][]string
152152

153-
result, _ = normalizePathValues([][]string{
153+
result, _ = normalizeAllPathValues([][]string{
154154
{"/foo", "X-header1", "X-Value1"},
155155
{"/foo/", "X-header2", "X-Value2"},
156156
{"/bar", "X-header3", "X-Value3"},
@@ -217,6 +217,40 @@ func TestNormalizePathMaps(t *testing.T) {
217217
}
218218
}
219219

220+
func TestDedupPathValues(t *testing.T) {
221+
var result []string
222+
223+
result = dedupPathValues(nil)
224+
if !expectStrings(result) {
225+
t.Error(result)
226+
}
227+
228+
result = dedupPathValues([]string{})
229+
if !expectStrings(result) {
230+
t.Error(result)
231+
}
232+
233+
result = dedupPathValues([]string{"/foo"})
234+
if !expectStrings(result, "/foo") {
235+
t.Error(result)
236+
}
237+
238+
result = dedupPathValues([]string{"/foo", "wow"})
239+
if !expectStrings(result, "/foo", "wow") {
240+
t.Error(result)
241+
}
242+
243+
result = dedupPathValues([]string{"/foo", "aa", "bb", "cc"})
244+
if !expectStrings(result, "/foo", "aa", "bb", "cc") {
245+
t.Error(result)
246+
}
247+
248+
result = dedupPathValues([]string{"/foo", "xx", "yy", "xx", "zz", "zz", "/foo"})
249+
if !expectStrings(result, "/foo", "xx", "yy", "zz", "/foo") {
250+
t.Error(result)
251+
}
252+
}
253+
220254
func TestNormalizeFilenames(t *testing.T) {
221255
files := []string{"", "abc/def.txt", "hello.txt"}
222256
normalized := normalizeFilenames(files)

0 commit comments

Comments
 (0)