Skip to content

Return HTTP 400 for malformed Content-Type header - #50109

Open
MdTanwer wants to merge 1 commit into
keycloak:mainfrom
MdTanwer:fix/49785-malformed-content-type-header
Open

Return HTTP 400 for malformed Content-Type header#50109
MdTanwer wants to merge 1 commit into
keycloak:mainfrom
MdTanwer:fix/49785-malformed-content-type-header

Conversation

@MdTanwer

@MdTanwer MdTanwer commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Reject syntactically invalid Content-Type headers with HTTP 400 via a new request filter
  • Add defensive parsing in DefaultSecurityHeadersProvider to prevent IllegalArgumentException from becoming HTTP 500

Closes #49785

@MdTanwer
MdTanwer requested a review from a team as a code owner June 18, 2026 09:12
Copilot AI review requested due to automatic review settings June 18, 2026 09:12
@MdTanwer
MdTanwer marked this pull request as draft June 18, 2026 09:12

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 addresses Keycloak’s handling of syntactically malformed Content-Type request headers by ensuring they result in a 400 Bad Request instead of triggering an unhandled exception during response filter processing (which previously produced HTTP 500 and ERROR logs).

Changes:

  • Adds a new pre-matching request filter to reject invalid Content-Type header values with HTTP 400.
  • Adds defensive handling in DefaultSecurityHeadersProvider to avoid IllegalArgumentException during request media type access.
  • Adds an integration test covering malformed Content-Type behavior for UserInfo and well-known endpoints.

Reviewed changes

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

File Description
tests/base/src/test/java/org/keycloak/tests/oidc/MalformedContentTypeHeaderTest.java Adds integration tests asserting malformed Content-Type returns 400 and valid values are not rejected.
services/src/main/java/org/keycloak/services/filters/InvalidContentTypeHeaderFilter.java Introduces a pre-matching request filter that validates Content-Type and rejects invalid values with 400.
services/src/main/java/org/keycloak/headers/DefaultSecurityHeadersProvider.java Wraps request media type retrieval to prevent malformed headers from causing an unhandled exception during header processing.

Comment thread services/src/main/java/org/keycloak/headers/DefaultSecurityHeadersProvider.java Outdated
@ahus1

ahus1 commented Jun 19, 2026

Copy link
Copy Markdown
Member

As of CONTRIBUTING.md, "new contributors may have at most 2 open pull requests at a time". Closing this one due to that.

@ahus1 ahus1 closed this Jun 19, 2026
@MdTanwer

Copy link
Copy Markdown
Contributor Author

Hi @ahus1, since my other PR is now merged, could we reopen this one for review? Thanks!

@ahus1 ahus1 reopened this Jun 24, 2026
@ahus1

ahus1 commented Jun 24, 2026

Copy link
Copy Markdown
Member

This PR is now in draft mode. It misses a DCO signoff, and there are also some GitHub copilot comments. Please fix and review, rebase, and then mark as ready-for-review.

@MdTanwer
MdTanwer force-pushed the fix/49785-malformed-content-type-header branch from 6077b60 to c1c4455 Compare June 25, 2026 04:39
@MdTanwer
MdTanwer marked this pull request as ready for review June 25, 2026 04:40
Copilot AI review requested due to automatic review settings June 25, 2026 04:40

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@MdTanwer
MdTanwer force-pushed the fix/49785-malformed-content-type-header branch from c1c4455 to 68c2fbf Compare June 25, 2026 04:48
@MdTanwer
MdTanwer requested a review from Copilot June 25, 2026 04:52

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

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

@MdTanwer
MdTanwer force-pushed the fix/49785-malformed-content-type-header branch from 68c2fbf to 3bc7258 Compare June 25, 2026 05:06
@MdTanwer
MdTanwer requested a review from Copilot June 25, 2026 05:06

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

@MdTanwer
MdTanwer force-pushed the fix/49785-malformed-content-type-header branch from 3bc7258 to a3b0fa5 Compare June 25, 2026 05:13
@MdTanwer
MdTanwer requested a review from Copilot June 25, 2026 05:13

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

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

Copilot AI review requested due to automatic review settings June 25, 2026 05:22
@MdTanwer
MdTanwer force-pushed the fix/49785-malformed-content-type-header branch from 6129bbe to a85eb79 Compare June 25, 2026 05:25

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

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

@MdTanwer

MdTanwer commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Hey @jordandunmire97-ai, could you take a look at this PR when you have some time? I think it's healthy and ready to merge. ):

@MdTanwer

MdTanwer commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Hey @jordandunmire97-ai, @ahus1 would you mind taking a look? Is this PR mergeable, or is there anything else needed from my side?

Copilot AI review requested due to automatic review settings July 27, 2026 11:57
@MdTanwer
MdTanwer force-pushed the fix/49785-malformed-content-type-header branch from a85eb79 to 7279f81 Compare July 27, 2026 11:57

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

Reject syntactically invalid Content-Type values early in a request filter
and add defensive parsing in DefaultSecurityHeadersProvider to prevent
IllegalArgumentException from surfacing as HTTP 500.

Register the new oidc test package in Base2TestSuite so CI validation passes.

Closes keycloak#49785

Signed-off-by: MdTanwer <tanw9004167@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI review requested due to automatic review settings July 27, 2026 12:03
@MdTanwer
MdTanwer force-pushed the fix/49785-malformed-content-type-header branch from 7279f81 to 8c6b40b Compare July 27, 2026 12:03

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

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

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.

UserInfo Endpoint: Handle malformed Content-Type header gracefully

4 participants