Skip to content

Commit c1fd2ce

Browse files
committed
chore: add tests
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent d2f8e2a commit c1fd2ce

File tree

7 files changed

+63
-42
lines changed

7 files changed

+63
-42
lines changed

docs/opa-middleware.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@ Example:
4646

4747
## Comprehensive API Path Protection
4848

49-
When writing Rego policies, it's crucial to implement thorough path matching to prevent unintended access to APIs. The daemon processes API paths without strict prefix validation, which could lead to security bypasses.
49+
When writing Rego policies, use pattern matching for API paths to prevent unauthorized access. Simple string matching can be bypassed by adding prefixes to API paths.
50+
51+
Consider this potentially vulnerable policy that tries to restrict access to a specific container:
52+
```
53+
# INCORRECT: Can be bypassed
54+
allow if {
55+
not (input.Path == "/v1.43/containers/sensitive-container/json")
56+
}
57+
```
58+
This policy can be bypassed in multiple ways:
59+
1. Using container ID instead of name: `/v1.43/containers/abc123.../json`
60+
2. Adding path prefixes: `/custom/v1.43/containers/sensitive-container/json`
61+
62+
Follow the path matching best practices below to properly secure your resources.
5063

5164
## Path Matching Best Practices
5265

docs/sample-rego-policies/case1-incompatible_API.rego

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ is_swarm_api if {
1818
input.Method == "GET"
1919
glob.match("/**/swarm", ["/"], input.Path)
2020
}
21+
22+
is_forbidden_container if {
23+
input.Method == "GET"
24+
glob.match("/**/containers/1f576a797a486438548377124f6cb7770a5cb7c8ff6a11c069cb4128d3f59462/top", ["/"], input.Path)
25+
}
26+
27+
is_missing_container if {
28+
input.Method == "GET"
29+
glob.match("/**/containers/1f576a797a486438548377124f6cb7770a5cb7c8ff6a11c069cb4128d3f59462/json", ["/"], input.Path)
30+
}

docs/sample-rego-policies/default.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ is_plugins if {
3333
}
3434

3535
is_forbidden_container if {
36-
intpu.Method == "GET"
36+
input.Method == "GET"
3737
glob.match("/**/container/1f576a797a486438548377124f6cb7770a5cb7c8ff6a11c069cb4128d3f59462/json", ["/"], input.Path)
3838
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package finch.authz
2+
3+
default allow = false
4+
5+
invalid syntax

docs/sample-rego-policies/test.rego

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package finch.authz
2+
3+
import future.keywords.if
4+
import rego.v1
5+
6+
default allow = false
7+
8+
allow if {
9+
not is_networs_api
10+
not is_swarm_api
11+
not is_inspect_by_name
12+
}
13+
14+
is_container_create if {
15+
input.Method == "POST"
16+
glob.match("/**/containers/create", ["/"], input.Path)
17+
}
18+
19+
is_networs_api if {
20+
input.Method == "GET"
21+
glob.match("/**/networks", ["/"], input.Path)
22+
}
23+
24+
is_swarm_api if {
25+
input.Method == "GET"
26+
glob.match("/**/swarm", ["/"], input.Path)
27+
}
28+
29+
30+
is_inspect_by_name if {
31+
input.Method == "GET"
32+
glob.match("/**/containers/test-container/json", ["/"], input.Path)
33+
}

e2e/e2e_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func TestRun(t *testing.T) {
2828
} else {
2929
t.Skip("E2E tests skipped. Set TEST_E2E=1 to run regular E2E tests or MIDDLEWARE_E2E=1 to run OPA middleware tests")
3030
}
31-
opt, _ := option.New([]string{*Subject, "--namespace", "finch"})
3231
}
3332

3433
func runOPATests(t *testing.T) {

0 commit comments

Comments
 (0)