From 5a5d7d11eec05877768a7200186e2996463eddc3 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Fri, 25 Sep 2026 12:49:12 +0200 Subject: [PATCH] feat(sast): hard-coded credentials in #define macros (#374) The C credential rule only matched variable initialisers, so the usual `#define WIFI_PASS "..."` in a config header went unnoticed. A regex alternative now covers #define lines in C and C++, PASS counts as a credential name, and placeholders (changeme, YOUR_..., xxx) are ignored. The nightly C fixture gains a config header with one real and one placeholder credential. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p --- compliance-agent/rules/cra_semgrep.yaml | 8 ++++++-- fixtures/nightly/baselines/fw-c-sensor-node.json | 3 ++- .../firmware/c-sensor-node/Core/Inc/net_config.h | 10 ++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 fixtures/nightly/firmware/c-sensor-node/Core/Inc/net_config.h diff --git a/compliance-agent/rules/cra_semgrep.yaml b/compliance-agent/rules/cra_semgrep.yaml index 56ed5fa..f96f949 100644 --- a/compliance-agent/rules/cra_semgrep.yaml +++ b/compliance-agent/rules/cra_semgrep.yaml @@ -188,12 +188,16 @@ rules: - pattern: $TYPE *$NAME = "$VALUE"; - pattern: $TYPE $NAME[] = "$VALUE"; - pattern: $TYPE $NAME[$N] = "$VALUE"; + # The usual embedded style: a macro in a config header + # (`#define WIFI_PASS "..."`), which semgrep's C patterns do not see. + - pattern-regex: '(?m)^[ \t]*#[ \t]*define[ \t]+(?P\w+)[ \t]+"(?P[^"]*)"' - metavariable-regex: metavariable: $NAME - regex: (?i).*(passw|passwd|pwd|secret|api_?key|token|psk|credential).* + regex: (?i).*((^|_)pass($|_)|passw|pwd|secret|api_?key|token|psk|credential).* + # Six characters or more, and not a placeholder left for the installer. - metavariable-regex: metavariable: $VALUE - regex: .{6,} + regex: ^(?!(?i:changeme|change_me|password|secret|admin|x{3,}|your_\w*|<[^>]*>|\*+)$).{6,}$ - id: cra-ai-13-c-broken-hash-or-cipher languages: [c, cpp] diff --git a/fixtures/nightly/baselines/fw-c-sensor-node.json b/fixtures/nightly/baselines/fw-c-sensor-node.json index 252acaa..788a0a5 100644 --- a/fixtures/nightly/baselines/fw-c-sensor-node.json +++ b/fixtures/nightly/baselines/fw-c-sensor-node.json @@ -19,7 +19,8 @@ "semgrep:cra-ai-20-c-unbounded-string-function comms.c:25", "semgrep:cra-ai-8-c-hardcoded-credential cli.cpp:8", "semgrep:cra-ai-8-c-hardcoded-credential comms.c:10", - "semgrep:cra-ai-8-c-hardcoded-credential comms.c:9" + "semgrep:cra-ai-8-c-hardcoded-credential comms.c:9", + "semgrep:cra-ai-8-c-hardcoded-credential net_config.h:7" ], "cves": [], "registry_rules": [] diff --git a/fixtures/nightly/firmware/c-sensor-node/Core/Inc/net_config.h b/fixtures/nightly/firmware/c-sensor-node/Core/Inc/net_config.h new file mode 100644 index 0000000..5abc2bf --- /dev/null +++ b/fixtures/nightly/firmware/c-sensor-node/Core/Inc/net_config.h @@ -0,0 +1,10 @@ +/* Network settings for the sensor node, as they are often shipped: in a + * header. Planted for the nightly: one real credential, one placeholder. */ +#ifndef NET_CONFIG_H +#define NET_CONFIG_H + +#define MQTT_HOST "broker.example.com" +#define MQTT_PASSWORD "sensor-Mq77!" +#define OTA_TOKEN "changeme" + +#endif -- 2.54.1