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:
parent
2d3262c8aa
commit
c36b521bf1
1 changed files with 190 additions and 1 deletions
|
|
@ -4,6 +4,8 @@ import (
|
|||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/modelcontextprotocol/registry/cmd/publisher/commands"
|
||||
|
|
@ -37,6 +39,57 @@ func TestPublishCommand_Success(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestPublishCommand_PreservesNonASCIIDescription(t *testing.T) {
|
||||
server := SetupMockRegistryServer(t,
|
||||
func(w http.ResponseWriter, r *http.Request) {
|
||||
var req apiv0.ServerJSON
|
||||
require.NoError(t, json.NewDecoder(r.Body).Decode(&req))
|
||||
assert.Equal(t, "Tools for agents — safely", req.Description)
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_ = json.NewEncoder(w).Encode(apiv0.ServerResponse{
|
||||
Server: apiv0.ServerJSON{
|
||||
Name: req.Name,
|
||||
Version: req.Version,
|
||||
},
|
||||
})
|
||||
},
|
||||
nil,
|
||||
)
|
||||
SetupTestToken(t, server.URL, "test-token")
|
||||
|
||||
CreateTestServerJSON(t, apiv0.ServerJSON{
|
||||
Schema: model.CurrentSchemaURL,
|
||||
Name: "com.example/test-server",
|
||||
Description: "Tools for agents — safely",
|
||||
Version: "1.0.0",
|
||||
})
|
||||
|
||||
require.NoError(t, commands.PublishCommand([]string{}))
|
||||
}
|
||||
|
||||
func TestPublishCommand_RejectsInvalidUTF8ServerJSON(t *testing.T) {
|
||||
createRawServerJSON(t, []byte{
|
||||
'{', '"', '$', 's', 'c', 'h', 'e', 'm', 'a', '"', ':', '"', '"', ',',
|
||||
'"', 'n', 'a', 'm', 'e', '"', ':', '"', 'c', 'o', 'm', '.', 'e', 'x', 'a', 'm', 'p', 'l', 'e', '/', 't', 'e', 's', 't', '"', ',',
|
||||
'"', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', '"', ':', '"', 'b', 'a', 'd', ' ', 0xe2, '"', ',',
|
||||
'"', 'v', 'e', 'r', 's', 'i', 'o', 'n', '"', ':', '"', '1', '.', '0', '.', '0', '"', '}',
|
||||
})
|
||||
|
||||
err := commands.PublishCommand([]string{})
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "UTF-8")
|
||||
}
|
||||
|
||||
func TestPublishCommand_RejectsUnpairedSurrogateEscape(t *testing.T) {
|
||||
createRawServerJSON(t, []byte(`{"$schema":"","name":"com.example/test","description":"bad \udc94","version":"1.0.0"}`))
|
||||
|
||||
err := commands.PublishCommand([]string{})
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "unpaired UTF-16 surrogate")
|
||||
assert.Contains(t, err.Error(), "$.description")
|
||||
}
|
||||
|
||||
func TestPublishCommand_422ValidationFlow(t *testing.T) {
|
||||
validateCallCount := 0
|
||||
publishCallCount := 0
|
||||
|
|
@ -102,6 +155,20 @@ func TestPublishCommand_422ValidationFlow(t *testing.T) {
|
|||
assert.Equal(t, 1, validateCallCount, "validate endpoint should be called once after 422")
|
||||
}
|
||||
|
||||
func createRawServerJSON(t *testing.T, data []byte) {
|
||||
t.Helper()
|
||||
|
||||
tempDir := t.TempDir()
|
||||
serverFile := filepath.Join(tempDir, "server.json")
|
||||
require.NoError(t, os.WriteFile(serverFile, data, 0600))
|
||||
|
||||
originalDir, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() { _ = os.Chdir(originalDir) })
|
||||
|
||||
require.NoError(t, os.Chdir(tempDir))
|
||||
}
|
||||
|
||||
func TestPublishCommand_422WithMultipleIssues(t *testing.T) {
|
||||
validateCallCount := 0
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue