-
Notifications
You must be signed in to change notification settings - Fork 0
205 lines (202 loc) · 7.65 KB
/
security_npm_dependency.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
name: Security npm dependency check
on:
workflow_call:
inputs:
channel_id:
description: 'The slack channel ID to send a message on failure. If this is not provided then no message is sent.'
required: false
default: 'NO_SLACK'
type: string
node_version_file:
description: "Passed to setup-node action to specify where to source the version of node from"
required: false
type: string
default: ".nvmrc"
secrets:
HMPPS_SRE_SLACK_BOT_TOKEN:
description: Slack bot token
required: false
permissions:
contents: read
security-events: write
jobs:
security-npm-check:
name: Security npm dependency check
runs-on: ubuntu-latest
permissions:
security-events: write
contents: read
actions: read
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node_version_file: ${{ inputs.node_version_file }}
- name: Show npm version
id: npm-version
run: 'npm -v'
- name: Show node version
id: node-version
run: 'node -v'
- name: Audit for vulnerabilities
id: npm
run: npx audit-ci@^7 --config ./audit-ci.json -o json > npm-security-check-reports.json
continue-on-error: true
- uses: actions/upload-artifact@v4
with:
name: npm-security-check-${{ github.event.repository.name }}
path: npm-security-check-reports.json
- name: Run translator to convert to sarif format
if: ${{ github.event.repository.visibility != 'private' && github.event.repository.visibility != 'internal' }}
run: |
import sys
import json
sev_lookup={
'high':'error',
'moderate':'warning',
'low':'note'
}
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def main():
if len(sys.argv)<2:
eprint('Usage: python3 auditjson_to_sarif.py <<input.json>> [-o output.json]')
sys.exit(1)
# Default for output file if required
args=sys.argv
input_file=args[1]
output_file=f"{args[1].split('.')[0]}.sarif"
for each_arg in args:
if each_arg=='-o' and len(args)>(args.index('-o')+1):
output_file=args[args.index('-o')+1]
# Build the file framework
output_dict={
"version": "2.1.0",
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.4.json",
"runs": [
{
"tool": {
"driver": {
"fullName": "NPM Audit",
"name": "npx audit-ci",
"rules": [],
"version": "0.0.13"
}
},
"results": [],
"artifacts": []
}
]
}
# Populate the results
rules_list=[]
result_list=[]
result_dict={}
try:
with open(input_file) as f:
results=json.load(f)
f.close()
if 'advisories' not in results:
eprint("No advisories in this json file - assuming it's OK")
else:
results_dict=results['advisories']
except:
eprint("Encountered an error - please check the json file")
sys.exit(1)
rule_index=0
for each_result_key in results_dict.keys():
this_result=results_dict[each_result_key]
# lookup result severity level
if this_result['severity'] in sev_lookup:
level=sev_lookup[this_result['severity']]
else:
level='none'
message=''
for each_element in this_result.keys():
message+=f'{each_element}: {this_result[each_element]}\n'
via=this_result['via'][0]
if not isinstance(via, str): # some vulnerabilities come via others - no sarif for them
rules_dict={
"id": via['cwe'][0],
"name": "LanguageSpecificPackageVulnerability",
"shortDescription": {
"text": via['title']
},
"defaultConfiguration": {
"level": level
},
"helpUri": via['url'],
"properties": {
"precision": "very-high",
"security-severity": str(via['cvss']['score']),
"tags": [
"vulnerability",
"security",
this_result['severity'].upper()
]
}
}
rules_list.append(rules_dict)
result_dict={
'ruleId': via['cwe'][0],
'ruleIndex': rule_index,
'level': level,
'message': {'text': message},
'locations': [ {
'physicalLocation': {
'artifactLocation': {
'uri': this_result['name']
}
}
} ]
}
result_list.append(result_dict)
rule_index+=1
output_dict['runs'][0]['tool']['driver']['rules']=rules_list
output_dict['runs'][0]['results']=result_list
with open(output_file,'w') as f:
json.dump(output_dict, f)
f.close()
if __name__ == '__main__':
main()
shell: python {0} npm-security-check-reports.json
- name: sarif upload
uses: github/codeql-action/upload-sarif@v3
id: npm-upload-sarif
if: ${{ github.event.repository.visibility != 'private' && github.event.repository.visibility != 'internal' }}
with:
sarif_file: 'npm-security-check-reports.sarif'
category: npm-dependency-check
- name: Audit for vulnerabilities - better
id: npm-better
if: steps.npm.outcome == 'failure'
run: npx better-npm-audit audit --level low --include-columns ID,Module,Paths,Severity,URL > npm-security-check-reports.txt
continue-on-error: true
env:
NO_COLOR: TRUE
- name: Process slack message results from npm better audit # returns SLACK_TXT
id: slack-message-result
if: steps.npm-better.outcome == 'failure' && inputs.channel_id != 'NO_SLACK'
uses: ministryofjustice/hmpps-github-actions/.github/actions/slack_prepare_results@v2 # WORKFLOW_VERSION
with:
input_file: npm-security-check-reports.txt
- name: npm audit slack notification
uses: ministryofjustice/hmpps-github-actions/.github/actions/slack_codescan_notification@v2 # WORKFLOW_VERSION
if: (failure() || steps.npm.outcome == 'failure' || steps.npm-better.outcome == 'failure') && inputs.channel_id != 'NO_SLACK'
with:
title: "npm dependency scan"
warningOnly: ${{ steps.npm-upload-sarif.outcome == 'success' && steps.slack-message-result.outcome == 'success' }}
channel_id: ${{ inputs.channel_id}}
SLACK_BOT_TOKEN: ${{ secrets.HMPPS_SRE_SLACK_BOT_TOKEN }}
- name: npm better audit slack output results
uses: slackapi/slack-github-action@37ebaef184d7626c5f204ab8d3baff4262dd30f0 # v1.27.0
if: inputs.channel_id != 'NO_SLACK' && steps.slack-message-result.outcome == 'success'
with:
channel-id: ${{ inputs.channel_id}}
payload: |
{
"type": "mrkdwn",
"text": "Output: ```${{ steps.slack-message-result.outputs.SLACK_TXT }}```"
}
env:
SLACK_BOT_TOKEN: ${{ secrets.HMPPS_SRE_SLACK_BOT_TOKEN }}