trivy icon indicating copy to clipboard operation
trivy copied to clipboard

feat(checks): Add checks to detect suspicious Kubernetes URL annotations

Open simar7 opened this issue 9 months ago • 10 comments

Annotations in Kubernetes are widely used across many different resources. As seen in https://github.com/aquasecurity/trivy-checks/pull/374 they can be misused.

We can write a check that checks all resources and their annotations for suspicious and unexpected values. As a part of this check, we can also improve the regex (or take a different approach) as was done here.

simar7 avatar Apr 03 '25 01:04 simar7

@simar7 Any examples of what suspicious annotations might look like?

nikpivkin avatar Apr 03 '25 06:04 nikpivkin

@simar7 Any examples of what suspicious annotations might look like?

@nikpivkin I actually missed an important detail, we should start off with annotations like auth_url or any other url* type annotation first as they're straightforward to define being URLs. If they are no URLs we should flag them.

So something like [-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*) taken off of here.

simar7 avatar Apr 04 '25 01:04 simar7

The ingress-nginx repository uses a fairly simple regular expression.

nikpivkin avatar Apr 04 '25 05:04 nikpivkin

Can we add checks for all annotations as the issue says? I have seen that some annotations allow characters that might be considered suspicious in some cases, such as line breaks.

nikpivkin avatar Apr 04 '25 05:04 nikpivkin

Can we add checks for all annotations as the issue says? I have seen that some annotations allow characters that might be considered suspicious in some cases, such as line breaks.

Sure, but what else would you consider suspicious? With URLs it's easy to determine but with plain text annotation fields, detection of something like injection comes to mind.

simar7 avatar Apr 05 '25 03:04 simar7

The ingress-nginx repository uses a fairly simple regular expression.

Yes I saw it, I'm not sure why it's this simplified. Then again it maybe enough to cover all cases.

simar7 avatar Apr 05 '25 03:04 simar7

Any examples of what suspicious annotations might look like?

This isn't about suspecious values, but about malformed values. Annotation values are always strings but in some cases expect a certain type of value. for example, the auth_url annotation clearly expects a url, but there's no validation, so the user can (intentionally or accidentally) set it to an invalid url. we've added a few validations based on the annotations used in ingress-nginx, but it's a good idea to validate any label that expects a specific type.

itaysk avatar Apr 05 '25 14:04 itaysk

good idea to validate any label that expects a specific type.

How can we know the type for any label? Except if we create a list of such annotations ourselves.

nikpivkin avatar Apr 09 '25 06:04 nikpivkin

good idea to validate any label that expects a specific type.

How can we know the type for any label? Except if we create a list of such annotations ourselves.

I think we can start with any annotation that has a key where a URL is the expected value.

simar7 avatar Apr 09 '25 19:04 simar7

good idea to validate any label that expects a specific type.

How can we know the type for any label? Except if we create a list of such annotations ourselves.

I think we can start with any annotation that has a key where a URL is the expected value.

@nikpivkin there's also this list which we could build checks against https://kubernetes.io/docs/reference/labels-annotations-taints/

As for other URLs that aren't well-known according to that list, we could do a basic regex grab for something that has "url" in the key of the annotation. This may lead to false positives but I don't have a better solution at this time.

simar7 avatar Apr 14 '25 22:04 simar7