Skip to content

fix(apiquery): reject non-string map keys - #88

Open
sylvesterkaczmarek wants to merge 3 commits into
openai:mainfrom
sylvesterkaczmarek:fix/apiquery-map-key-validation
Open

fix(apiquery): reject non-string map keys#88
sylvesterkaczmarek wants to merge 3 commits into
openai:mainfrom
sylvesterkaczmarek:fix/apiquery-map-key-validation

Conversation

@sylvesterkaczmarek

Copy link
Copy Markdown

Summary

Fail clearly when URL-query encoding receives a map whose keys are not strings, instead of serializing reflection placeholder text into parameter names.

Fixes #87.

Problem

internal/apiquery.encodeMap currently assumes every map key is a string and calls:

subkey := iter.Key().String()

without checking the key kind.

For a non-string reflect.Value, String() is not a conversion of the underlying key. It returns reflection's diagnostic representation. A value such as:

map[int]string{1: "one"}

can therefore proceed into request encoding with a malformed implementation-specific query key instead of failing at the serialization boundary.

Root cause

The map encoder relies on a string-key invariant that it never validates.

The sibling multipart/form encoder already rejects non-string map keys explicitly, so the two request encoders currently disagree on the same unsupported input shape.

Fix

Inspect each map key before using it:

mapKey := iter.Key()
if mapKey.Kind() != reflect.String {
    return nil, fmt.Errorf("apiquery: cannot encode a map with a non-string key")
}

String-key behavior and nested query formatting remain unchanged.

Regression coverage

Added focused tests proving:

  • map[int]string{1: "one"} returns a clear error and no query values;
  • an ordinary map[string]string continues to encode normally.

The invalid-map case exercises the public Marshal path rather than calling the internal encoder directly.

Validation

The branch is based directly on upstream main at d082a010f7c6cacf407d8a1581446a7857f9f1bb and is not behind it.

Production diff: 5 additions and 1 deletion in internal/apiquery/encoder.go, plus one focused regression file.

Full repository validation is left to the repository's GitHub Actions checks.

Risk

Low. The only newly rejected inputs are map shapes that cannot preserve their key semantics in URL query parameter names. Existing string-key maps, arrays, primitives, and nesting formats are unchanged.

@sylvesterkaczmarek
sylvesterkaczmarek requested a review from a team as a code owner August 18, 2026 11:08
@markstuart-oai

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 6, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-06T07:55:26.263530Z 1b44518 New commits
🔒 Security Review Completed 2026-09-06T07:56:25.511270Z 1b44518 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c628bb98a7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/apiquery/encoder.go Outdated
Comment on lines +49 to +51
mapKey := iter.Key()
if mapKey.Kind() != reflect.String {
return nil, fmt.Errorf("apiquery: cannot encode a map with a non-string key")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Validate the map key type before iterating

Because this check only runs for entries returned by MapRange, Marshal(map[int]string{}) and Marshal(map[int]string(nil)) still succeed with empty query values even though their map key type is unsupported. This makes rejection depend on whether the map happens to contain data; inspect value.Type().Key().Kind() before starting the iteration so empty and populated non-string-keyed maps follow the same contract.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1b44518. Map key type is now validated before iteration, so populated, empty, and nil non-string-key maps are rejected consistently. Focused apiquery tests pass.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1b44518. The encoder now validates the map key type before iterating, so empty and nil non-string-key maps fail consistently with populated maps. Added regressions for both empty and nil cases. go test ./internal/apiquery passes.

@chatgpt-codex-connector

Copy link
Copy Markdown

Security review completed. No security issues were found in this pull request.

Reviewed commit: c628bb98a7

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query encoder silently serializes non-string map keys as reflection placeholders

2 participants