from codepathfinder.container_decorators import compose_rule
from codepathfinder.container_matchers import service_has, service_missing
from codepathfinder.container_combinators import all_of, any_of, none_of # optionalDeclares a rule scoped to docker-compose*.yml files. Automatically sets file_pattern="**/docker-compose*.yml".
@compose_rule(
id="COMPOSE-SEC-001", # required
name="",
severity="MEDIUM", # CRITICAL | HIGH | MEDIUM | LOW | INFO
category="security",
cwe="",
cve="",
tags="",
message="",
)
def my_rule():
return service_has(key="privileged", equals=True)@dockerfile_rule, @compose_rule does not accept an owasp parameter.Matches services with a specific property. All constraint parameters are optional; pass only the ones you need.
from codepathfinder.container_matchers import service_has
# Privileged mode (container escape)
service_has(key="privileged", equals=True)
# Docker socket mounted inside container
service_has(key="volumes", contains="/var/run/docker.sock")
# Network mode = host (bypasses network isolation)
service_has(key="network_mode", equals="host")
# Dangerous Linux capabilities
service_has(
key="cap_add",
contains_any=["SYS_ADMIN", "NET_ADMIN", "SYS_PTRACE", "ALL"],
)
# Environment variable with secret-ish name
service_has(key="environment", env_name_regex=r"(?i).*(password|secret|token).*")
# Port published to host below a threshold (privileged ports)
service_has(key="ports", published_port_less_than=1024)keyrequiredequalsnot_equalscontainsnot_containscontains_anyregexenv_name_regexenv_value_regexvolume_typesource_regextarget_regexpublished_port_less_thanMatches services that do NOT have a specific property. Ideal for enforcing security defaults like read_only or security_opt: no-new-privileges.
from codepathfinder.container_matchers import service_missing
service_missing(key="read_only") # filesystem isn't read-only
service_missing(key="security_opt") # no security_opt at all
service_missing(key="security_opt", value_contains="no-new-privileges") # present but missing this flagkeyrequiredvalue_containsSame combinators as Dockerfile rules.
from codepathfinder.container_combinators import all_of, any_of, none_of
# Service is privileged AND mounts docker.sock
all_of(
service_has(key="privileged", equals=True),
service_has(key="volumes", contains="/var/run/docker.sock"),
)
# Any dangerous network setting
any_of(
service_has(key="network_mode", equals="host"),
service_has(key="pid", equals="host"),
service_has(key="ipc", equals="host"),
)Real rule from the registry (COMPOSE-SEC-008). Flags services that add capabilities enabling container escape.
from codepathfinder.container_decorators import compose_rule
from codepathfinder.container_matchers import service_has
@compose_rule(
id="COMPOSE-SEC-008",
name="Dangerous Capability Added",
severity="HIGH",
cwe="CWE-250",
category="security",
tags="docker-compose,compose,capabilities,cap-add,security,privilege-escalation,container-escape,linux,kernel",
message=(
"Service adds dangerous capability. These capabilities can be used for container escape "
"or privilege escalation."
)
)
def dangerous_capabilities():
"""Capabilities like SYS_ADMIN or SYS_MODULE provide near-root powers."""
return service_has(
key="cap_add",
contains_any=[
"SYS_ADMIN",
"NET_ADMIN",
"SYS_PTRACE",
"SYS_MODULE",
"DAC_READ_SEARCH",
"ALL",
],
)