Skip to content

Commit b603146

Browse files
committed
ci: refactor build_docs so that pandoc runs in isolated read-only job [citest_skip]
The pandoc conversion part of the workflow does not need write access, so refactor that into a separate job. Add permissions to woke because it does not need write permisssion. The latest security guidance is to use the full commit hash, which is immutable, instead of a tag or version, which can be mutable, for the reference to a version of a github action. There are known attacks which inserted unauthorized code in a version tag and moved the tag. This prevents this sort of attack, at the cost of more maintenance burden, but dependabot will largely take care of this for us. We already did this for the other workflows, this is specific for the build_docs workflow. Signed-off-by: Rich Megginson <rmeggins@redhat.com>
1 parent ac378d3 commit b603146

2 files changed

Lines changed: 73 additions & 30 deletions

File tree

.github/workflows/build_docs.yml

Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,60 @@ on: # yamllint disable-line rule:truthy
1313
permissions:
1414
contents: read
1515
jobs:
16+
# Pandoc runs in an isolated read-only job so a compromised container cannot
17+
# use a runner-mounted contents: write credential to push.
1618
build_docs:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Check out README and Pandoc template
22+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
23+
with:
24+
persist-credentials: false
25+
sparse-checkout: |
26+
README.md
27+
.pandoc_template.html5
28+
sparse-checkout-cone-mode: false
29+
- name: Set RELEASE_VERSION based on whether run on release or on push
30+
env:
31+
RELEASE_TAG: ${{ github.event.release.tag_name }}
32+
run: |
33+
set -euxo pipefail
34+
if [ ${{ github.event_name }} = release ]; then
35+
tag_name="$RELEASE_TAG"
36+
if [[ "$tag_name" =~ ^[0-9]+[.][0-9]+[.][0-9]+$ ]]; then
37+
echo "RELEASE_VERSION=$tag_name" >> $GITHUB_ENV
38+
else
39+
echo "RELEASE_VERSION=${{ github.event.release.id }}" >> $GITHUB_ENV
40+
fi
41+
elif [ ${{ github.event_name }} = push ]; then
42+
echo "RELEASE_VERSION=latest" >> $GITHUB_ENV
43+
else
44+
echo Unsupported event
45+
exit 1
46+
fi
47+
48+
- name: Ensure that version directory exists
49+
run: mkdir -p ${{ env.RELEASE_VERSION }}
50+
51+
- name: Remove badges from README.md prior to converting to HTML
52+
run: sed -i '1,8 {/^\[\!.*actions\/workflows/d}' README.md
53+
54+
- name: Convert README.md to HTML and save to the version directory
55+
uses: docker://pandoc/core:8d7467e8ee40b0365a344c3d41067d6d2349da52e9961e4229f331094806fb14
56+
with:
57+
args: >-
58+
--from gfm --to html5 --toc --shift-heading-level-by=-1
59+
--template .pandoc_template.html5
60+
--output ${{ env.RELEASE_VERSION }}/README.html README.md
61+
62+
- name: Upload docs HTML artifact
63+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
64+
with:
65+
name: docs-html
66+
path: ${{ env.RELEASE_VERSION }}/README.html
67+
68+
publish_docs:
69+
needs: build_docs
1770
runs-on: ubuntu-latest
1871
permissions:
1972
contents: write
@@ -27,6 +80,7 @@ jobs:
2780
- name: Check out code
2881
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
2982
with:
83+
persist-credentials: true # needed for docs branch initialization push
3084
fetch-depth: 0
3185
- name: Ensure the docs branch
3286
run: |
@@ -51,50 +105,37 @@ jobs:
51105
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
52106
with:
53107
ref: docs
54-
55-
- name: Fetch README.md and .pandoc_template.html5 template from the workflow branch
56-
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
57-
with:
58-
sparse-checkout: |
59-
README.md
60-
.pandoc_template.html5
61-
sparse-checkout-cone-mode: false
62-
path: ref_branch
108+
persist-credentials: true # needed for commit and push
63109
- name: Set RELEASE_VERSION based on whether run on release or on push
110+
env:
111+
RELEASE_TAG: ${{ github.event.release.tag_name }}
64112
run: |
65113
set -euxo pipefail
66114
if [ ${{ github.event_name }} = release ]; then
67-
echo "RELEASE_VERSION=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
115+
tag_name="$RELEASE_TAG"
116+
if [[ "$tag_name" =~ ^[0-9]+[.][0-9]+[.][0-9]+$ ]]; then
117+
echo "RELEASE_VERSION=$tag_name" >> $GITHUB_ENV
118+
else
119+
echo "RELEASE_VERSION=${{ github.event.release.id }}" >> $GITHUB_ENV
120+
fi
68121
elif [ ${{ github.event_name }} = push ]; then
69122
echo "RELEASE_VERSION=latest" >> $GITHUB_ENV
70123
else
71124
echo Unsupported event
72125
exit 1
73126
fi
74127
75-
- name: Ensure that version and docs directories exist
76-
run: mkdir -p ${{ env.RELEASE_VERSION }} docs
77-
78-
- name: Remove badges from README.md prior to converting to HTML
79-
run: sed -i '1,8 {/^\[\!.*actions\/workflows/d}' ref_branch/README.md
80-
81-
- name: Convert README.md to HTML and save to the version directory
82-
uses: docker://pandoc/core:latest
128+
- name: Download docs HTML artifact
129+
uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7
83130
with:
84-
args: >-
85-
--from gfm --to html5 --toc --shift-heading-level-by=-1
86-
--template ref_branch/.pandoc_template.html5
87-
--output ${{ env.RELEASE_VERSION }}/README.html ref_branch/README.md
131+
name: docs-html
132+
path: ${{ env.RELEASE_VERSION }}
88133

89134
- name: Copy latest README.html to docs/index.html for GitHub pages
90135
if: env.RELEASE_VERSION == 'latest'
91-
run: cp ${{ env.RELEASE_VERSION }}/README.html docs/index.html
92-
93-
- name: Upload README.html as an artifact
94-
uses: actions/upload-artifact@v7
95-
with:
96-
name: README.html
97-
path: ${{ env.RELEASE_VERSION }}/README.html
136+
run: |
137+
mkdir -p docs
138+
cp ${{ env.RELEASE_VERSION }}/README.html docs/index.html
98139
99140
- name: Commit changes
100141
run: |
@@ -104,7 +145,7 @@ jobs:
104145
git commit -m "Update README.html for ${{ env.RELEASE_VERSION }}"
105146
106147
- name: Push changes
107-
uses: ad-m/github-push-action@master
148+
uses: ad-m/github-push-action@881a6320fdb16eb5318c5054f31c218aec2b324c # master
108149
with:
109150
github_token: ${{ secrets.GITHUB_TOKEN }}
110151
branch: docs

.github/workflows/woke.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
name: Woke
44
on: # yamllint disable-line rule:truthy
55
- pull_request
6+
permissions:
7+
contents: read
68
jobs:
79
woke:
810
if: |

0 commit comments

Comments
 (0)