Skip to content

fix: Ignore Content-Type header parameters when checking media type - #934

Open
zoliszabo wants to merge 3 commits into
jsonrainbow:mainfrom
zoliszabo:feature/ignore-content-type-header-parameters
Open

fix: Ignore Content-Type header parameters when checking media type#934
zoliszabo wants to merge 3 commits into
jsonrainbow:mainfrom
zoliszabo:feature/ignore-content-type-header-parameters

Conversation

@zoliszabo

Copy link
Copy Markdown

Description

Both HTTP retrievers (FileGetContents and Curl) capture the raw Content-Type header value verbatim (regex /Content-Type:(\V*)/ims), charset included. CDNs serve schemas with e.g. application/json; charset=utf-8, which fails the exact media type check against application/json in confirmMediaType().

RFC 8259 (JSON Data Interchange Format, https://www.rfc-editor.org/info/rfc8259/) does not define a charset parameter for the application/json media type: section 11 says "No 'charset' parameter is defined for this registration. Adding one really has no effect on compliant recipients.". The parameter therefore must not affect the comparison.

In the updated version, the retrievers capture only up to the first ; (/Content-Type:([^;\v]*)/ims), so the Content-Type is normalized to its base type before the check, ignoring any parameters.

CDN example: https://cdn.jsdelivr.net/gh/WordPress/gutenberg@trunk/schemas/json/theme.json.

Related Issue

N/A

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Other (please describe):

Checklist

  • I have read the CONTRIBUTING guidelines
  • My code follows the code style of this project
  • I have added tests that prove my fix is effective or that my feature works
  • All new and existing tests pass
  • I have updated the documentation accordingly
  • My changes generate no new warnings

Additional Notes

N/A

Both HTTP retrievers (`FileGetContents` and `Curl`) captured the raw `Content-Type` header value verbatim (regex `/Content-Type:(\V*)/ims`), charset included. CDNs serve schemas with e.g. `application/json; charset=utf-8`, which failed the exact media type check against `application/json` in confirmMediaType().

RFC 8259 (JSON Data Interchange Format, https://www.rfc-editor.org/info/rfc8259/) does not define a charset parameter for the application/json media type: section 11 says "No 'charset' parameter is defined for this registration. Adding one really has no effect on compliant recipients.". The parameter therefore must not affect the comparison.

The retrievers now capture only up to the first `;` (`/Content-Type:([^;\v]*)/ims`), so the `Content-Type` is normalized to its base type before the check.

CDN example: https://cdn.jsdelivr.net/gh/WordPress/gutenberg@trunk/schemas/json/theme.json.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the URI retrievers’ Content-Type parsing so media-type validation treats application/json; charset=utf-8 as application/json, preventing false failures when CDNs include parameters on JSON schema responses.

Changes:

  • Update Content-Type extraction regexes in FileGetContents and Curl to capture only the base media type (up to ;).
  • Add PHPUnit coverage to ensure Content-Type parameters are ignored for both retrievers.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/JsonSchema/Uri/Retrievers/FileGetContents.php Adjusts Content-Type header parsing to ignore parameters before media-type validation.
src/JsonSchema/Uri/Retrievers/Curl.php Adjusts Content-Type parsing from raw cURL response to ignore parameters before media-type validation.
tests/Uri/Retrievers/FileGetContentsTest.php Adds data-driven tests verifying Content-Type parameter stripping for FileGetContents.
tests/Uri/Retrievers/CurlTest.php Adds data-driven tests verifying Content-Type parameter stripping for Curl.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/JsonSchema/Uri/Retrievers/FileGetContents.php Outdated
Comment thread src/JsonSchema/Uri/Retrievers/Curl.php Outdated
Comment thread tests/Uri/Retrievers/CurlTest.php
The parameter-stripping capture `[^;\v]*` was introduced inside a double-quoted string, where PHP interprets `\v` as a vertical-tab character instead of passing it through to PCRE as the vertical-whitespace escape. For a Content-Type without parameters, the match could therefore run past the CRLF and capture subsequent headers and body.

Both retrievers now use a single-quoted, line-anchored pattern '/^Content-Type:([^;\v]*)/im': single-quoting lets the escape reach PCRE verbatim, and anchoring prevents headers like `X-Content-Type` from matching as a content type. The unused `s` modifier is dropped.

Tests are consolidated into data-provider driven cases covering the no-parameter form, charset and multiple parameters, and the `X-Content-Type` negative, so both the escaping and anchoring regressions are guarded.
@zoliszabo

Copy link
Copy Markdown
Author

Pushed a new commit with the regex fixes and consolidated tests.

@DannyvdSluijs

Copy link
Copy Markdown
Collaborator

@zoliszabo thanks for you contributions. I'm planning to review them in the upcoming week.

@DannyvdSluijs

Copy link
Copy Markdown
Collaborator

Seems all good. Will wait with the merge depending on #931 to avoid conflicts. Expect to merge this within a week.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants