-
Notifications
You must be signed in to change notification settings - Fork 40
feat: add check to detect URL annotations with suspicious characters #399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nikpivkin
wants to merge
2
commits into
aquasecurity:main
Choose a base branch
from
nikpivkin:annot-url
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
Annotations containing URLs with suspicious or non-standard characters may indicate attempts to obfuscate malicious behavior or bypass security controls. | ||
|
||
|
||
### Impact | ||
<!-- Add Impact here --> | ||
|
||
<!-- DO NOT CHANGE --> | ||
{{ remediationActions }} | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# METADATA | ||
# title: URL annotation contains suspicious characters | ||
# description: | | ||
# Annotations containing URLs with suspicious or non-standard characters may indicate attempts to obfuscate malicious behavior or bypass security controls. | ||
# scope: package | ||
# schemas: | ||
# - input: schema["kubernetes"] | ||
# custom: | ||
# id: KCV0094 | ||
# avd_id: AVD-KCV-0094 | ||
# severity: CRITICAL | ||
# short_code: suspicious-url-annotation | ||
# recommended_action: Review and sanitize URL annotations to remove suspicious characters. | ||
# input: | ||
# selector: | ||
# - type: kubernetes | ||
package builtin.kubernetes.kcv0094 | ||
|
||
import rego.v1 | ||
|
||
annotations_to_check := {"link.argocd.argoproj.io/external-link"} # https://argo-cd.readthedocs.io/en/release-3.0/user-guide/external-url/ | ||
|
||
regexp := "https?://[^\\s<>\"{}|^`\\\\\\r\\n]*[\\r\\n<>\"{}|^`\\\\][^\\s]*" | ||
|
||
deny contains res if { | ||
some key in annotations_to_check | ||
annot := input.metadata.annotations[key] | ||
regex.match(regexp, annot) | ||
res := result.new( | ||
sprintf("Annotation '%s' contains suspicious characters", [key]), | ||
input.metadata, | ||
) | ||
} | ||
|
||
# https://kubernetes-sigs.github.io/aws-load-balancer-controller/v2.2/guide/ingress/annotations/#authentication | ||
alb_oidc_annot_key := "alb.ingress.kubernetes.io/auth-idp-oidc" | ||
|
||
deny contains res if { | ||
annot := input.metadata.annotations[alb_oidc_annot_key] | ||
d := json.unmarshal(substring(annot, 1, count(annot) - 2)) | ||
some k | ||
lower(k) in {"issuer", "authorizationendpoint", "tokenendpoint", "userinfoendpoint"} | ||
regex.match(regexp, d[k]) | ||
res := result.new( | ||
sprintf("Annotation '%s' contains suspicious characters", [alb_oidc_annot_key]), | ||
input.metadata, | ||
) | ||
} |
41 changes: 41 additions & 0 deletions
41
checks/kubernetes/network/suspicious_url_annotation_test.rego
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package builtin.kubernetes.kcv0094_test | ||
|
||
import data.builtin.kubernetes.kcv0094 as check | ||
|
||
import rego.v1 | ||
|
||
test_check[name] if { | ||
some name, tc in { | ||
"valid url": { | ||
"key": "link.argocd.argoproj.io/external-link", | ||
"value": "https://valid-url.com", | ||
"expected": 0, | ||
}, | ||
"suspicious characters": { | ||
"key": "link.argocd.argoproj.io/external-link", | ||
"value": "http://example.com/#;\ninjection_point", | ||
"expected": 1, | ||
}, | ||
"unsupported annotation": { | ||
"key": "foo", | ||
"value": "http://example.com/#;\ninjection_point", | ||
"expected": 0, | ||
}, | ||
"alb oidc": { | ||
"key": "alb.ingress.kubernetes.io/auth-idp-oidc", | ||
"value": `'{"issuer":"https://example.com","authorizationEndpoint":"http://example.com/#;\ninjection_point","tokenEndpoint":"https://token.example.com","userInfoEndpoint":"https://userinfo.example.com","secretName":"my-k8s-secret"}'`, | ||
"expected": 1, | ||
}, | ||
} | ||
|
||
r := check.deny with input as { | ||
"apiVersion": "v1", | ||
"kind": "test", | ||
"metadata": { | ||
"namespace": "default", | ||
"annotations": {tc.key: tc.value}, | ||
}, | ||
} | ||
|
||
count(r) == tc.expected | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: since we don't have a way to definitively prove an annotation key would have a value of a URL, we are relying here for two specific keys:
"alb.ingress.kubernetes.io/auth-idp-oidc"
which is part of the kubernetes well known URL annotations"link.argocd.argoproj.io/external-link"
a commonly used annotation in argoCD.determining if a key will have a value of a URL is a hard problem. therefore we've started with well-known annotations for now.
WDYT @itaysk?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that is fine. we could also search for regexp matches across all annotations but I imagine the concern was for performance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance certainly yes but there could be annotations where a URL is expected as a value but the key may not make it obvious as such.