fix: reject mangled publisher metadata (#1310)

## Summary
- add a publisher preflight that rejects non-UTF-8 `server.json` bytes
before parsing
- reject unpaired UTF-16 surrogate escapes inside JSON string fields
before publish/validate requests are sent
- keep valid non-ASCII descriptions, including em dashes, round-tripping
through the publish path
- make the publisher test token helper set `USERPROFILE` as well as
`HOME` so Windows tests use the temp home directory

Closes #1306.

## To verify
- `go test ./cmd/publisher/commands -run
"TestPublishCommand_(Success|PreservesNonASCIIDescription|RejectsInvalidUTF8ServerJSON|RejectsUnpairedSurrogateEscape)$"
-count=1`
- `go test ./cmd/publisher/commands -run TestPublishCommand -count=1`
- `go test ./cmd/publisher/commands -run TestValidateCommand -count=1`
- `git diff --check`

Co-authored-by: Radoslav Dimitrov <radoslav@stacklok.com>
This commit is contained in:
Yufeng He 2026-07-13 00:57:45 +08:00 • committed by GitHub
commit c36b521bf1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -142,6 +142,9 @@ func ValidateCommand(args []string) error {
}
return fmt.Errorf("failed to read %s: %w", serverFile, err)
}
if err := validateJSONUnicode(serverFile, serverData); err != nil {
return err
}
// Validate JSON
var serverJSON apiv0.ServerJSON
@ -188,6 +191,10 @@ func ValidateCommand(args []string) error {
// validateViaAPI calls the /validate endpoint on the registry
func validateViaAPI(registryURL string, serverData []byte) (*validators.ValidationResult, error) {
if err := validateJSONUnicode("server.json", serverData); err != nil {
return nil, err
}
// Parse the server JSON data to ensure it's valid JSON
var serverJSON apiv0.ServerJSON
err := json.Unmarshal(serverData, &serverJSON)