Skip to content

Commit

Permalink
modules API: Add test for ACL check of empty prefix (redis#13678)
Browse files Browse the repository at this point in the history
- Add empty string test for the new API
`RedisModule_ACLCheckKeyPrefixPermissions`.
- Fix order of checks: `(pattern[patternLen - 1] != '*' || patternLen ==
0)`

---------

Co-authored-by: debing.sun <[email protected]>
  • Loading branch information
moticless and sundb authored Dec 10, 2024
1 parent 0dd0572 commit c51c966
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ int prefixmatch(const char *pattern, int patternLen,
* it can match any suffix of the string beyond the prefix. This check
* remains outside stringmatchlen_impl() to keep its complexity manageable.
*/
if (pattern[patternLen - 1] != '*' || patternLen == 0)
if (patternLen == 0 || pattern[patternLen - 1] != '*' )
return 0;

/* Count backward the number of consecutive backslashes preceding the '*'
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/moduleapi/aclcheck.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,20 @@ start_server {tags {"modules acl"}} {
catch {r aclcheck.set.check.prefixkey "~" ESCAPED_STAR* ESCAPED_STAR* 5} e
assert_match "*DENIED KEY*" $e
assert_equal [r aclcheck.set.check.prefixkey "~" NON_ESCAPED_STAR\\ NON_ESCAPED_STAR\\clothes 5] OK
}
}

test {check ACL permissions versus empty string prefix} {
# The empty string should should match all keys permissions
r acl setuser default +set resetkeys %R~* %W~* ~*
assert_equal [r aclcheck.set.check.prefixkey "~" "" CART_BOOKS_12 5] OK
assert_equal [r aclcheck.set.check.prefixkey "W" "" ORDER_2024_564879 5] OK
assert_equal [r aclcheck.set.check.prefixkey "R" "" PRODUCT_BOOKS_753376 5] OK

# The empty string prefix should not match if cannot access all keys
r acl setuser default +set resetkeys %R~x* %W~x* ~x*
catch {r aclcheck.set.check.prefixkey "~" "" CART_BOOKS_12 5} e
assert_match "*DENIED KEY*" $e
}

test {test module check acl for key perm} {
# give permission for SET and block all keys but x(READ+WRITE), y(WRITE), z(READ)
Expand Down

0 comments on commit c51c966

Please sign in to comment.