Repository navigation
259 lines (234 loc) · 11.8 KB
/
Copy pathsecurity.yml
File metadata and controls
259 lines (234 loc) · 11.8 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
name: security
# OSV-Scanner over the lockfile. The job is read-only except for the SARIF
# upload step, which needs security-events: write.
#
# CodeQL is not run from here. The repository uses GitHub's default CodeQL
# setup (Settings → Code security), which GitHub schedules and configures
# itself, and GitHub rejects a SARIF upload whose results were produced by
# CodeQL while the default setup is on. Only CodeQL's own results are refused,
# which is why the OSV upload below is unaffected. Nothing is lost by dropping
# the workflow's own CodeQL job either: the default setup analyzes
# `javascript-typescript` and `actions`, where the job analyzed only the
# former.
#
# The OSV upload is an explicit opt-in, gated behind the
# vars.CODE_SCANNING_ENABLED repository variable: findings reach the Security
# tab only once an admin creates that variable with the value "true". The gate
# is a rollout switch, not a workaround — Code Security is enabled on this
# repository, so the upload is expected to succeed the moment the variable is
# set. It stays a switch because a SARIF upload 403s against a repository
# without Code Security, and a permanently failing check trains reviewers to
# ignore it. While the gate is closed osv-scanner still runs and still
# attaches its SARIF as a plain workflow artifact, so the results are not
# lost.
#
# While that gate is closed the job announces it on every run, in the log and
# in the job summary: a skipped upload that says nothing is indistinguishable
# from a scan that found nothing.
#
# OSV findings do not fail this workflow. A transitive advisory with no
# available fix would otherwise block every unrelated pull request, and the
# archive serves prerendered static files rather than running this dependency
# tree in production. Findings are surfaced instead — job summary, warning
# annotations, and the Security tab once the gate above is open — so the
# signal is visible without being a merge gate. A scanner that fails to run,
# by contrast, does fail the job: "found nothing" and "could not look" must
# not report the same way.
#
# The production-headers job runs only on the schedule and on manual runs. It
# checks that the public docs site (vars.DOCS_SITE_URL) still serves the
# router's security headers (apps/router/scripts/headers-check.mjs;
# ARCHITECTURE.md's CSP section says why they can drift). A scheduled failure opens or updates one issue labelled
# `security-header-drift`, and the next passing run closes it.
on:
push:
branches: [main]
pull_request:
schedule:
# Weekly, off the standard push/PR path, so a dependency-only advisory or
# a coder.com header change still gets caught when nobody touches the repo.
- cron: "0 6 * * 1"
workflow_dispatch:
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
osv-scanner:
name: OSV-Scanner (pnpm-lock.yaml)
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
security-events: write
actions: read
steps:
- name: Harden Runner
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
with:
egress-policy: audit
# Every run says whether the upload is actually wired up. Without this
# the gate is invisible: the Security tab stays empty, and nothing
# distinguishes that from a clean scan.
- name: Report the OSV upload gate
if: ${{ vars.CODE_SCANNING_ENABLED != 'true' }}
run: |
set -euo pipefail
note="OSV-Scanner results are not uploaded to the Security tab: the repository variable CODE_SCANNING_ENABLED is not \"true\". Set it to turn the upload on (Settings → Secrets and variables → Actions → Variables)."
echo "::warning::${note}"
{
echo "### OSV-Scanner upload is disabled"
echo
echo "${note}"
echo
echo "OSV-Scanner still ran; its SARIF is attached to this run as the \`osv-scanner\` artifact."
} >> "$GITHUB_STEP_SUMMARY"
- name: Checkout
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
persist-credentials: false
- name: Run OSV-Scanner against pnpm-lock.yaml
id: scan
continue-on-error: true
uses: google/osv-scanner-action/osv-scanner-action@a345acffa64b0eaede81a3d9aae6141214d9c8fc # v2.6.0
with:
scan-args: |-
--lockfile=pnpm-lock.yaml
--format=sarif
--output-file=osv-results.sarif
# Findings are reported, not fatal (see the header). What IS fatal is a
# scanner that did not produce a result set: OSV-Scanner exits non-zero
# both when it finds vulnerabilities and when it fails, so the two are
# told apart by the SARIF it leaves behind rather than by the exit code
# alone.
- name: Summarize OSV-Scanner results
if: always()
env:
# Read through the environment rather than interpolated into the
# script body: `outcome` is a fixed enum today, but a `${{ }}`
# expansion inside `run:` is the shape of a shell-injection bug and
# should not be the pattern this repo copies from.
SCAN_OUTCOME: ${{ steps.scan.outcome }}
run: |
set -euo pipefail
if [[ ! -f osv-results.sarif ]]; then
if [[ "${SCAN_OUTCOME}" == "success" ]]; then
echo "OSV-Scanner reported no vulnerabilities in \`pnpm-lock.yaml\`." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
echo "::error::OSV-Scanner did not produce a SARIF file (outcome: ${SCAN_OUTCOME}); the lockfile was not scanned"
exit 1
fi
findings="$(jq '[.runs[]?.results[]?] | length' osv-results.sarif)"
if [[ "${findings}" -eq 0 ]]; then
if [[ "${SCAN_OUTCOME}" != "success" ]]; then
echo "::error::OSV-Scanner exited unsuccessfully with an empty result set; treating that as a failed scan rather than a clean one"
exit 1
fi
echo "OSV-Scanner found no known vulnerabilities in \`pnpm-lock.yaml\`." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
jq -r '.runs[]?.results[]? | "\(.ruleId // "unknown")\t\((.message.text // "no detail") | split("\n")[0])"' \
osv-results.sarif > findings.tsv
{
echo "### OSV-Scanner: ${findings} finding(s) in \`pnpm-lock.yaml\`"
echo
echo "Reported, not blocking. Review each advisory and bump the dependency, or record why it does not apply."
echo
echo "| Advisory | Detail |"
echo "| --- | --- |"
while IFS=$'\t' read -r rule detail; do
printf '| %s | %s |\n' "${rule}" "${detail}"
done < findings.tsv
} >> "$GITHUB_STEP_SUMMARY"
while IFS=$'\t' read -r rule detail; do
echo "::warning title=OSV-Scanner ${rule}::${detail}"
done < findings.tsv
# Always uploaded, independent of GitHub Code Security, so results are
# never lost while vars.CODE_SCANNING_ENABLED is unset.
- name: Upload OSV-Scanner scan results as an artifact
if: ${{ always() && hashFiles('osv-results.sarif') != '' }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: osv-scanner
path: osv-results.sarif
retention-days: 30
# See the header comment: the upload is an explicit opt-in, skipped
# until vars.CODE_SCANNING_ENABLED is set to "true", and for fork pull
# requests, whose read-only token cannot upload.
- name: Upload OSV-Scanner scan results to GitHub Security tab
if: ${{ always() && hashFiles('osv-results.sarif') != '' && vars.CODE_SCANNING_ENABLED == 'true' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) }}
uses: github/codeql-action/upload-sarif@b96794f015dfd88f77b49b1c93e0fa7110f94c63 # v4.38.0
with:
sarif_file: osv-results.sarif
category: "OSV-Scanner"
production-headers:
name: Production security headers
if: ${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
issues: write
steps:
- name: Harden Runner
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
with:
egress-policy: audit
- name: Checkout
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
persist-credentials: false
- uses: ./.github/actions/setup
# The expected headers come from the router's own responseHeadersFor, which imports the manifest package's build.
- name: Build the manifest package
run: pnpm turbo run build --filter=@coder/docs-manifest
- name: Choose the active site
id: site
working-directory: apps/docs
env:
DOCS_PUBLISH_SITE: ${{ vars.DOCS_PUBLISH_SITE }}
run: node scripts/publish/sites.mjs
- name: Check production headers against the router's
id: check
env:
DOCS_SITE_URL: ${{ vars.DOCS_SITE_URL }}
DOCS_BASE_PATH: ${{ vars.DOCS_BASE_PATH }}
# The router's own DOCS_ANALYTICS is expected to match the site table's flag the publisher builds with.
DOCS_ANALYTICS: ${{ steps.site.outputs.analytics }}
run: pnpm --filter @coder/docs-router headers:check
- name: Report drift on the tracking issue
if: ${{ failure() && github.event_name == 'schedule' }}
env:
GH_TOKEN: ${{ github.token }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
CHECK: ${{ steps.check.outcome }}
DOCS_SITE_URL: ${{ vars.DOCS_SITE_URL }}
DOCS_BASE_PATH: ${{ vars.DOCS_BASE_PATH }}
run: |
set -euo pipefail
label="security-header-drift"
gh label create "$label" -R "$GITHUB_REPOSITORY" --color B60205 --description "The public docs site no longer serves the router's security headers" --force
if [[ "$CHECK" == "failure" ]]; then
note="The weekly check found $DOCS_SITE_URL serving headers that differ from the router's; the diff is in the job log and summary of $RUN_URL. coder.com's header rules for $DOCS_BASE_PATH replace the router's (see ARCHITECTURE.md, \"Content Security Policy for the public docs host\"), so the fix is usually in coder.com's next.config.js."
else
note="The weekly production header check did not run to completion; see $RUN_URL."
fi
existing=$(gh issue list -R "$GITHUB_REPOSITORY" --label "$label" --state open --json number --limit 1 --jq '.[0].number // empty')
if [[ -n "$existing" ]]; then
gh issue comment -R "$GITHUB_REPOSITORY" "$existing" --body "$note"
else
gh issue create -R "$GITHUB_REPOSITORY" --title "Production security headers drifted" --label "$label" --body "$note"
fi
- name: Close the tracking issue
if: ${{ success() }}
env:
GH_TOKEN: ${{ github.token }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
set -euo pipefail
existing=$(gh issue list -R "$GITHUB_REPOSITORY" --label "security-header-drift" --state open --json number --limit 1 --jq '.[0].number // empty')
if [[ -n "$existing" ]]; then
gh issue close -R "$GITHUB_REPOSITORY" "$existing" --comment "Production serves the router's security headers again ($RUN_URL)."
fi