Apply resource-wide security to every endpoint when no methods are listed - #97
dhruv-techdev wants to merge 2 commits into
Conversation
|
Hi! This PR is ready for review whenever you get a chance. Happy to make any changes. Thanks! @ebourgeois |
ebourgeois
left a comment
There was a problem hiding this comment.
Review of the fix for #96. The change itself works: the new tests pass and black and pylint are clean. The findings below are what it exposes or leaves open. Inline comments cover lines in the diff; these five are on lines the PR does not touch:
-
Specs with two schemes now break (
firestone/spec/openapi.py:560).add_rsrc_componentsreplacescomponents['securitySchemes']per resource instead of merging. With resource foo using onlybearer_authand baz using onlyapi_key(reproduced), securitySchemes contains onlyapi_keybut GET /foo hassecurity: [{bearer_auth: []}]. The spec is invalid and validators/openapi-generator reject it; before this PR the same input produced a valid spec. Fix:components.setdefault('securitySchemes', {}).update(security['scheme']). -
Regenerated Rust servers stop compiling (
firestone/spec/server_rust.py:496). Cargo.toml is a kept scaffold and only getssubtlewhen the server is secured. A server generated earlier from a scheme-only resource has nosubtle; regenerating now rewrites lib.rs withpub mod auth;and writes auth.rs withuse subtle::Choice, while Cargo.toml is kept.cargo buildfails on the unresolved import unless the user passes--forceand loses their scaffold. (From reading the code; not compiled.) -
Only the first scheme is ever applied (
firestone/spec/openapi.py:283).list(security['scheme'].keys())[0]meansscheme: {bearer_auth: ..., api_key: ...}secures every operation withbearer_authonly (reproduced);api_keyis emitted but never accepted, so API-key clients get 401 everywhere instead of an OR of both schemes. -
Docs are still wrong and the behaviour change is undocumented (
docs/site/content/advanced-topics/security-schemes.md:32). The doc cited by the issue shows OpenAPI-stylesecuritySchemes:andsecurity: - BearerAuth: []list syntax that resource.yaml rejects. Existing scheme-only users now get every operation secured, GET and HEAD included, so regenerated servers answer 401 untilAPI_TOKENSis set, with no doc or release note. -
Dead resource-wide security path in the template (
firestone/schema/openapi.jinja2:11). The top-levelsecurity:block keyed onsecurity_namecan never render becausegenerate()never passessecurity_name. The PR removes the last remnant of that path (rsrc['security'] = [...]) but leaves this branch, the same kind of misleading dead path that caused #96.
Verified by applying the diff and running the suite on Python 3.13: 216 passed, 18 failed, all traced to celpy missing in the test container, not to this PR.
|
Thanks for the detailed review, @ebourgeois ! You're right on all of these. I'll update this PR to:
For the Rust |
Same PR is fine. |
…mes, Cargo.toml check, docs
|
Thanks @ebourgeois, I've pushed an update covering the whole review:
black and pylint are clean, and the example |
Fixes #96
What was wrong
If a resource set a security scheme without listing methods, no endpoint got marked as secured. The docs say it should apply to all methods. Nothing errored, so a server generated from the spec could accept requests without any login.
What this PR changes
In
openapi.py, the code used to save the security setting intorsrc["security"], which nothing reads. Now, when no per-method lists are given, it fills them in with every method:The existing per-method code then secures every endpoint as expected.
Tests
Added two tests in
test/spec/test_openapi.py:I regenerated
examples/addressbook/openapi.yaml, and it didn't change, since those resources already list their methods.How to test
Or by hand, with a resource that has only
security.schemeset:Before: the only match is
securitySchemes:.After: every endpoint shows
security: - bearer_auth: [].