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

@ -28,6 +28,9 @@ func PublishCommand(args []string) error {
}
return fmt.Errorf("failed to read server.json: %w", err)
}
if err := validateJSONUnicode(serverFile, serverData); err != nil {
return err
}
// Validate JSON
var serverJSON apiv0.ServerJSON
@ -99,6 +102,10 @@ func PublishCommand(args []string) error {
}
func publishToRegistry(registryURL string, serverData []byte, token string) (*apiv0.ServerResponse, int, error) {
if err := validateJSONUnicode("server.json", serverData); err != nil {
return nil, 0, err
}
// Parse the server JSON data
var serverJSON apiv0.ServerJSON
err := json.Unmarshal(serverData, &serverJSON)