Fix isPublic plugin/prefix scope leak and hasRole type juggling#165
Merged
Conversation
AclTrait::_isPublic() (src/Auth/AclTrait.php:317) used asymmetric guards that short-circuited to false when the request had no plugin or prefix, so a plugin-scoped or prefix-scoped allow rule would match a request without one. Mirror the symmetric check from AllowTrait::_getAllowRule() so a rule is skipped whenever either side has a scope and they do not match. AuthUserTrait::hasRole() (src/Auth/AuthUserTrait.php:122) used a loose in_array() that matched 0 against any non-numeric string and treated auto-int array keys as alias matches. Coerce both sides to string and compare strictly while still treating numeric-string and int as the same role id; only string array keys count as alias matches. Reject non-scalar inputs early. Adds regression tests for both code paths.
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #165 +/- ##
============================================
- Coverage 67.68% 67.59% -0.10%
- Complexity 621 627 +6
============================================
Files 29 29
Lines 1600 1614 +14
============================================
+ Hits 1083 1091 +8
- Misses 517 523 +6 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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
Two authorization-correctness fixes in security-critical code paths.
AclTrait::_isPublic()— asymmetric scope guardsrc/Auth/AclTrait.php:317used:The first conjunct short-circuits to
falsewhen the request has no plugin/prefix, so the rule is not skipped even though it was authored for a specific plugin or prefix. A rule defined forFoo.Tags::indexcould mark a non-plugin request toTags::indexas public — the canonicalAllowTrait::_getAllowRule()(src/Auth/AllowTrait.php:33-50) does not have this asymmetry. The fix mirrors that symmetric guard so a rule is skipped whenever either side has a scope and they do not match.AuthUserTrait::hasRole()— loosein_arrayand stray auto-int keyssrc/Auth/AuthUserTrait.php:122calledin_array($expectedRole, $roles)without strict mode, sohasRole(0, ['admin', 'moderator'])returnedtrue(loose0 == 'admin'). Thearray_key_exists()branch additionally treated auto-int array keys as alias matches, sohasRole(0, [0 => 'admin'])was also a false positive.The fix coerces both sides to string and compares strictly. Numeric-string / int equivalence (the common DB-ints-vs-Configure-strings case) is preserved on purpose; only string keys count as alias matches; non-scalar inputs return
falseearly.Tests
AuthenticationComponentTest::testIsPublicDoesNotMatchPluginRuleForNonPluginRequestandtestIsPublicDoesNotMatchPrefixRuleForNonPrefixRequestlock down the symmetric scope guard.AuthUserComponentTest::testHasRoleRejectsLooseTypeJugglingMatchescovershasRole(0, ['admin']),hasRole('1abc', [1, 2, 3]), null/bool inputs, and confirms numeric-string / int equivalence still works.phpunit,composer stan, andphpcs src/ tests/are all clean locally.