Repository navigation
Return Basic realm from CONTENT_ORIGIN in WWW-Authenticate header - #1640
Merged
Merged
Conversation
Contributor
Reviewer's guide (collapsed on small PRs)Reviewer's GuideThe PR replaces the static Bearer challenge with a Basic challenge whose realm is derived from CONTENT_ORIGIN, including a production-host fallback, and updates the authentication error test to provide the required setting. Sequence diagram for the CONTENT_ORIGIN Basic authentication challengesequenceDiagram
participant Client
participant DRF
participant Auth as OrgGroupAssignmentMixin
participant Settings
Client->>DRF: Unauthenticated content-guarded request
DRF->>Auth: authenticate_header(request)
Auth->>Settings: CONTENT_ORIGIN
Auth->>Auth: urlparse(CONTENT_ORIGIN)
alt CONTENT_ORIGIN has a hostname
Auth-->>DRF: Basic realm=hostname
else hostname is missing
Auth-->>DRF: Basic realm=packages.redhat.com
end
DRF-->>Client: 401 with WWW-Authenticate header
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="pulp_service/pulp_service/app/authentication.py" line_range="84" />
<code_context>
def authenticate_header(self, request):
- return "Bearer"
+ hostname = urlparse(settings.CONTENT_ORIGIN).hostname or "packages.redhat.com"
+ return f'Basic realm="{hostname}"'
</code_context>
<issue_to_address>
**Guarded PyPI tests fail**
When the guarded-PyPI functional test runs, `test_unauthenticated_denied_on_guarded_pypi_simple_api` expects `WWW-Authenticate` to equal `Bearer`, but `authenticate_header()` returns a Basic challenge, so the assertion fails and the functional suite is red.
Update the functional test to assert the intended Basic challenge.
Also at `pulp_service/pulp_service/app/authentication.py:85`.
</issue_to_address>
### Comment 2
<location path="pulp_service/pulp_service/tests/unit/test_authentication_error_content_guard.py" line_range="97" />
<code_context>
+ def test_permit_maps_authentication_error_to_401(self, settings):
detail_guard = _detail_guard("identity", AuthenticationError("Authentication required."))
request = MagicMock(path="/lightwell/osv/record")
+ settings.CONTENT_ORIGIN = "https://packages.redhat.com"
with pytest.raises(HTTPUnauthorized) as exc_info:
</code_context>
<issue_to_address>
**Static realm passes the test**
When the realm implementation stops deriving its hostname from `CONTENT_ORIGIN` and always uses the `packages.redhat.com` fallback, `test_permit_maps_authentication_error_to_401` sets `CONTENT_ORIGIN` to `packages.redhat.com` at line 97 and checks that same hostname at line 103, so a constant `Basic realm="packages.redhat.com"` passes even when other environments receive the wrong realm.
Set `CONTENT_ORIGIN` to a different hostname in this test and assert that hostname appears in the challenge.
</issue_to_address>Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: pulp_service/pulp_service/app/authentication.py:84, pulp_service/pulp_service/tests/unit/test_authentication_error_content_guard.py:97
The OrgGroupAssignmentMixin.authenticate_header() was returning a static "Bearer" value. When DRF denied an unauthenticated request to a content-guarded PyPI view, the 401 response carried WWW-Authenticate: Bearer instead of a Basic challenge that prompts browsers for credentials. Derive the realm from settings.CONTENT_ORIGIN so the header matches the environment (e.g. packages.redhat.com vs packages.stage.redhat.com). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
git-hyagi
force-pushed
the
header-content-guard-www-auth
branch
from
October 23, 2026 15:09
98bbc43 to
4b93fd6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The OrgGroupAssignmentMixin.authenticate_header() was returning a static "Bearer" value. When DRF denied an unauthenticated request to a content-guarded PyPI view, the 401 response carried WWW-Authenticate: Bearer instead of a Basic challenge that prompts browsers for credentials.
Derive the realm from settings.CONTENT_ORIGIN so the header matches the environment (e.g. packages.redhat.com vs packages.stage.redhat.com).
ref: https://redhat.atlassian.net/browse/PULP-2594
Summary by Sourcery
Use the configured content origin to generate browser-compatible WWW-Authenticate Basic challenges.
Bug Fixes:
Tests: