Skip to content

Commit affe451

Browse files
refactor(ci): centralize binary scan config and remove policy bash bridges
1 parent cf33a59 commit affe451

6 files changed

Lines changed: 103 additions & 140 deletions

File tree

tools/ci/bin/run.sh

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,57 @@ run_or_fail() {
4646
fi
4747
}
4848

49+
run_policy_runner_bridge() {
50+
local policy_check_id="$1"
51+
local policy_out_dir="$2"
52+
local summary_message="$3"
53+
local fallback_evidence="$4"
54+
55+
mkdir -p "${ROOT_DIR}/${policy_out_dir}"
56+
57+
local policy_exit=0
58+
if ! ci_run_capture "${summary_message}" dotnet "${ROOT_DIR}/tools/ci/checks/PolicyRunner/bin/Release/net10.0/PolicyRunner.dll" --check-id "${policy_check_id}" --repo-root "${ROOT_DIR}" --out-dir "${policy_out_dir}"; then
59+
policy_exit=1
60+
fi
61+
62+
local policy_result_json="${ROOT_DIR}/${policy_out_dir}/result.json"
63+
if [[ ! -f "${policy_result_json}" ]]; then
64+
ci_result_add_violation "CI-POLICY-001" "fail" "PolicyRunner did not produce result.json" "${policy_out_dir}/result.json"
65+
ci_result_append_summary "${summary_message} failed (missing result.json)."
66+
return 1
67+
fi
68+
69+
local findings=0
70+
local has_fail=0
71+
while IFS= read -r violation; do
72+
local rule_id severity message
73+
rule_id="$(jq -r '.rule_id' <<< "$violation")"
74+
severity="$(jq -r '.severity' <<< "$violation")"
75+
message="$(jq -r '.message' <<< "$violation")"
76+
77+
mapfile -t evidence_paths < <(jq -r '.evidence_paths[]' <<< "$violation")
78+
if [[ "${#evidence_paths[@]}" -eq 0 ]]; then
79+
evidence_paths=("${fallback_evidence}")
80+
fi
81+
82+
ci_result_add_violation "$rule_id" "$severity" "$message" "${evidence_paths[@]}"
83+
findings=$((findings + 1))
84+
if [[ "$severity" == "fail" ]]; then
85+
has_fail=1
86+
fi
87+
done < <(jq -c '.rule_violations[]' "${policy_result_json}")
88+
89+
if [[ "$findings" -eq 0 ]]; then
90+
ci_result_append_summary "${summary_message} passed."
91+
else
92+
ci_result_append_summary "${summary_message} violations: ${findings}"
93+
fi
94+
95+
if [[ "$policy_exit" -ne 0 || "$has_fail" -eq 1 ]]; then
96+
return 1
97+
fi
98+
}
99+
49100
build_validators() {
50101
run_or_fail "CI-SETUP-001" "Restore validator projects (locked mode)" dotnet restore --locked-mode "${ROOT_DIR}/tools/ci/checks/ResultSchemaValidator/ResultSchemaValidator.csproj"
51102
run_or_fail "CI-SETUP-001" "Build ResultSchemaValidator" dotnet build -c Release "${ROOT_DIR}/tools/ci/checks/ResultSchemaValidator/ResultSchemaValidator.csproj"
@@ -63,7 +114,7 @@ run_preflight() {
63114
run_or_fail "CI-PREFLIGHT-001" "Docs check" python3 "${ROOT_DIR}/tools/check-docs.py"
64115
run_or_fail "CI-PREFLIGHT-001" "Versioning guard" bash "${ROOT_DIR}/tools/versioning/check-versioning.sh"
65116
run_or_fail "CI-PREFLIGHT-001" "Format check" dotnet format "${ROOT_DIR}/FileClassifier.sln" --verify-no-changes
66-
if ! ci_run_capture "Policy shell safety" bash "${ROOT_DIR}/tools/ci/policies/policy_shell_safety.sh"; then
117+
if ! run_policy_runner_bridge "preflight" "artifacts/ci/_policy_preflight" "Policy shell safety" "tools/ci/bin/run.sh"; then
67118
return 1
68119
fi
69120
run_or_fail "CI-GRAPH-001" "CI graph assertion" bash "${ROOT_DIR}/tools/ci/bin/assert_ci_graph.sh"

tools/ci/checks/PolicyRunner/Program.cs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ void EvaluateRegexLineRule(PolicyRule rule)
259259
foreach (var file in files)
260260
{
261261
var relFile = Rel(repoRootPath, file);
262-
if (IsKnownBinaryPath(file))
262+
if (IsConfiguredBinaryPath(file, rule.Params.BinaryExtensions))
263263
{
264-
logger.Add($"SCAN_SKIP|rule_id={rule.RuleId}|file={relFile}|reason=known_binary_extension");
264+
logger.Add($"SCAN_SKIP|rule_id={rule.RuleId}|file={relFile}|reason=configured_binary_extension");
265265
continue;
266266
}
267267

@@ -692,42 +692,21 @@ static int LeadingSpaces(string input)
692692
return count;
693693
}
694694

695-
static bool IsKnownBinaryPath(string path)
695+
static bool IsConfiguredBinaryPath(string path, IReadOnlyList<string> configuredExtensions)
696696
{
697+
if (configuredExtensions.Count == 0)
698+
{
699+
return false;
700+
}
701+
697702
var ext = Path.GetExtension(path);
698703
if (string.IsNullOrWhiteSpace(ext))
699704
{
700705
return false;
701706
}
702707

703-
return ext.ToLowerInvariant() switch
704-
{
705-
".dll" => true,
706-
".exe" => true,
707-
".pdb" => true,
708-
".so" => true,
709-
".dylib" => true,
710-
".a" => true,
711-
".o" => true,
712-
".class" => true,
713-
".jar" => true,
714-
".zip" => true,
715-
".gz" => true,
716-
".7z" => true,
717-
".tar" => true,
718-
".png" => true,
719-
".jpg" => true,
720-
".jpeg" => true,
721-
".gif" => true,
722-
".bmp" => true,
723-
".ico" => true,
724-
".pdf" => true,
725-
".woff" => true,
726-
".woff2" => true,
727-
".ttf" => true,
728-
".otf" => true,
729-
_ => false
730-
};
708+
var normalized = ext.StartsWith(".", StringComparison.Ordinal) ? ext.ToLowerInvariant() : $".{ext.ToLowerInvariant()}";
709+
return configuredExtensions.Any(configured => string.Equals(configured, normalized, StringComparison.Ordinal));
731710
}
732711

733712
static (bool Ok, string[] Lines, string? ErrorCode) TryReadUtf8Lines(string filePath)
@@ -904,6 +883,7 @@ static bool IsKnownBinaryPath(string path)
904883
"check_ids" or
905884
"artifact_root" or
906885
"scan_paths" or
886+
"binary_extensions" or
907887
"regex_pattern" or
908888
"max_inline_run_lines" or
909889
"docs_paths" or
@@ -922,6 +902,11 @@ static bool IsKnownBinaryPath(string path)
922902
CheckIds: GetStringSequence(paramsNode, "check_ids").OrderBy(static x => x, StringComparer.Ordinal).Distinct(StringComparer.Ordinal).ToList(),
923903
ArtifactRoot: GetScalar(paramsNode, "artifact_root"),
924904
ScanPaths: GetStringSequence(paramsNode, "scan_paths").OrderBy(static x => x, StringComparer.Ordinal).Distinct(StringComparer.Ordinal).ToList(),
905+
BinaryExtensions: GetStringSequence(paramsNode, "binary_extensions")
906+
.Select(static extension => extension.StartsWith(".", StringComparison.Ordinal) ? extension.ToLowerInvariant() : $".{extension.ToLowerInvariant()}")
907+
.OrderBy(static x => x, StringComparer.Ordinal)
908+
.Distinct(StringComparer.Ordinal)
909+
.ToList(),
925910
RegexPattern: GetScalar(paramsNode, "regex_pattern"),
926911
MaxInlineRunLines: GetIntScalar(paramsNode, "max_inline_run_lines"),
927912
DocsPaths: GetStringSequence(paramsNode, "docs_paths").OrderBy(static x => x, StringComparer.Ordinal).Distinct(StringComparer.Ordinal).ToList(),
@@ -991,6 +976,7 @@ internal sealed record RuleParams(
991976
IReadOnlyList<string> CheckIds,
992977
string? ArtifactRoot,
993978
IReadOnlyList<string> ScanPaths,
979+
IReadOnlyList<string> BinaryExtensions,
994980
string? RegexPattern,
995981
int? MaxInlineRunLines,
996982
IReadOnlyList<string> DocsPaths,

tools/ci/policies/policy_artifact_contract.sh

Lines changed: 0 additions & 54 deletions
This file was deleted.

tools/ci/policies/policy_shell_safety.sh

Lines changed: 0 additions & 54 deletions
This file was deleted.

tools/ci/policies/rules/shell_safety.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,31 @@ rules:
88
applies_to:
99
- preflight
1010
params:
11+
binary_extensions: &binary_extensions
12+
- dll
13+
- exe
14+
- pdb
15+
- so
16+
- dylib
17+
- a
18+
- o
19+
- class
20+
- jar
21+
- zip
22+
- gz
23+
- 7z
24+
- tar
25+
- png
26+
- jpg
27+
- jpeg
28+
- gif
29+
- bmp
30+
- ico
31+
- pdf
32+
- woff
33+
- woff2
34+
- ttf
35+
- otf
1136
scan_paths:
1237
- .github/workflows
1338
regex_pattern: 'continue-on-error:\s*true'
@@ -20,6 +45,7 @@ rules:
2045
applies_to:
2146
- preflight
2247
params:
48+
binary_extensions: *binary_extensions
2349
scan_paths:
2450
- .github/workflows
2551
regex_pattern: '\|\|\s*true'
@@ -32,6 +58,7 @@ rules:
3258
applies_to:
3359
- preflight
3460
params:
61+
binary_extensions: *binary_extensions
3562
scan_paths:
3663
- .github/workflows
3764
- tools/ci

tools/ci/policies/schema/rules.schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
"minLength": 1
8585
}
8686
},
87+
"binary_extensions": {
88+
"type": "array",
89+
"items": {
90+
"type": "string",
91+
"pattern": "^\\.?[a-zA-Z0-9]+$"
92+
}
93+
},
8794
"regex_pattern": {
8895
"type": "string",
8996
"minLength": 1

0 commit comments

Comments
 (0)