mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-15 21:03: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
@@ -408,24 +408,21 @@ func decodeFilenameParam(filename string) string {
|
||||
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
|
||||
// This handles cases where servers HTML-encode special characters like & to &
|
||||
func unescapeHTMLEntities(s string) string {
|
||||
// Common HTML entities that might appear in filenames
|
||||
replacements := map[string]string{
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
""": "\"",
|
||||
"'": "'",
|
||||
"'": "'",
|
||||
}
|
||||
|
||||
result := s
|
||||
for entity, char := range replacements {
|
||||
result = strings.ReplaceAll(result, entity, char)
|
||||
}
|
||||
return result
|
||||
return htmlEntityReplacer.Replace(s)
|
||||
}
|
||||
|
||||
// tryDecodeGBK attempts to decode string as GBK encoding
|
||||
|
||||
Reference in new issue
Block a user