Return 422 Unprocessable Entity on invalid archive uploads.

Fixes: https://codeberg.org/git-pages/git-pages/issues/216
This commit is contained in:
miyuko
2026-06-19 03:09:15 +01:00
parent 2737f275cf
commit f620381551
2 changed files with 40 additions and 0 deletions
+38
View File
@@ -244,3 +244,41 @@ func ExtractZip(ctx context.Context, reader io.Reader, oldManifest *Manifest) (*
return manifest, nil
}
func IsArchiveParseError(err error) bool {
targets := []error{
tar.ErrHeader,
tar.ErrWriteTooLong,
tar.ErrFieldTooLong,
tar.ErrWriteAfterClose,
tar.ErrInsecurePath,
zip.ErrFormat,
zip.ErrAlgorithm,
zip.ErrChecksum,
zip.ErrInsecurePath,
gzip.ErrChecksum,
gzip.ErrHeader,
zstd.ErrReservedBlockType,
zstd.ErrCompressedSizeTooBig,
zstd.ErrBlockTooSmall,
zstd.ErrUnexpectedBlockSize,
zstd.ErrMagicMismatch,
zstd.ErrWindowSizeExceeded,
zstd.ErrWindowSizeTooSmall,
zstd.ErrDecoderSizeExceeded,
zstd.ErrUnknownDictionary,
zstd.ErrFrameSizeExceeded,
zstd.ErrFrameSizeMismatch,
zstd.ErrCRCMismatch,
}
for _, target := range targets {
if errors.Is(err, target) {
return true
}
}
return false
}
+2
View File
@@ -712,6 +712,8 @@ func reportUpdateResult(w http.ResponseWriter, r *http.Request, result UpdateRes
w.WriteHeader(http.StatusUnsupportedMediaType)
} else if errors.Is(result.err, ErrArchiveTooLarge) {
w.WriteHeader(http.StatusRequestEntityTooLarge)
} else if IsArchiveParseError(result.err) {
w.WriteHeader(http.StatusUnprocessableEntity)
} else if errors.Is(result.err, ErrRepositoryTooLarge) {
w.WriteHeader(http.StatusUnprocessableEntity)
} else if errors.Is(result.err, ErrMalformedPatch) {