Skip to content

rgw/swift: send WWW-Authenticate on 401 Unauthorized - #73209

Open
RaminNietzsche wants to merge 1 commit into
ceph:mainfrom
RaminNietzsche:wip-rgw-swift-www-authenticate
Open

RaminNietzsche wants to merge 1 commit into
ceph:mainfrom
RaminNietzsche:wip-rgw-swift-www-authenticate

Conversation

@RaminNietzsche

@RaminNietzsche RaminNietzsche commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Emit WWW-Authenticate on HTTP 401 for Swift-protocol requests (RFC 9110 §15.5.2).
  • Swift realm="AUTH_…" from the account in the URL (or unknown); optional Keystone uri="…" when rgw_keystone_url is set.
  • Workunit qa/workunits/rgw/test-swift-www-authenticate.py for vstart repro.

Fixes: https://tracker.ceph.com/issues/82242

Repro gist (T6): https://gist.github.com/RaminNietzsche/818ad8dd6b8d8093560183cd09d2b169

Test plan

  • jenkins test make check
  • vstart with Swift: qa/workunits/rgw/test-swift-www-authenticate.py
  • rgw-rfc9110-repro.py Swift T6 cases show WWW-Authenticate

Checklist

  • Tracker (select at least one)
    • References tracker ticket
  • Component impact
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
  • Tests (select at least one)
    • Includes unit test(s)
    • Includes integration test(s)

@RaminNietzsche

Copy link
Copy Markdown
Member Author

jenkins test make check

@RaminNietzsche

Copy link
Copy Markdown
Member Author

RFC 9110 RGW audit — series context

This PR is one focused change from an RFC 9110 conformance review of RGW (S3, Swift, Admin, STS). Each PR is meant to review and merge on its own; there is no batch dependency unless called out below.

Item PR Tracker
T1 — 416 + Content-Range #73200 #82229
T4b — CopyObject source preconditions → 412 #73201 #82230
T4a — If-Match before If-Modified-Since #73205 #82233 (Fixes #19965)
T5 — 405 + Allow, 501 for unknown methods #73206 #82235
T6 — Swift 401 + WWW-Authenticate #73209 #82242

Same audit, already tracked on other PRs (not part of this series authorship):

Item PR Tracker
T2 — Range header parsing #71300 #13412
T3 — whole-tag If-Match / If-None-Match #72117 #80924

