From 75e179a478d7070dc179e6c5db3c18234eb3e3aa Mon Sep 17 00:00:00 2001 From: don-petry <36422719+don-petry@users.noreply.github.com> Date: Tue, 1 Sep 2026 12:09:56 +0000 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20implement=20issue=20#533=20?= =?UTF-8?q?=E2=80=94=20[Fleet=20Monitor]=20petry-projects/broodly=20?= =?UTF-8?q?=E2=80=94=20.github/workflows/sonarcloud.yml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 4 ++ tests/scripts/sonar-config.test.sh | 106 +++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100755 tests/scripts/sonar-config.test.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a2413862..48be33e5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -184,6 +184,10 @@ jobs: run: bash tests/scripts/workflow-integrity.test.sh - name: Workflow manifest validation run: bash scripts/validate-workflows.sh + - name: SonarCloud config guard + # Rejects '*'/'**' wildcards in sonar.sources / sonar.tests, which + # SonarCloud fails deterministically on (Fleet Monitor #533). + run: bash tests/scripts/sonar-config.test.sh - name: Workflow validator test run: bash tests/scripts/validate-workflows.test.sh - name: Dependabot auto-merge contract test diff --git a/tests/scripts/sonar-config.test.sh b/tests/scripts/sonar-config.test.sh new file mode 100755 index 00000000..1c132c0a --- /dev/null +++ b/tests/scripts/sonar-config.test.sh @@ -0,0 +1,106 @@ +#!/usr/bin/env bash +# Test: sonar-project.properties never uses wildcards in sonar.sources / sonar.tests. +# +# Why this exists: Fleet Monitor issue #533 flagged .github/workflows/sonarcloud.yml +# with a high failure rate. The failing run's SonarCloud scan failed deterministically +# on every retry attempt with: +# ERROR Invalid value of sonar.tests for petry-projects_broodly +# ERROR Wildcards ** and * are not supported in "sonar.sources" and "sonar.tests" +# properties. +# SonarCloud forbids '*' / '**' wildcards in sonar.sources and sonar.tests (they are a +# comma-separated list of directories). Wildcards belong only in the *.exclusions, +# *.inclusions, and *.test.* filter properties. A wildcard in either sonar.sources or +# sonar.tests makes the scanner exit 3 on every run, and the sonarcloud.yml retry loop +# cannot recover a deterministic config error — it just fails the job (and inflates +# duration). This guard fails CI fast so such a config can never merge to main. +# +# Run: bash tests/scripts/sonar-config.test.sh + +set -euo pipefail + +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +PROPERTIES_FILE="${REPO_ROOT}/sonar-project.properties" + +fail=0 +pass_count=0 + +# validate_sonar_properties +# Prints a human-readable reason and returns non-zero when the file sets a +# sonar.sources or sonar.tests value containing a '*' wildcard. +validate_sonar_properties() { + local path="$1" + if [[ ! -f "$path" ]]; then + echo "file not found: ${path}" + return 1 + fi + + local reason="" + local key + for key in "sonar.sources" "sonar.tests"; do + # Extract the raw value for the key (last assignment wins, matching the + # java.util.Properties semantics the scanner uses). Ignore commented lines. + local value + value="$(grep -E "^[[:space:]]*${key//./\\.}[[:space:]]*=" "$path" 2>/dev/null \ + | grep -vE "^[[:space:]]*#" \ + | tail -n1 \ + | sed -E "s/^[[:space:]]*${key//./\\.}[[:space:]]*=[[:space:]]*//")" || true + if [[ "$value" == *"*"* ]]; then + reason="${key} contains an unsupported wildcard: '${value}'" + break + fi + done + + [[ -z "$reason" ]] || { echo "$reason"; return 1; } +} + +# --- Negative assertions: wildcard sonar.sources / sonar.tests are rejected. --- +# These mirror the exact corruption behind issue #533 and prove the validator +# does not pass trivially. +_fixture_dir="$(mktemp -d)" +trap '[[ -n "${_fixture_dir:-}" ]] && rm -rf "$_fixture_dir"' EXIT + +_bad_tests="${_fixture_dir}/bad-tests.properties" +cat > "$_bad_tests" <<'EOF' +sonar.projectKey=petry-projects_broodly +sonar.sources=. +sonar.tests=apps/**/__tests__ +EOF + +! validate_sonar_properties "$_bad_tests" >/dev/null 2>&1 \ + && { echo "ok - validator rejects a wildcard sonar.tests"; pass_count=$((pass_count + 1)); } \ + || { echo "not ok - validator accepted a wildcard sonar.tests"; fail=1; } + +_bad_sources="${_fixture_dir}/bad-sources.properties" +cat > "$_bad_sources" <<'EOF' +sonar.projectKey=petry-projects_broodly +sonar.sources=apps/* +EOF + +! validate_sonar_properties "$_bad_sources" >/dev/null 2>&1 \ + && { echo "ok - validator rejects a wildcard sonar.sources"; pass_count=$((pass_count + 1)); } \ + || { echo "not ok - validator accepted a wildcard sonar.sources"; fail=1; } + +# --- Negative control: wildcards in sonar.exclusions are allowed. -------------- +# SonarCloud permits wildcards in the filter properties; the guard must not flag +# them, or it would reject the repo's own valid config. +_ok_exclusions="${_fixture_dir}/ok-exclusions.properties" +cat > "$_ok_exclusions" <<'EOF' +sonar.projectKey=petry-projects_broodly +sonar.sources=. +sonar.exclusions=_bmad/**,.github/** +EOF + +validate_sonar_properties "$_ok_exclusions" >/dev/null 2>&1 \ + && { echo "ok - validator allows wildcards in sonar.exclusions"; pass_count=$((pass_count + 1)); } \ + || { echo "not ok - validator wrongly rejected wildcards in sonar.exclusions"; fail=1; } + +# --- Positive assertion: the repo's real properties file is valid. ------------- +reason="$(validate_sonar_properties "$PROPERTIES_FILE")" \ + && { echo "ok - sonar-project.properties has no wildcard sonar.sources/sonar.tests"; pass_count=$((pass_count + 1)); } \ + || { echo "not ok - sonar-project.properties is invalid: ${reason}"; fail=1; } + +echo "---" +[[ "$fail" -eq 0 ]] \ + && echo "PASS — ${pass_count} assertion(s) passed" \ + || echo "FAIL — see assertions above" +exit "$fail" From 2ada06b614f260cf7aa215ee79b3de34da59f884 Mon Sep 17 00:00:00 2001 From: don-petry <36422719+don-petry@users.noreply.github.com> Date: Tue, 1 Sep 2026 12:13:53 +0000 Subject: [PATCH 2/4] fix(bot): address bot feedback [skip ci-relay] --- tests/scripts/sonar-config.test.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/scripts/sonar-config.test.sh b/tests/scripts/sonar-config.test.sh index 1c132c0a..ec338ddf 100755 --- a/tests/scripts/sonar-config.test.sh +++ b/tests/scripts/sonar-config.test.sh @@ -40,10 +40,10 @@ validate_sonar_properties() { # Extract the raw value for the key (last assignment wins, matching the # java.util.Properties semantics the scanner uses). Ignore commented lines. local value - value="$(grep -E "^[[:space:]]*${key//./\\.}[[:space:]]*=" "$path" 2>/dev/null \ + value="$(grep -E "^[[:space:]]*${key//./\\.}[[:space:]]*[:=][[:space:]]*" "$path" 2>/dev/null \ | grep -vE "^[[:space:]]*#" \ | tail -n1 \ - | sed -E "s/^[[:space:]]*${key//./\\.}[[:space:]]*=[[:space:]]*//")" || true + | sed -E "s/^[[:space:]]*${key//./\\.}[[:space:]]*[:=][[:space:]]*//")" || true if [[ "$value" == *"*"* ]]; then reason="${key} contains an unsupported wildcard: '${value}'" break @@ -56,7 +56,7 @@ validate_sonar_properties() { # --- Negative assertions: wildcard sonar.sources / sonar.tests are rejected. --- # These mirror the exact corruption behind issue #533 and prove the validator # does not pass trivially. -_fixture_dir="$(mktemp -d)" +_fixture_dir="$(mktemp -d)" || { echo "Failed to create temporary directory" >&2; exit 1; } trap '[[ -n "${_fixture_dir:-}" ]] && rm -rf "$_fixture_dir"' EXIT _bad_tests="${_fixture_dir}/bad-tests.properties" From 1deb68267faf4bf8ffcfb405581aea4931caef63 Mon Sep 17 00:00:00 2001 From: don-petry <36422719+don-petry@users.noreply.github.com> Date: Tue, 1 Sep 2026 12:19:30 +0000 Subject: [PATCH 3/4] fix(reviews): address review comments [skip ci-relay] --- tests/scripts/sonar-config.test.sh | 100 ++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 8 deletions(-) diff --git a/tests/scripts/sonar-config.test.sh b/tests/scripts/sonar-config.test.sh index ec338ddf..f4db94bc 100755 --- a/tests/scripts/sonar-config.test.sh +++ b/tests/scripts/sonar-config.test.sh @@ -24,6 +24,59 @@ PROPERTIES_FILE="${REPO_ROOT}/sonar-project.properties" fail=0 pass_count=0 +# _sonar_property_value +# Extracts the logical value for from a java.util.Properties file exactly +# as SonarScanner's parser would. Honors all java.util.Properties forms: '=', +# ':' and whitespace key/value separators, '#'/'!' comment lines, and backslash +# line-continuations (a physical line ending in an odd number of backslashes is +# joined with the next, whose leading whitespace is discarded). Last assignment +# wins. Prints the raw value ('' if the key is absent). Parsing the logical +# property first is what stops a wildcard hidden in a colon/whitespace/continued +# form from bypassing the guard. +_sonar_property_value() { + local path="$1" key="$2" + awk -v target="$key" ' + function trailing_bs(s, i, c) { + c = 0; i = length(s) + while (i >= 1 && substr(s, i, 1) == "\\") { c++; i-- } + return c + } + function is_ws(ch) { return (ch == " " || ch == "\t" || ch == "\f") } + function process(line, i, ch, k, v, n) { + sub(/^[ \t\f]+/, "", line) + if (line == "" || substr(line, 1, 1) == "#" || substr(line, 1, 1) == "!") return + n = length(line); i = 1; k = "" + while (i <= n) { + ch = substr(line, i, 1) + if (ch == "\\") { k = k substr(line, i + 1, 1); i += 2; continue } + if (ch == "=" || ch == ":" || is_ws(ch)) break + k = k ch; i++ + } + while (i <= n && is_ws(substr(line, i, 1))) i++ + if (i <= n && (substr(line, i, 1) == "=" || substr(line, i, 1) == ":")) { + i++ + while (i <= n && is_ws(substr(line, i, 1))) i++ + } + v = substr(line, i) + if (k == target) value = v + } + BEGIN { buf = ""; cont = 0; value = "" } + { + line = $0 + if (cont) sub(/^[ \t\f]+/, "", line) + if (trailing_bs(line) % 2 == 1) { + buf = buf substr(line, 1, length(line) - 1) + cont = 1 + next + } + buf = buf line + process(buf) + buf = ""; cont = 0 + } + END { if (buf != "") process(buf); printf "%s", value } + ' "$path" +} + # validate_sonar_properties # Prints a human-readable reason and returns non-zero when the file sets a # sonar.sources or sonar.tests value containing a '*' wildcard. @@ -35,15 +88,11 @@ validate_sonar_properties() { fi local reason="" - local key + local key value for key in "sonar.sources" "sonar.tests"; do - # Extract the raw value for the key (last assignment wins, matching the - # java.util.Properties semantics the scanner uses). Ignore commented lines. - local value - value="$(grep -E "^[[:space:]]*${key//./\\.}[[:space:]]*[:=][[:space:]]*" "$path" 2>/dev/null \ - | grep -vE "^[[:space:]]*#" \ - | tail -n1 \ - | sed -E "s/^[[:space:]]*${key//./\\.}[[:space:]]*[:=][[:space:]]*//")" || true + # Resolve the logical property value (all java.util.Properties separator and + # continuation forms, last-assignment-wins) before checking for a wildcard. + value="$(_sonar_property_value "$path" "$key")" if [[ "$value" == *"*"* ]]; then reason="${key} contains an unsupported wildcard: '${value}'" break @@ -80,6 +129,41 @@ EOF && { echo "ok - validator rejects a wildcard sonar.sources"; pass_count=$((pass_count + 1)); } \ || { echo "not ok - validator accepted a wildcard sonar.sources"; fail=1; } +# --- Negative assertions: alternate java.util.Properties forms are covered. ---- +# A wildcard must be caught regardless of the separator (':' or whitespace) or +# whether the value is spread across backslash-continuation lines — these are the +# forms a naive single-line '=' match would let slip past the guard. +_bad_colon="${_fixture_dir}/bad-colon.properties" +cat > "$_bad_colon" <<'EOF' +sonar.projectKey=petry-projects_broodly +sonar.tests:apps/**/__tests__ +EOF + +! validate_sonar_properties "$_bad_colon" >/dev/null 2>&1 \ + && { echo "ok - validator rejects a wildcard sonar.tests with ':' separator"; pass_count=$((pass_count + 1)); } \ + || { echo "not ok - validator accepted a wildcard sonar.tests with ':' separator"; fail=1; } + +_bad_ws="${_fixture_dir}/bad-whitespace.properties" +cat > "$_bad_ws" <<'EOF' +sonar.projectKey=petry-projects_broodly +sonar.sources apps/* +EOF + +! validate_sonar_properties "$_bad_ws" >/dev/null 2>&1 \ + && { echo "ok - validator rejects a wildcard sonar.sources with whitespace separator"; pass_count=$((pass_count + 1)); } \ + || { echo "not ok - validator accepted a wildcard sonar.sources with whitespace separator"; fail=1; } + +_bad_cont="${_fixture_dir}/bad-continuation.properties" +cat > "$_bad_cont" <<'EOF' +sonar.projectKey=petry-projects_broodly +sonar.tests=\ + apps/**/__tests__ +EOF + +! validate_sonar_properties "$_bad_cont" >/dev/null 2>&1 \ + && { echo "ok - validator rejects a wildcard sonar.tests across a line continuation"; pass_count=$((pass_count + 1)); } \ + || { echo "not ok - validator accepted a wildcard sonar.tests across a line continuation"; fail=1; } + # --- Negative control: wildcards in sonar.exclusions are allowed. -------------- # SonarCloud permits wildcards in the filter properties; the guard must not flag # them, or it would reject the repo's own valid config. From bf6a320f1068e86d6563cd2ed8d8ab433aca9de3 Mon Sep 17 00:00:00 2001 From: don-petry <36422719+don-petry@users.noreply.github.com> Date: Tue, 22 Sep 2026 00:02:10 +0000 Subject: [PATCH 4/4] fix(reviews): address review comments [skip ci-relay] --- tests/scripts/sonar-config.test.sh | 124 +++++++++++++++++++++++++---- 1 file changed, 108 insertions(+), 16 deletions(-) diff --git a/tests/scripts/sonar-config.test.sh b/tests/scripts/sonar-config.test.sh index f4db94bc..65782f56 100755 --- a/tests/scripts/sonar-config.test.sh +++ b/tests/scripts/sonar-config.test.sh @@ -27,12 +27,16 @@ pass_count=0 # _sonar_property_value # Extracts the logical value for from a java.util.Properties file exactly # as SonarScanner's parser would. Honors all java.util.Properties forms: '=', -# ':' and whitespace key/value separators, '#'/'!' comment lines, and backslash +# ':' and whitespace key/value separators, '#'/'!' comment lines, backslash # line-continuations (a physical line ending in an odd number of backslashes is -# joined with the next, whose leading whitespace is discarded). Last assignment -# wins. Prints the raw value ('' if the key is absent). Parsing the logical -# property first is what stops a wildcard hidden in a colon/whitespace/continued -# form from bypassing the guard. +# joined with the next, whose leading whitespace is discarded), and the escape +# sequences java.util.Properties decodes in keys and values (\t \n \r \f, an +# escaped separator/backslash, and \uXXXX Unicode escapes). Last assignment +# wins. Prints the decoded value ('' if the key is absent). Decoding \uXXXX is +# what stops an encoded wildcard such as sonar.tests=apps/* (which the +# scanner decodes to apps/*) from slipping past the guard. Exits non-zero on a +# malformed \uXXXX escape — java.util.Properties throws on such input, so the +# guard must reject it rather than let a half-decoded value pass. _sonar_property_value() { local path="$1" key="$2" awk -v target="$key" ' @@ -42,25 +46,57 @@ _sonar_property_value() { return c } function is_ws(ch) { return (ch == " " || ch == "\t" || ch == "\f") } - function process(line, i, ch, k, v, n) { + function hexval(ch, p) { + p = index("0123456789abcdef", tolower(ch)) + return p - 1 + } + # Decode java.util.Properties escapes in a logical key or value. Sets the + # global bad=1 (and returns "") on a malformed \uXXXX escape. + function decode(s, out, i, n, ch, nx, code, j, hv) { + out = ""; n = length(s); i = 1 + while (i <= n) { + ch = substr(s, i, 1) + if (ch != "\\") { out = out ch; i++; continue } + nx = substr(s, i + 1, 1) + if (nx == "") { i += 1; continue } + if (nx == "u") { + if (i + 5 > n) { bad = 1; return "" } + code = 0 + for (j = 0; j < 4; j++) { + hv = hexval(substr(s, i + 2 + j, 1)) + if (hv < 0) { bad = 1; return "" } + code = code * 16 + hv + } + out = out sprintf("%c", code); i += 6; continue + } + if (nx == "t") { out = out "\t"; i += 2; continue } + if (nx == "n") { out = out "\n"; i += 2; continue } + if (nx == "r") { out = out "\r"; i += 2; continue } + if (nx == "f") { out = out "\f"; i += 2; continue } + out = out nx; i += 2 + } + return out + } + # Split off the raw (still-escaped) key, honoring escaped separators, then + # decode key and value separately so \uXXXX in either is resolved. + function process(line, i, ch, rawk, n) { sub(/^[ \t\f]+/, "", line) if (line == "" || substr(line, 1, 1) == "#" || substr(line, 1, 1) == "!") return - n = length(line); i = 1; k = "" + n = length(line); i = 1; rawk = "" while (i <= n) { ch = substr(line, i, 1) - if (ch == "\\") { k = k substr(line, i + 1, 1); i += 2; continue } + if (ch == "\\") { rawk = rawk substr(line, i, 2); i += 2; continue } if (ch == "=" || ch == ":" || is_ws(ch)) break - k = k ch; i++ + rawk = rawk ch; i++ } while (i <= n && is_ws(substr(line, i, 1))) i++ if (i <= n && (substr(line, i, 1) == "=" || substr(line, i, 1) == ":")) { i++ while (i <= n && is_ws(substr(line, i, 1))) i++ } - v = substr(line, i) - if (k == target) value = v + if (decode(rawk) == target) value = decode(substr(line, i)) } - BEGIN { buf = ""; cont = 0; value = "" } + BEGIN { buf = ""; cont = 0; value = ""; bad = 0 } { line = $0 if (cont) sub(/^[ \t\f]+/, "", line) @@ -73,7 +109,7 @@ _sonar_property_value() { process(buf) buf = ""; cont = 0 } - END { if (buf != "") process(buf); printf "%s", value } + END { if (buf != "") process(buf); if (bad) exit 2; printf "%s", value } ' "$path" } @@ -90,9 +126,14 @@ validate_sonar_properties() { local reason="" local key value for key in "sonar.sources" "sonar.tests"; do - # Resolve the logical property value (all java.util.Properties separator and - # continuation forms, last-assignment-wins) before checking for a wildcard. - value="$(_sonar_property_value "$path" "$key")" + # Resolve the logical property value (all java.util.Properties separator, + # continuation and escape forms, last-assignment-wins) before checking for a + # wildcard. A non-zero exit means a malformed \uXXXX escape, which + # java.util.Properties would reject — so the guard rejects it too. + if ! value="$(_sonar_property_value "$path" "$key")"; then + reason="${key} has a malformed \\uXXXX escape (java.util.Properties would reject it)" + break + fi if [[ "$value" == *"*"* ]]; then reason="${key} contains an unsupported wildcard: '${value}'" break @@ -164,6 +205,57 @@ EOF && { echo "ok - validator rejects a wildcard sonar.tests across a line continuation"; pass_count=$((pass_count + 1)); } \ || { echo "not ok - validator accepted a wildcard sonar.tests across a line continuation"; fail=1; } +# --- Negative assertions: java.util.Properties \uXXXX escapes are decoded. ----- +# The scanner runs each key/value through java.util.Properties, which decodes +# \uXXXX before use, so an encoded wildcard (*) or encoded key must be +# caught after decoding — a raw string match would let it slip past the guard. +# 'bs' is a single backslash, built via awk so these fixtures can embed literal +# \uXXXX escapes without a heredoc or the editor mangling them. +bs="$(awk 'BEGIN { printf "%c", 92 }')" + +_bad_enc_value="${_fixture_dir}/bad-encoded-value.properties" +{ + echo "sonar.projectKey=petry-projects_broodly" + echo "sonar.tests=apps/${bs}u002a" +} > "$_bad_enc_value" + +! validate_sonar_properties "$_bad_enc_value" >/dev/null 2>&1 \ + && { echo "ok - validator rejects a \\uXXXX-encoded wildcard value"; pass_count=$((pass_count + 1)); } \ + || { echo "not ok - validator accepted a \\uXXXX-encoded wildcard value"; fail=1; } + +_bad_enc_key="${_fixture_dir}/bad-encoded-key.properties" +{ + echo "sonar.projectKey=petry-projects_broodly" + echo "sonar${bs}u002esources=apps/${bs}u002a" +} > "$_bad_enc_key" + +! validate_sonar_properties "$_bad_enc_key" >/dev/null 2>&1 \ + && { echo "ok - validator rejects a wildcard under a \\uXXXX-encoded key"; pass_count=$((pass_count + 1)); } \ + || { echo "not ok - validator accepted a wildcard under a \\uXXXX-encoded key"; fail=1; } + +_bad_malformed="${_fixture_dir}/bad-malformed-escape.properties" +cat > "$_bad_malformed" <<'EOF' +sonar.projectKey=petry-projects_broodly +sonar.tests=apps/\u00zz +EOF + +! validate_sonar_properties "$_bad_malformed" >/dev/null 2>&1 \ + && { echo "ok - validator rejects a malformed \\uXXXX escape"; pass_count=$((pass_count + 1)); } \ + || { echo "not ok - validator accepted a malformed \\uXXXX escape"; fail=1; } + +# --- Negative control: a \uXXXX-encoded key with no wildcard is accepted. ------ +# Decoding must not itself introduce a false positive: an encoded key/value that +# resolves to a wildcard-free directory list is valid and must pass. +_ok_encoded="${_fixture_dir}/ok-encoded.properties" +{ + echo "sonar.projectKey=petry-projects_broodly" + echo "sonar${bs}u002esources=apps/api,apps/mobile" +} > "$_ok_encoded" + +validate_sonar_properties "$_ok_encoded" >/dev/null 2>&1 \ + && { echo "ok - validator accepts a wildcard-free \\uXXXX-encoded key"; pass_count=$((pass_count + 1)); } \ + || { echo "not ok - validator wrongly rejected a wildcard-free \\uXXXX-encoded key"; fail=1; } + # --- Negative control: wildcards in sonar.exclusions are allowed. -------------- # SonarCloud permits wildcards in the filter properties; the guard must not flag # them, or it would reject the repo's own valid config.