-
Notifications
You must be signed in to change notification settings - Fork 0
168 lines (141 loc) · 5.61 KB
/
security.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
name: Security
on:
workflow_run:
workflows: ["Pre-Analysis"]
types:
- completed
permissions:
actions: read
pull-requests: write
jobs:
antivirus:
runs-on: ubuntu-latest
timeout-minutes: 10
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
steps:
- name: 'Download pre analysis artifacts'
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const fs = require('fs');
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "pr-artifacts"
})[0];
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/pr-artifacts.zip`, Buffer.from(download.data));
- name: 'Unzip artifacts'
run: unzip pr-artifacts.zip
- name: 'Verify bundles.json file and URL format'
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const fs = require('fs')
const validateUrl = function (str) {
let urlRegex = /^(https):\/\/[^\s/$.?#].[^\s]*$/i;
let url = new RegExp(urlRegex);
if (!url.test(str)) {
throw new Error(`Invalid url : ${str}`)
}
}
let rawBundles = fs.readFileSync('bundles.json')
let bundles = JSON.parse(rawBundles)
for (let bundle of bundles) {
validateUrl(bundle)
}
- name: 'Verify bundles.json size'
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
PR_NUMBER: ${{ env.pr_number }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const fs = require('fs')
const { PR_NUMBER } = process.env
const BUNDLES_MAX_SIZE = 20
let rawBundles = fs.readFileSync('bundles.json')
let bundles = JSON.parse(rawBundles)
if (bundles.length > BUNDLES_MAX_SIZE) {
let message = `Less than ${BUNDLES_MAX_SIZE} VirusTotal analysis per merge request allowed! `
+ `Please, reduce number of bundles (${bundles.length}/${BUNDLES_MAX_SIZE}) by opening multiple merge requests.`
+ `If no bundles binaries are added or modified, you can ignore this message.`
github.rest.issues.createComment({
issue_number: PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
body: `:shield: **Virus Total Analysis Report**
${message}`
})
console.log(message)
throw new Error(message)
} else {
console.log(`Allowed bundles : ${bundles.length} / ${BUNDLES_MAX_SIZE}`)
}
- name: Retrieve bundles url
id: bundles-url
run: |
BUNDLES_JSON=$(cat bundles.json)
echo "$BUNDLES"
echo "bundles_json=$BUNDLES_JSON" >> $GITHUB_ENV
- name: Retrieve PR number
id: pr-number
run: |
PR_NUMBER=$(cat pr-number)
echo "$PR_NUMBER"
echo "pr_number=$PR_NUMBER" >> $GITHUB_ENV
- name: "Download bundles files"
id: bundles-download
run: |
readarray -t bundles <<<"$(jq -r '.[]' <<<'${{ env.bundles_json }}')"
path=()
mkdir /tmp/bundles
for bundle in ${bundles[@]}; do
echo "Retrieving bundles ${bundle}."
wget --user-agent github/owlplug-workflow --directory-prefix /tmp/bundles ${bundle}
done
files=(/tmp/bundles/*)
FILES_JSON=$(jq --compact-output --null-input '$ARGS.positional' --args -- "${files[@]}")
echo "files_json=$FILES_JSON" >> $GITHUB_ENV
- name: VirusTotal Scan
id: vt-scan
uses: crazy-max/ghaction-virustotal@93ce6fb8ca09fed3d3d2010a8be65552ae5d3854 # v4.1.0
with:
vt_api_key: ${{ secrets.VT_API_KEY }}
request_rate: 4
files: "${{join(fromJSON(env.files_json),'\n')}}"
- name: Publish AV report
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
VT_OUTPUT: ${{ steps.vt-scan.outputs.analysis }}
PR_NUMBER: ${{ env.pr_number }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const { VT_OUTPUT, PR_NUMBER } = process.env
// Abort script if VT_OUTPUT is empty, null or undefined
if (!VT_OUTPUT) {
return;
}
let markdown = ""
let files = VT_OUTPUT.split(",")
for (let file of files) {
markdown += "* " + file.replace("=https://", ": https://") + "\n";
}
github.rest.issues.createComment({
issue_number: PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
body: `:shield: **Virus Total Analysis Report**
${markdown}`
})