Skip to content

Commit 0c98acf

Browse files
committed
Add npx rule for YAML files
1 parent 05d2bc4 commit 0c98acf

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
rules:
2+
- id: npx-usage-yml
3+
languages:
4+
- yaml
5+
severity: WARNING
6+
metadata:
7+
tags: [security]
8+
shortDescription: "npx usage introduces supply chain security risks"
9+
confidence: HIGH
10+
help: |
11+
Using npx to install and run packages introduces significant supply chain security risks for the following reasons:
12+
13+
1. **Unpinned by default**: Running `npx <package>` fetches the latest release outside of your lockfile. If a malicious version of a package is published ([example])(https://socket.dev/blog/npm-author-qix-compromised-in-major-supply-chain-attack), `npx` will install and execute it the next time it is run.
14+
15+
2. **Bypasses lockfile guarantees**: Packages executed with npx are not added to your project's package.json or lockfile. As a result, their versions and lockfile integrity hashes are not captured for reproducibility, making builds non-deterministic and harder to audit
16+
17+
### Recommended practice
18+
- Add packages as dependencies or devDependencies in `package.json`.
19+
- Use your package manager to install and execute them (e.g., `yarn add <package> --dev` followed by `yarn <package> <command>`).
20+
21+
**Bad example (using npx):**
22+
```yaml
23+
- name: Run tests
24+
run: npx jest --coverage
25+
```
26+
27+
**Good example (proper dependency):**
28+
```yaml
29+
- name: Run tests
30+
run: yarn jest --coverage
31+
```
32+
33+
message: >-
34+
Avoid using 'npx' to run packages due to supply chain security risks. Instead, install the package
35+
as a dependency / devDependency and invoke it using your package manager to ensure version pinning
36+
and reproducibility.
37+
patterns:
38+
- pattern: |
39+
run: $CMD
40+
- metavariable-pattern:
41+
metavariable: $CMD
42+
language: sh
43+
pattern: npx ...
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Test Workflow
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
jobs:
8+
test:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
13+
# Test basic npx usage in GitHub Actions - should be flagged
14+
- name: Run tests
15+
# ruleid: npx-usage-yml
16+
run: npx jest --coverage
17+
18+
- name: Lint code
19+
# ruleid: npx-usage-yml
20+
run: npx eslint src/
21+
22+
- name: Format code
23+
# ruleid: npx-usage-yml
24+
run: npx prettier --write .
25+
26+
- name: Create app
27+
# ruleid: npx-usage-yml
28+
run: npx create-react-app my-app
29+
30+
- name: Setup tool with flag
31+
# ruleid: npx-usage-yml
32+
run: npx --yes setup-tool --config config.json
33+
34+
- name: Run with env vars
35+
# ruleid: npx-usage-yml
36+
run: npx jest ${GITHUB_WORKSPACE} --coverage
37+
38+
# Test scoped package with output redirection - should be flagged
39+
- name: Generate fingerprint
40+
# ruleid: npx-usage-yml
41+
run: npx @expo/fingerprint ./ > fingerprint-pr.json
42+
43+
- name: Another scoped package
44+
# ruleid: npx-usage-yml
45+
run: npx @typescript-eslint/parser --version
46+
47+
# Test npx in middle of command strings - should be flagged
48+
- name: Install and test
49+
# ruleid: npx-usage-yml
50+
run: yarn install && npx jest --coverage
51+
52+
- name: Setup and lint
53+
# ruleid: npx-usage-yml
54+
run: echo "Setting up" && npx eslint src/
55+
56+
- name: Build and format
57+
# ruleid: npx-usage-yml
58+
run: npm run build && npx prettier --write .
59+
60+
# Test good alternatives - should not be flagged
61+
- name: Run tests with yarn
62+
# ok: npx-usage-yml
63+
run: yarn jest --coverage
64+
65+
- name: Lint code with yarn
66+
# ok: npx-usage-yml
67+
run: yarn eslint src/
68+
69+
- name: Format code with yarn
70+
# ok: npx-usage-yml
71+
run: yarn prettier --write .
72+
73+
- name: Create app with yarn dlx
74+
# ok: npx-usage-yml
75+
run: yarn dlx create-react-app my-app
76+
77+
- name: Build with npm script
78+
# ok: npx-usage-yml
79+
run: npm run build
80+
81+
- name: Description mentions npx but doesn't use it
82+
# ok: npx-usage-yml
83+
run: echo "This workflow mentions npx but doesn't execute it"
84+
85+
- name: Just npm (not npx)
86+
# ok: npx-usage-yml
87+
run: npm install

0 commit comments

Comments
 (0)