Skip to content

Commit 263582c

Browse files
author
Alvaro Muñoz
committed
feat: Add sanitizers for bash test commands
1 parent f76d4d6 commit 263582c

2 files changed

Lines changed: 86 additions & 1 deletion

File tree

ql/lib/codeql/actions/Bash.qll

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,11 +691,32 @@ module Bash {
691691
// echo "FIELD=${VAR2:-default}" >> $GITHUB_ENV (field, file_write_value)
692692
script.getAnAssignment(var2, value2) and
693693
containsCmdSubstitution(value2, cmd) and
694-
containsParameterExpansion(expr, var2, _, _)
694+
containsParameterExpansion(expr, var2, _, _) and
695+
not varMatchesRegexTest(script, var2, alphaNumericRegex())
695696
)
696697
or
697698
// var reaches the file write directly
698699
// echo "FIELD=$(cmd)" >> $GITHUB_ENV (field, file_write_value)
699700
containsCmdSubstitution(expr, cmd)
700701
}
702+
703+
/**
704+
* Holds if there test command that checks a variable against a regex
705+
* eg: `[[ $VAR =~ ^[a-zA-Z0-9_]+$ ]]`
706+
*/
707+
bindingset[var, regex]
708+
predicate varMatchesRegexTest(BashShellScript script, string var, string regex) {
709+
exists(string lhs, string rhs |
710+
lhs = script.getACommand().regexpCapture(".*\\[\\[\\s*(.*?)\\s*=~\\s*(.*?)\\s*\\]\\].*", 1) and
711+
containsParameterExpansion(lhs, var, _, _) and
712+
rhs = script.getACommand().regexpCapture(".*\\[\\[\\s*(.*?)\\s*=~\\s*(.*?)\\s*\\]\\].*", 2) and
713+
trimQuotes(rhs).regexpMatch(regex)
714+
)
715+
}
716+
717+
/**
718+
* Holds if the given regex is used to match an alphanumeric string
719+
* eg: `^[0-9a-zA-Z]{40}$`, `^[0-9]+$` or `^[a-zA-Z0-9_]+$`
720+
*/
721+
string alphaNumericRegex() { result = "^\\^\\[([09azAZ_-]+)\\](\\+|\\{\\d+\\})\\$$" }
701722
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
on:
2+
workflow_run:
3+
4+
jobs:
5+
test:
6+
runs-on: ubuntu-22.04
7+
if: >
8+
(github.event.workflow_run.event == 'pull_request' ||
9+
github.event.workflow_run.event == 'pull_request_target') &&
10+
github.event.workflow_run.conclusion == 'success'
11+
12+
steps:
13+
- name: 'Download artifact'
14+
uses: actions/github-script@v3.1.0
15+
with:
16+
script: |
17+
var artifacts = await github.actions.listWorkflowRunArtifacts({
18+
owner: context.repo.owner,
19+
repo: context.repo.repo,
20+
run_id: ${{github.event.workflow_run.id }},
21+
});
22+
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
23+
return artifact.name == "doc-build-artifact"
24+
})[0];
25+
var download = await github.actions.downloadArtifact({
26+
owner: context.repo.owner,
27+
repo: context.repo.repo,
28+
artifact_id: matchArtifact.id,
29+
archive_format: 'zip',
30+
});
31+
var fs = require('fs');
32+
fs.writeFileSync('${{steps.setup-env.outputs.current_work_dir}}/doc-build-artifact.zip', Buffer.from(download.data));
33+
34+
- run: |
35+
mkdir build_dir
36+
unzip doc-build-artifact.zip -d build_dir
37+
38+
- name: Get commit_sha & pr_number
39+
id: github-context
40+
run: |
41+
content_commit_sha=$(cat ./build_dir/commit_sha)
42+
if [[ $content_commit_sha =~ ^[0-9a-zA-Z]{40}$ ]]; then
43+
echo "commit_sha=$content_commit_sha" >> $GITHUB_OUTPUT
44+
rm -rf ./build_dir/commit_sha
45+
else
46+
echo "Encountered an invalid commit_sha"
47+
exit 1
48+
fi
49+
50+
content_pr_number=$(cat ./build_dir/pr_number)
51+
if [[ $content_pr_number =~ ^[0-9]+$ ]]; then
52+
echo "pr_number=$content_pr_number" >> $GITHUB_OUTPUT
53+
rm -rf ./build_dir/pr_number
54+
else
55+
echo "Encountered an invalid pr_number"
56+
exit 1
57+
fi
58+
59+
- run: |
60+
echo "hub_docs_url=pr_${{ steps.github-context.outputs.pr_number }}" >> $GITHUB_OUTPUT
61+
62+
- run: |
63+
cd build_dir
64+
doc-builder push --commit_msg "Updated with commit ${{ steps.github-context.outputs.commit_sha }}

0 commit comments

Comments
 (0)