Skip to content

Commit 007ae7f

Browse files
committed
report sarif in security per branch in Github
1 parent 42cb062 commit 007ae7f

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

.github/scripts/otp-compliance.es

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,8 +1342,8 @@ osv_scan(#{version := Version, sarif := Sarif}) ->
13421342
[];
13431343
_ ->
13441344
NameVulnerabilities = lists:zip(osv_names(OSVQuery), OSVResults),
1345-
lists:filtermap(fun ({Name, #{~"vulns" := Ids}}) ->
1346-
{true, {Name, [Id || #{~"id" := Id} <- Ids]}};
1345+
lists:filtermap(fun ({NameVersion, #{~"vulns" := Ids}}) ->
1346+
{true, {NameVersion, [Id || #{~"id" := Id} <- Ids]}};
13471347
(_) ->
13481348
false
13491349
end, NameVulnerabilities)
@@ -1352,24 +1352,24 @@ osv_scan(#{version := Version, sarif := Sarif}) ->
13521352
{error, [URI, Error]}
13531353
end,
13541354
Vulns1 = ignore_vex_cves(Vulns),
1355-
ok = generate_sarif(Sarif, Vulns1),
1355+
ok = generate_sarif(Version, Sarif, Vulns1),
13561356
FormattedVulns = format_vulnerabilities(Vulns1),
13571357
report_vulnerabilities(FormattedVulns).
13581358

1359-
generate_sarif(false, _Vulns) ->
1359+
generate_sarif(_, false, _Vulns) ->
13601360
io:format("[SARIF] No sarif file generated~n~n"),
13611361
ok;
1362-
generate_sarif(true, Vulns) ->
1362+
generate_sarif(Branch, true, Vulns) ->
13631363
SarifFilename = "results.sarif",
13641364

13651365
{ok, Cwd} = file:get_cwd(),
13661366
io:format("[SARIF] Generating Sarif: ~s~n", [Cwd ++ "/" ++ SarifFilename]),
13671367
io:format("ok~n~n"),
13681368

1369-
Sarif = json:format(generate_sarif(Vulns)),
1369+
Sarif = json:format(generate_sarif(Branch, Vulns)),
13701370
file:write_file(SarifFilename, Sarif).
13711371

1372-
generate_sarif(Vulns) ->
1372+
generate_sarif(Branch, Vulns) ->
13731373
#{ ~"version" => ~"2.1.0",
13741374
~"$schema" => ~"https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json",
13751375
~"runs" =>
@@ -1396,39 +1396,48 @@ generate_sarif(Vulns) ->
13961396
~"ruleId" => ~"CVE-OTP-VENDOR",
13971397
~"ruleIndex" => 0, % matches rule object that should apply
13981398
~"level" => ~"warning",
1399-
~"message" => #{ ~"text" => error_to_text({Dependency, CVE}) },
1399+
~"message" => #{
1400+
~"text" => error_to_text(Branch, Dependency, Version, CVE)
1401+
},
14001402
~"locations" =>
14011403
[ #{ ~"physicalLocation" =>
14021404
#{ ~"artifactLocation" =>
14031405
#{ ~"uri" => Dependency }}}
1404-
]
1405-
} || {Dependency, CVEs} <- Vulns, CVE <- CVEs],
1406+
],
1407+
~"partialFingerprints" =>
1408+
#{ Branch => calculate_fingerprint(Branch, Dependency, Version, CVE)}
1409+
} || {Dependency, Version, CVEs} <- Vulns, CVE <- CVEs],
14061410
~"artifacts" =>
14071411
[ #{ ~"location" => #{ ~"uri" => Dependency},
14081412
~"length" => -1
1409-
} || {Dependency, _} <- Vulns]
1413+
} || {Dependency, _, _} <- Vulns]
14101414
}]
14111415
}.
14121416

1413-
error_to_text({Dependency, Vuln}) ->
1414-
<<"Dependency ", Dependency/binary, " has ", Vuln/binary>>.
1417+
error_to_text(Branch, Dependency, Version, Vuln) ->
1418+
<<"[", Branch/binary, "] Dependency ", Dependency/binary, " in commit/version ", Version/binary,
1419+
" has the following detected vulnerability: ", Vuln/binary>>.
1420+
1421+
calculate_fingerprint(Branch, Dependency, Version, CVE) ->
1422+
Bin = crypto:hash(sha, <<Branch/binary, Dependency/binary, Version/binary, CVE/binary>>),
1423+
binary:encode_hex(Bin).
14151424

14161425
%% TODO: fix by reading VEX files from erlang/vex or repo containing VEX files
14171426
ignore_vex_cves(Vulns) ->
14181427
lists:foldl(fun ({~"github.com/wxWidgets/wxWidgets", _CVEs}, Acc) ->
14191428
%% OTP cannot be vulnerable to wxwidgets because
14201429
%% we only take documentation.
14211430
Acc;
1422-
({Name, CVEs}, Acc) ->
1431+
({{Name, Version}, CVEs}, Acc) ->
14231432
case maps:get(Name, non_vulnerable_cves(), not_found) of
14241433
not_found ->
1425-
[{Name, CVEs} | Acc];
1434+
[{Name, Version, CVEs} | Acc];
14261435
NonCVEs ->
14271436
case CVEs -- NonCVEs of
14281437
[] ->
14291438
Acc;
14301439
Vs ->
1431-
[{Name, Vs} | Acc]
1440+
[{Name, Version, Vs} | Acc]
14321441
end
14331442
end
14341443
end, [], Vulns).
@@ -1445,7 +1454,7 @@ non_vulnerable_cves() -> #{}.
14451454
format_vulnerabilities({error, ErrorContext}) ->
14461455
{error, ErrorContext};
14471456
format_vulnerabilities(ExistingVulnerabilities) when is_list(ExistingVulnerabilities) ->
1448-
lists:map(fun ({N, Ids}) ->
1457+
lists:map(fun ({N, _, Ids}) ->
14491458
io_lib:format("- ~s: ~s~n", [N, lists:join(",", Ids)])
14501459
end, ExistingVulnerabilities).
14511460

@@ -1458,11 +1467,14 @@ report_vulnerabilities(FormatVulns) ->
14581467

14591468
osv_names(#{~"queries" := Packages}) ->
14601469
lists:map(fun osv_names/1, Packages);
1461-
osv_names(#{~"package" := #{~"name" := Name }}) ->
1462-
Name.
1470+
osv_names(#{~"package" := #{~"name" := Name }, ~"commit" := Commit}) ->
1471+
{Name, Commit};
1472+
osv_names(#{~"package" := #{~"name" := Name }, ~"version" := Version}) ->
1473+
{Name, Version}.
1474+
14631475

14641476
generate_osv_query(Packages) ->
1465-
#{~"queries" => lists:foldl(fun generate_osv_query/2, [], Packages)}.
1477+
#{~"queries" => lists:usort(lists:foldl(fun generate_osv_query/2, [], Packages))}.
14661478
generate_osv_query(#{~"versionInfo" := Vsn, ~"ecosystem" := Ecosystem, ~"name" := Name}, Acc) ->
14671479
Package = #{~"package" => #{~"name" => Name, ~"ecosystem" => Ecosystem}, ~"version" => Vsn},
14681480
[Package | Acc];

.github/workflows/reusable-vendor-vulnerability-scanner.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,18 @@ jobs:
8282
cd /home/runner/work/otp/otp/${{ env.VERSION }} && \
8383
.github/scripts/otp-compliance.es sbom osv-scan --version ${{ inputs.version }}
8484
85+
- name: "Get ${{ inputs.version }} sha"
86+
id: sha
87+
if: ${{ !failure() && inputs.sarif }}
88+
run: |
89+
echo "sha=$(cd /home/runner/work/otp/otp/${{ env.VERSION }} && git rev-parse HEAD)" >> $GITHUB_OUTPUT
90+
8591
- name: "Upload to code-scanning"
8692
if: ${{ !failure() && inputs.sarif }}
93+
env:
94+
SHA: ${{ steps.sha.outputs.sha }}
8795
uses: github/codeql-action/upload-sarif@ea9e4e37992a54ee68a9622e985e60c8e8f12d9f # ratchet:github/codeql-action/upload-sarif@v3
8896
with:
8997
sarif_file: ${{ env.VERSION }}/results.sarif
98+
ref: refs/heads/${{ env.VERSION }}
99+
sha: ${{ env.SHA }}

erts/emulator/openssl/vendor.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"licenseDeclared": "Apache-2.0",
1616
"name": "openssl",
1717
"versionInfo": "3.5",
18-
"sha": "cdfb0923a66155ce97640fca68ae57b3a2972029",
18+
"sha": "636dfadc70ce26f2473870570bfd9ec352806b1d",
1919
"path": "./erts/emulator/openssl",
2020
"exclude": ["./erts/emulator/openssl/vendor.info",
2121
"./erts/emulator/openssl/README",

0 commit comments

Comments
 (0)