Skip to content

Commit e6360fc

Browse files
committed
add a rule to force GH Actions to use same node version as package.json
1 parent 864b07e commit e6360fc

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
tests:
2+
- name: "Should have node version file configured"
3+
def: {}
4+
params: {}
5+
expect: "pass"
6+
git:
7+
repo_base: correct
8+
- name: "Should fail when the user manually specified node version in GH Actions"
9+
def: {}
10+
params: {}
11+
expect: "fail"
12+
git:
13+
repo_base: misconfigured
14+
- name: "Should pass when there are no GH Actions at all"
15+
def: {}
16+
params: {}
17+
expect: "pass"
18+
git: {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
name: NodeJS with Gulp
3+
'on':
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: Use Node.js
16+
uses: actions/setup-node@v3
17+
with:
18+
node-version-file: package.json
19+
- name: Build
20+
run: |
21+
npm install
22+
gulp
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
name: NodeJS with Gulp
3+
'on':
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: Use Node.js
16+
uses: actions/setup-node@v3
17+
with:
18+
node-version: 22
19+
- name: Build
20+
run: |
21+
npm install
22+
gulp
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
version: v1
3+
release_phase: alpha
4+
type: rule-type
5+
name: force_package_json_for_node_version
6+
display_name: Force GitHub Actions to use the node version specified in package.json
7+
short_failure_message: GitHub Actions need to specify node version using `node-version-file`
8+
severity:
9+
value: medium
10+
context: {}
11+
description: |
12+
Verifies that the Node version used in GitHub Actions workflow files is the same as the one specified
13+
in `package.json`. This ensures that the application is tested with the same Node version in GitHub Actions
14+
that it is meant to be locally developed with.
15+
guidance: |
16+
To make sure you are running GitHub Actions with the same Node version that your application is meant
17+
to be developed and deploy with, one step you can take is manage your Node version through `package.json`.
18+
19+
In the `actions/setup-node` GitHub Action, you can use the `node-version-file` input set to `package.json`.
20+
This helps establish a single source of truth for the Node version.
21+
22+
For more information, check out `actions/setup-node`'s [documentation](https://github.com/actions/setup-node/blob/main/docs/advanced-usage.md#node-version-file).
23+
24+
This approach is recommended for Node applications that are meant to be deployed in the cloud directly,
25+
but might not be as well suited for libraries that need to be tested against various different versions of Node.
26+
def:
27+
in_entity: repository
28+
rule_schema:
29+
type: object
30+
properties: {}
31+
ingest:
32+
type: git
33+
git: {}
34+
eval:
35+
type: rego
36+
rego:
37+
type: deny-by-default
38+
def: |
39+
package minder
40+
import future.keywords.if
41+
import future.keywords.every
42+
43+
default message := "For GitHub Actions workflows, Node version has to be specified through the `node-version-file` argument."
44+
default allow := false
45+
46+
workflows := file.ls(".github/workflows/")
47+
48+
all_args[args] {
49+
some w
50+
51+
file.read(workflows[w])
52+
workflowstr := file.read(workflows[w])
53+
workflow := parse_yaml(workflowstr)
54+
55+
some job_id
56+
job := workflow.jobs[job_id]
57+
58+
some step_id
59+
step := job.steps[step_id]
60+
61+
startswith(step.uses, "actions/setup-node@")
62+
63+
args := step["with"]
64+
}
65+
66+
allow if {
67+
every args in all_args {
68+
print(args)
69+
args["node-version-file"] == "package.json"
70+
not args["node-version"]
71+
}
72+
}
73+
74+
message := "" if allow
75+
alert:
76+
type: security_advisory
77+
security_advisory: {}

0 commit comments

Comments
 (0)