Skip to content

Commit 0ab0f13

Browse files
committed
Match suffixes and prefixes in string constraints
1 parent 0027980 commit 0ab0f13

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

docs/reference/configuration.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ policy:
391391
392392
# Regular expressions that match allowed emails
393393
regexes: ["@example\\.com$"]
394+
# Suffixes that match allowed emails
395+
suffixes: ["@example.com"]
394396

395397
# If specified, the email address *must not* match one of the banned addresses.
396398
# If unspecified, all email addresses are allowed.
@@ -401,6 +403,10 @@ policy:
401403
substrings: ["evil"]
402404
# Regular expressions that match banned emails
403405
regexes: ["@evil\\.corp$"]
406+
# Suffixes that match banned emails
407+
suffixes: ["@evil.corp"]
408+
# Prefixes that match banned emails
409+
prefixes: ["alice@"]
404410

405411
requester:
406412
# List of IP addresses and CIDRs that are not allowed to register
@@ -414,6 +420,8 @@ policy:
414420
literals: ["Pretend this is Real;"]
415421
substrings: ["Chrome"]
416422
regexes: ["Chrome 1.*;"]
423+
prefixes: ["Mozilla/"]
424+
suffixes: ["Safari/605.1.15"]
417425
```
418426
419427
## `rate_limiting`

policies/common/common.rego

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ matches_string_constraints(str, constraints) if matches_substrings(str, constrai
88

99
matches_string_constraints(str, constraints) if matches_literals(str, constraints.literals)
1010

11+
matches_string_constraints(str, constraints) if matches_suffixes(str, constraints.suffixes)
12+
13+
matches_string_constraints(str, constraints) if matches_prefixes(str, constraints.prefixes)
14+
1115
matches_regexes(str, regexes) if {
1216
some pattern in regexes
1317
regex.match(pattern, str)
@@ -23,6 +27,16 @@ matches_literals(str, literals) if {
2327
str == literal
2428
}
2529

30+
matches_suffixes(str, suffixes) if {
31+
some suffix in suffixes
32+
endswith(str, suffix)
33+
}
34+
35+
matches_prefixes(str, prefixes) if {
36+
some prefix in prefixes
37+
startswith(str, prefix)
38+
}
39+
2640
# Normalize an IP address or CIDR to a CIDR
2741
normalize_cidr(ip) := ip if contains(ip, "/")
2842

policies/common/common_test.rego

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ test_match_regex if {
1818
not common.matches_string_constraints("some string", {"regexes": ["^string"]})
1919
}
2020

21+
test_match_prefix if {
22+
common.matches_string_constraints("some string", {"prefixes": ["some"]})
23+
not common.matches_string_constraints("some string", {"prefixes": ["string"]})
24+
}
25+
26+
test_match_suffix if {
27+
common.matches_string_constraints("some string", {"suffixes": ["string"]})
28+
not common.matches_string_constraints("some string", {"suffixes": ["some"]})
29+
}
30+
2131
test_ip_in_list if {
2232
common.ip_in_list("192.168.1.1", ["192.168.1.1"])
2333
common.ip_in_list("192.168.1.1", ["192.168.1.0/24"])

0 commit comments

Comments
 (0)