Skip to content

Fixes #38424: correct response code of oauth2 token request to registry #11389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MartinSpiessl
Copy link

@MartinSpiessl MartinSpiessl commented May 14, 2025

If oauth2 authentication (via POST request) is not supported, a container registry should respond with code 404, but we currently respond with 415 (Unsupported Media Type). By disabling the media type check for the token endpoint, we will return 401 (unauthorized) instead, which is enough for some clients to then try the regular token authentication. Adding a custom before_action, we can achieve that 404 is returned, thus we are fully compliant with the spec:
https://docker-docs.uclv.cu/registry/spec/auth/oauth/

What are the changes introduced in this pull request?

  • content type check is disabled for :token
  • POST requests on :token trigger a 404 response (the spec demands to return 404 if this is not supported)

Considerations taken when implementing this change?

  • Adhere to the spec
  • only disable media type check where necessary

What are the testing steps for this pull request?

The following curl request can be used to test the response (replace foreman.example.com with your foreman instance, orgname with your orgname, productname with your product name for the container registry, and containername is arbitrary, as is username and password):

curl -k -X POST "https://forman.example.com/v2/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "scope=repository:orgname/productname/containername:pull,push repository:registry:pull,push" \
--data-urlencode "grant_type=password" --data-urlencode "username=myuser" \
--data-urlencode "password=mypassword" \
--data-urlencode "service=foreman.example.com" -w "\\n%{http_code}\\n"

This will yield the following output:

{
"error": {"message":"Media type in 'Content-Type: application/x-www-form-urlencoded' is unsupported in API v2 for POST and PUT requests. Please use 'Content-Type: application/json'."}
}


415

With the fix applied the output is simply:


404

Summary by Sourcery

Ensure compliance with OAuth2 spec by returning 404 for unsupported POST requests to the /v2/token endpoint.

Bug Fixes:

  • Return 404 for POST requests to the OAuth2 token endpoint instead of 415 error when unsupported.

Enhancements:

  • Add token_request_type_check before_action and skip media type validation for the token endpoint.

If oauth2 authentication (via POST request) is not supported,
the registry should respond with code 404, but we currently respond
with 415 (Unsupported Media Type). By disabling the media type check
for the token endpoint, we will return 401 (unauthorized) instead,
which is enough for some clients to then try the regular token authentication.
Adding a custom before_action, we can achieve that 404 is returned,
thus we are fully compliant with the spec:
https://docker-docs.uclv.cu/registry/spec/auth/oauth/
Copy link

sourcery-ai bot commented May 14, 2025

Reviewer's Guide

Disable media type validation and add a pre-filter on the token endpoint so that unsupported POST requests return 404, bringing OAuth2 token handling into spec compliance.

File-Level Changes

Change Details Files
Skip media type check on the :token action
  • Added :token to skip_before_action :check_media_type
app/controllers/katello/api/registry/registry_proxies_controller.rb
Introduce a custom before_action to return 404 for POST token requests
  • Registered token_request_type_check for :token
  • token_request_type_check issues head :not_found and halts on POST
app/controllers/katello/api/registry/registry_proxies_controller.rb

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @MartinSpiessl - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Member

@ianballou ianballou left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution @MartinSpiessl!

I believe there's a more straight-forward way to make POST requests return 404s via our registry:

match '/v2/token' => 'registry_proxies#token', :via => :post

If this line is removed, sending a POST to the token endpoint should result in a 404.


However, this makes me wonder why we have defined a POST endpoint for tokens if it does not work. It looks like the endpoint fails because our API only expects JSON input, not application/x-www-form-urlencoded.

It looks like it was added 7 years ago: #7249
I suppose either the standard changed at some point, or perhaps even the POST route snuck in without proper testing.

It would be nice to get this endpoint to work properly -- we might not be far away from a working implementation. I won't make that a requirement of this PR though. I'll at least make sure a redmine gets filed for it depending on the outcome of this PR.

@ianballou
Copy link
Member

To add to my review - is there a deeper end goal for some specific tool / client to work with our registry?

@nadjaheitmann
Copy link
Contributor

@MartinSpiessl Do you mind coming back to this?

@MartinSpiessl
Copy link
Author

@MartinSpiessl Do you mind coming back to this?

Yes, sorry, I will look at it today, I had a bit of a pile up of todos here recently

is there a deeper end goal for some specific tool / client to work with our registry?

For now the goal is to make it work with helm, where there are also other issues things I am currently investigating.

It would be nice to get this endpoint to work properly -- we might not be far away from a working implementation. I won't make that a requirement of this PR though. I'll at least make sure a redmine gets filed for it depending on the outcome of this PR.

Thanks! Yes it would be great to have that end point work, but for now I would focus on get the registry work for helm, and I would treat the POST endpoint as an orthogonal issue.

I believe there's a more straight-forward way to make POST requests return 404s via our registry:

Thanks, I will try that out and get back with an update to this PR.

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.

3 participants