Implement strict RFC 5987 header encoding to fix Next.js proxy crash
Some checks failed
Automated Container Build / build-and-push (push) Failing after 40s

This commit is contained in:
Elijah 2026-05-31 22:27:04 -07:00
parent 855c42edd9
commit d139501dd4

View file

@ -1028,8 +1028,29 @@ func (h *FSHandler) Download(c *fiber.Ctx) error {
c.Set("Accept-Ranges", "bytes")
c.Set("Content-Type", mime.TypeByExtension(filepath.Ext(resolvedPath)))
safeNameQuote := strings.ReplaceAll(info.Name(), "\"", "\\\"")
encodedName := url.PathEscape(info.Name())
// Ultra-safe fallback name: replace quotes and non-ASCII with underscore
safeNameBytes := make([]byte, 0, len(info.Name()))
for i := 0; i < len(info.Name()); i++ {
b := info.Name()[i]
if b >= 32 && b <= 126 && b != '"' && b != '\\' {
safeNameBytes = append(safeNameBytes, b)
} else {
safeNameBytes = append(safeNameBytes, '_')
}
}
safeNameQuote := string(safeNameBytes)
// Strict RFC 5987 encoding for filename*=
var sb strings.Builder
for i := 0; i < len(info.Name()); i++ {
b := info.Name()[i]
if (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') || (b >= '0' && b <= '9') || b == '.' || b == '-' || b == '_' || b == '~' {
sb.WriteByte(b)
} else {
sb.WriteString(fmt.Sprintf("%%%02X", b))
}
}
encodedName := sb.String()
if c.Query("download") == "true" {
c.Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"; filename*=UTF-8''%s", safeNameQuote, encodedName))