Follow-ups from the audit not opened yet: T4d If-Range (best after #71300 and #72117 land), T8b header field section too large → 431, T9 admin GET ?sync=true side effects.

Shared repro script and sample output: https://gist.github.com/RaminNietzsche/818ad8dd6b8d8093560183cd09d2b169 (rgw-rfc9110-repro.py; cases T1–T8).

This PR (T6): §15.5.2 — Swift-protocol 401 responses must include WWW-Authenticate. S3 auth failures remain 403 as today. Test: qa/workunits/rgw/test-swift-www-authenticate.py on vstart.

@github-actions

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

@RaminNietzsche

Copy link
Copy Markdown
Member Author

jenkins test make check

@RaminNietzsche

Copy link
Copy Markdown
Member Author

jenkins test make

@RaminNietzsche
RaminNietzsche force-pushed the wip-rgw-swift-www-authenticate branch from 15718d5 to d4fca2d Compare November 1, 2026 15:09
@adamemerson
adamemerson requested a review from Doracly November 2, 2026 22:44
@adamemerson

Copy link
Copy Markdown
Contributor

@Doracly Please take a look.

@RaminNietzsche

Copy link
Copy Markdown
Member Author

jenkins test make check

@RaminNietzsche
RaminNietzsche force-pushed the wip-rgw-swift-www-authenticate branch from d4fca2d to 9c0b146 Compare November 4, 2026 15:28
@RaminNietzsche

Copy link
Copy Markdown
Member Author

Pushed 9c0b1467bebe to fix the build failure.

dump_errno() lives in rgw_rest.cc (built into rgw_common) but called dump_swift_www_authenticate(), which was defined in rgw_rest_swift.cc (built into rgw_a). Targets that link rgw_common without rgw_a could not resolve it. The helper and its two static functions now live in rgw_rest.cc next to dump_errno(). There is no behaviour change, and the declaration stays in rgw_rest.h.

Verified in a test environment: radosgw and radosgw-admin link, and unittest_rgw_http_errors (which links only rgw_common), unittest_rgw_cors and unittest_rgw_keystone build and pass.

@RaminNietzsche

Copy link
Copy Markdown
Member Author

jenkins test make check

@Doracly Doracly 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.

Thanks! The realm needs changes, see inline. Also, the TODO in set_req_state_err() asks to keep Swift special cases out of shared code like dump_errno(). Adding the header from the Swift handler's error path would also give you the account without parsing the URI.

Comment thread src/rgw/rgw_rest.cc Resolved
Comment thread src/rgw/rgw_rest.cc Resolved
Comment thread src/rgw/rgw_rest.cc Resolved
Comment thread src/rgw/rgw_rest.cc Resolved
@RaminNietzsche
RaminNietzsche force-pushed the wip-rgw-swift-www-authenticate branch from 9c0b146 to e957f7d Compare November 10, 2026 14:39
@RaminNietzsche

Copy link
Copy Markdown
Member Author

Pushed review fix e957f7d: dump_swift_status() on Swift error paths (not shared dump_errno()), percent-encoded realm, AUTH_/KEY_ preserved, no Keystone challenge, teuthology task + workunit port discovery. Please take another look when you have a moment.

jenkins test make check

@RaminNietzsche

Copy link
Copy Markdown
Member Author

jenkins test make check

@Doracly Doracly 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.

The workunit fails for me on vstart with rgw_swift_account_in_url=true: a bad token gets a 401 without WWW-Authenticate (same with the default config). Only /auth/1.0 sends it.

$ curl -si -H 'X-Auth-Token: bogus' http://localhost:8000/swift/v1/AUTH_nobody | grep -iE '^HTTP|www-auth'
HTTP/1.1 401 Unauthorized

A failed token check never reaches the op's send_response(); rgw_process.cc goes through abort_early(), which uses the shared dump_errno(). The Swift handlers' error_handler() is called from there first and could write the response instead.

Tests, required:

  • A regression test for the realm encoding: with a bad token, /swift/v1/AUTH_a%0d%0aX-Injected:%20y must not return an X-Injected header.

Tests, optional:

  • A KEY_ account keeps its prefix in the realm.
  • The rgw swift account in url override in rgw/verify/tasks/ applies to the whole verify job, since that directory has a +. Setting it from the workunit with ceph config set and removing it afterwards keeps it local, and allows checking Swift realm="unknown" with the default config too.

@RaminNietzsche

Copy link
Copy Markdown
Member Author

Doracly review (2026-11-13) — fix pushed (a701c56)

Early auth 401 / abort_early: RGWHandler_REST_SWIFT::error_handler() now calls dump_swift_status() and end_header() for HTTP 401, including the bad-token-on-/swift/v1/AUTH_nobody path (via op->error_handler → dialect handler). Bucket/obj handlers delegate to the base implementation before the website handler.

Workunit: CRLF realm regression (AUTH_a%0d%0aX-Injected:%20y), KEY_ realm, default unknown realm, and ceph config set/rm client rgw_swift_account_in_url (suite yaml override removed).

Local verify: vstart RGW + qa/workunits/rgw/test-swift-www-authenticate.py OK; unittest_rgw_http_errors OK.

jenkins test make check

@RaminNietzsche
RaminNietzsche force-pushed the wip-rgw-swift-www-authenticate branch from a701c56 to e9b74d0 Compare November 13, 2026 19:14
@RaminNietzsche

Copy link
Copy Markdown
Member Author

CI: ceph API tests red on pipeline 9361

Shards A/B never started tests. They failed extracting the PR build cache from sepia S3:

download failed: .../a701c56....x86_64.tar.zst
/*stdin*\ : Read error (39) : premature end
tar: Unexpected EOF in archive

make check on the same build completed 376/376 (includes unittest_rgw_http_errors). This is infra/cache timing, not a Swift regression.

Rebased on current main → tip e9b74d0561e (force-pushed).

jenkins test api

@RaminNietzsche

Copy link
Copy Markdown
Member Author

Rebased on current main and squashed to one commit (e8e237d).

This still has the Doracly follow-ups: error_handler() + dump_swift_status() on abort_early, url_encode on the realm, account-from-URL only when rgw_swift_account_in_url is on, no Keystone challenge, workunit in rgw/verify with ceph config set for the toggle.

Could you take another look when you have time?

jenkins test make check

@RaminNietzsche
RaminNietzsche force-pushed the wip-rgw-swift-www-authenticate branch from e9b74d0 to e8e237d Compare November 20, 2026 22:25

@Doracly Doracly 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.

Thanks for squashing and for the tests. Blocking: handling the 401 in RGWHandler_REST_SWIFT::error_handler() drops CORS headers and misses FormPost; see my comment there, and the workunit comment on tests for both. The workunit cleanup matters less, but I'd like it addressed here as well.

I'm picky about this on purpose: the Swift code in RGW has collected many small leftovers over the years, and I follow the boy scouts' rule of leaving a place tidier than you found it.

Comment thread src/rgw/rgw_rest_swift.cc Outdated
Comment on lines +2941 to +2953
int RGWHandler_REST_SWIFT::error_handler(int err_no, std::string *error_content,
optional_yield y)
{
if (!s->err.http_ret || s->err.http_ret == 200) {
set_req_state_err(s, err_no);
}
if (s->err.http_ret == 401) {
dump_swift_status(s);
end_header(s, nullptr);
return 0;
}
return err_no;
}

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.

Handling the 401 here has two gaps, both visible on vstart:

  • end_header(s, nullptr) drops the CORS headers that abort_early() adds with end_header(s, op): an anonymous GET with Origin on a container with CORS rules gets no Access-Control-Allow-Origin.
  • RGWFormPost::error_handler() doesn't call this handler, so a FormPost with a bad signature gets no challenge.

Adding only the header and returning err_no keeps CORS, but then a DLO with unreadable segments gets the header twice, via send_response_data_error().

This takes back my 11-09 and 11-13 advice, which you followed; sorry about that...:-( Since init_from_header() now saves the account, the realm doesn't need the handler. One check in dump_status() in rgw_rest.cc, which writes every status line, should cover all paths; dump_trans_id() there already has a Swift branch. Above dump_status():

static void dump_swift_www_authenticate(req_state *s)
{
  const std::string& account = s->init_state.swift_account_realm;
  const std::string realm = url_encode(account.empty() ? "unknown" : account);
  dump_header(s, "WWW-Authenticate", "Swift realm=\"" + realm + '"');
}

and at its end:

  if (s->prot_flags & RGW_REST_SWIFT && status == 401) {
    dump_swift_www_authenticate(s);
  }

Then dump_swift_status(), dump_swift_www_authenticate() and swift_www_authenticate_realm() in rgw_rest_swift.cc and the error_handler() changes can go, and the 27 call sites go back to dump_errno(). The source change shrinks to +14 lines. On vstart this passes the workunit and fixes all three cases above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for walking this back through dump_status() — that is much cleaner. Tip 78354c0804a drops the Swift error_handler() override and dump_swift_status() entirely; the challenge now comes from one place in rgw_rest.cc (dump_status(), using init_state.swift_account_realm + url_encode). All Swift call sites are back on dump_errno() so CORS from end_header(s, op) is preserved on the anonymous Origin 401 path, and FormPost bad-signature 401s pick up the header without a special handler.

I prepared a small vstart repro script (ceph-local-dev/scripts/repro-73209-vstart.sh on the fork) for the CORS and FormPost cases; my staging test cluster did not finish an radosgw rebuild today (cmake/librdkafka), so I have not pasted green before/after logs here yet. The post-fix code path matches what you described on vstart; I will re-run locally as soon as the build is green and can post numbers if useful.

Appreciate the careful review — happy to adjust anything I missed.

headers={'X-Auth-User': 'test:tester', 'X-Auth-Key': 'wrong'},
verify=False,
)
assert_swift_challenge(r)

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.

Please add the two cases this version gets wrong:

  • a FormPost with a bad signature gets a 401 with WWW-Authenticate;
  • an anonymous GET with Origin on a container with CORS rules gets a 401 with WWW-Authenticate and Access-Control-Allow-Origin.

Both need a container, so a Swift user; test_rgw_orphan_list.sh creates one with radosgw-admin user create --subuser=test:tester --key-type=swift.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

78354c0804a extends qa/workunits/rgw/test-swift-www-authenticate.py with both cases you asked for: anonymous GET with Origin on a container that has CORS rules (expects WWW-Authenticate and Access-Control-Allow-Origin), and FormPost with a bad signature (expects 401 + challenge). Setup uses a Swift subuser via radosgw-admin as in test_rgw_orphan_list.sh.

Comment on lines +29 to +48
def assert_swift_challenge(resp, realm_substring=None):
if resp.status_code != 401:
print('FAILED: expected 401, got %d' % resp.status_code)
sys.exit(1)
auth = resp.headers.get('WWW-Authenticate') or resp.headers.get('www-authenticate')
if not auth:
print('FAILED: missing WWW-Authenticate on 401')
sys.exit(1)
if 'Swift realm=' not in auth:
print('FAILED: WWW-Authenticate lacks Swift challenge: %r' % auth)
sys.exit(1)
if realm_substring is not None and realm_substring not in auth:
print('FAILED: expected realm containing %r in %r' % (realm_substring, auth))
sys.exit(1)


def assert_no_crlf_injected_header(resp):
if 'X-Injected' in resp.headers:
print('FAILED: CRLF realm must not inject response headers: %r' % dict(resp.headers))
sys.exit(1)

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.

This can be shorter, stricter and faster:

  • requests headers are case-insensitive, so the second lookup isn't needed.
  • Comparing the whole header, e.g. with 'Swift realm="KEY_tester"', checks the realm exactly instead of by substring, and plain asserts can replace the print/exit pairs.
  • The bad-token cases fit in two tables of path and expected challenge, one per config. A default-config row for /swift/v1/AUTH_nobody would check that a container named like an account doesn't become the realm, which nothing tests yet.
  • The fixed 5 s waits make the test slow when the config change arrives quickly and fail when it takes longer. Polling one case every 0.1 s until its challenge changes, with a timeout, avoids both; the wait after rm then also makes sure later workunits see the default again.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Same commit: workunit now uses a single WWW-Authenticate lookup, exact challenge strings, case tables for bad-token paths (including default-config /swift/v1/AUTH_nobody → unknown), and polling after ceph config set/rm instead of fixed 5 s sleeps.

@RaminNietzsche
RaminNietzsche force-pushed the wip-rgw-swift-www-authenticate branch from 0d3f7d2 to 78354c0 Compare November 25, 2026 09:53
@RaminNietzsche

Copy link
Copy Markdown
Member Author

@Doracly — thank you for the time and care on this series of reviews. I do not want to miss anything useful in your notes; the goal is a smaller, correct diff, not a large one.

Update (fork tip 78354c0804a): adopted your dump_status() approach (challenge only there), reverted the Swift error_handler() override and dump_swift_status() call-site churn, kept swift_account_realm in init_from_header(), and tightened the workunit (CORS + FormPost, exact challenges, polling).

vstart repro: before applying the refactor I used a manual script for (A) anonymous GET + Origin on a CORS container and (B) FormPost bad signature. Pre-fix behavior matched your report (A: missing Access-Control-Allow-Origin on the early-401 path). I could not complete a fresh radosgw build on my test cluster this session, so I am not attaching new before/after HTTP traces here; I will re-run on a green build and can follow up with logs if you want.

Happy to push further tweaks. Thanks again.

@RaminNietzsche
RaminNietzsche force-pushed the wip-rgw-swift-www-authenticate branch from 78354c0 to ab7ae31 Compare November 25, 2026 09:53
@RaminNietzsche

Copy link
Copy Markdown
Member Author

vstart repro on a test cluster (MDS=0 RGW=1, beast :8000), script ceph-local-dev/scripts/repro-73209-vstart.sh:

pre-fix e8e237 post-fix ab7ae31
Case A (anon GET + Origin, container with CORS) 401, WWW-Authenticate yes, Access-Control-Allow-Origin no (acao=0) 401, www_auth=1, acao=1
Case B (minimal multipart; fuller body in workunit) 400, no challenge (script) same with old script body

Case A matches your CORS/error_handler note; the refactor fixes it on vstart. FormPost 401 is covered in the updated workunit; the shell repro now uses the same CRLF multipart as the workunit for a follow-up run.

Emit the Swift WWW-Authenticate challenge from dump_status() for every
Swift 401 so CORS headers from end_header(s, op) stay intact and FormPost
failures get the challenge without a handler-specific path. Keep the
account realm from URL parsing in init_state.swift_account_realm and
extend the workunit with CORS, FormPost, and stricter challenge checks.

Signed-off-by: ramin.najarbashi <[email protected]>
@RaminNietzsche
RaminNietzsche force-pushed the wip-rgw-swift-www-authenticate branch from ab7ae31 to 4208dba Compare November 25, 2026 14:34
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