feat(request): auto-inject dummy credentials for non-bearer auth schemes#180
Merged
feat(request): auto-inject dummy credentials for non-bearer auth schemes#180
Conversation
Generalises `auto_inject_dummy_bearer` into `auto_inject_dummy_credentials`, which fills dummy values for `apiKey` schemes (header / cookie / query) in addition to bearer. The legacy flag stays as a bearer-only alias for v1.x consumers and is bypassed when the new flag is on. Tests using `actingAs()` against endpoints whose spec declares cookie / apiKey authentication no longer false-fail under `auto_validate_request: true` — the validator's view receives synthetic credentials while the Symfony Request bag remains untouched, matching the existing bearer behaviour. Refs #178
Reviewer feedback follow-up: - Lift `in` and `name` onto the `apiKey()` factory of SchemeClassification so SecuritySchemeIntrospector reads the validated payload from the classification rather than re-validating $schemeDef. Drops the defensive literal-string guard that only existed to keep the array-shape return type provable. - Tighten test coverage: header/query empty-string-as-absent parity, multi-apiKey AND end-to-end via new petstore-3.0 fixture endpoint, and RuntimeException swallow contract pinned for the credentials path. - Comment cleanup: drop "byte-for-byte preserved" claim that no longer matches the delegated implementation, drop redundant restatements, abstract caller-name references that would rot on refactor, and add the missing notes on `security: []` opt-out and `_credentials` precedence.
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.
Summary
Generalises
auto_inject_dummy_bearerinto a superset configauto_inject_dummy_credentialsthat also fills dummy values forapiKeyschemes (header / cookie / query) in the validator's view. The legacy flag
stays as a bearer-only alias for v1.x consumers and is bypassed when the new
flag is on.
Why
Fixes #178.
When
auto_validate_request: trueis enabled in a consumer test suite, teststhat authenticate via
actingAs()(or auth middleware bypass) currentlyfalse-fail on every endpoint whose spec security uses cookie / apiKey-in-header
schemes — there is no synthetic equivalent to the bearer inject for those.
Reproduced in
studio-design/studio-apiPR #6617 with 250+ false-failsacross
CookieAuth, JWT-style apiKey-in-header, and similar requirements.The new flag fills the gap without touching the Symfony Request bag, so the
inject path stays consistent with existing behaviour: only the validator's
view is rewritten, and only when the test did not supply a real credential.
Verification
composer testpasses (1285 tests, 3070 assertions)composer stanpasses (PHPStan level 6 oversrc/+tests/)composer cs-checkpassesFailing-test-first workflow:
injectable_credentials_for_*cases inSecuritySchemeIntrospectorTest— confirmed red, then implemented.ValidatesOpenApiSchemaAutoInjectCredentialsTestcovering apiKeyheader / cookie / query, bearer upward-compat, AND-entry, existing-value
pass-through, empty-string-as-absent, no-mutation guarantee, loud-fail
on non-bool, and legacy flag interactions — confirmed red, then implemented.
ValidatesOpenApiSchemaAutoInjectBearerTestcontinues to passunmodified, confirming legacy
auto_inject_dummy_beareris byte-for-bytepreserved.
Notes for reviewers
SecurityValidator::classifyScheme()was promoted fromprivatetopublic staticsoSecuritySchemeIntrospector::injectableCredentialsFor()reuses the exact classification rules. Kept
@internal—Validation/Request/is not part of the SemVer surface.
SecuritySchemeIntrospector::endpointAcceptsBearer()is preserved for thelegacy
auto_inject_dummy_bearerpath and now delegates toinjectableCredentialsFor()for consistency.slotIsAlreadyPopulated()treats empty-string as "absent" to matchSecurityValidator::checkApiKeySatisfied()'s missing-value definition; thisis a marginal tightening over the legacy bearer check (which used a looser
!== '' && !== []test) but the existing bearer test suite does not exercisethe divergent case, and the new behaviour is the more correct of the two.
every supported scheme in the requirement is listed, and the trait writes
all of them. Over-injection on an OR-endpoint is harmless; under-injection
on an AND-endpoint would re-introduce the false-fail this feature exists
to prevent.
skip_security_validation(case C). Both are deferred to follow-up issuesto keep this PR focused on the case A default.