mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-17 13:53:41 +00:00
fix(http): decode HTML entities in filenames in one pass (#1651)
unescapeHTMLEntities applied its replacements while ranging over a map, so the order changed from run to run. An escaped ampersand such as "a&lt;b.txt" came out as "a<b.txt" or, when & was replaced first, as the double-decoded "a<b.txt". Replace all entities in a single pass with strings.NewReplacer and cover the escaped-ampersand cases in TestUnescapeHTMLEntities, running each case repeatedly so an order-dependent implementation fails.
This commit is contained in:
1 parent
89dfbe4c45
commit
6cd95507fd
2 files changed
+28
-18
No files matched your search
@@ -125,13 +125,26 @@ func TestUnescapeHTMLEntities(t *testing.T) {
|
|||||||
in: "normal.txt",
|
in: "normal.txt",
|
||||||
want: "normal.txt",
|
want: "normal.txt",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "escaped ampersand before entity name is decoded once",
|
||||||
|
in: "a&lt;b.txt",
|
||||||
|
want: "a<b.txt",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "escaped ampersand before quote entity is decoded once",
|
||||||
|
in: "a&quot;b.txt",
|
||||||
|
want: "a"b.txt",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
got := unescapeHTMLEntities(tt.in)
|
// Run repeatedly so that an order-dependent implementation fails reliably.
|
||||||
if got != tt.want {
|
for i := 0; i < 200; i++ {
|
||||||
t.Errorf("unescapeHTMLEntities() = %q, want %q", got, tt.want)
|
got := unescapeHTMLEntities(tt.in)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Fatalf("unescapeHTMLEntities() = %q, want %q (run %d)", got, tt.want, i)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -408,24 +408,21 @@ func decodeFilenameParam(filename string) string {
|
|||||||
return decoded
|
return decoded
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// htmlEntityReplacer replaces every entity in a single left-to-right pass, so an
|
||||||
|
// escaped ampersand such as "&lt;" becomes "<" and is not decoded twice.
|
||||||
|
var htmlEntityReplacer = strings.NewReplacer(
|
||||||
|
"&", "&",
|
||||||
|
"<", "<",
|
||||||
|
">", ">",
|
||||||
|
""", "\"",
|
||||||
|
"'", "'",
|
||||||
|
"'", "'",
|
||||||
|
)
|
||||||
|
|
||||||
// unescapeHTMLEntities unescapes common HTML entities in filenames
|
// unescapeHTMLEntities unescapes common HTML entities in filenames
|
||||||
// This handles cases where servers HTML-encode special characters like & to &
|
// This handles cases where servers HTML-encode special characters like & to &
|
||||||
func unescapeHTMLEntities(s string) string {
|
func unescapeHTMLEntities(s string) string {
|
||||||
// Common HTML entities that might appear in filenames
|
return htmlEntityReplacer.Replace(s)
|
||||||
replacements := map[string]string{
|
|
||||||
"&": "&",
|
|
||||||
"<": "<",
|
|
||||||
">": ">",
|
|
||||||
""": "\"",
|
|
||||||
"'": "'",
|
|
||||||
"'": "'",
|
|
||||||
}
|
|
||||||
|
|
||||||
result := s
|
|
||||||
for entity, char := range replacements {
|
|
||||||
result = strings.ReplaceAll(result, entity, char)
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// tryDecodeGBK attempts to decode string as GBK encoding
|
// tryDecodeGBK attempts to decode string as GBK encoding
|
||||||
|
|||||||
Reference in new issue
Block a user