Skip to content

Commit 6cb15f0

Browse files
author
Alvaro Muñoz
committed
fix(fn): Apply json wrappers to source regexps
1 parent 27a9bc8 commit 6cb15f0

File tree

6 files changed

+89
-19
lines changed

6 files changed

+89
-19
lines changed

ql/lib/codeql/actions/Ast.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ module Utils {
99
.regexpReplaceAll("\\[\"([a-zA-Z0-9_\\*\\-]+)\"\\]", ".$1")
1010
.regexpReplaceAll("\\s*\\.\\s*", ".")
1111
}
12+
13+
bindingset[regex]
14+
string wrapRegexp(string regex) {
15+
result = ["\\b" + regex + "\\b", "fromJSON\\(" + regex + "\\)", "toJSON\\(" + regex + "\\)"]
16+
}
1217
}
1318

1419
class AstNode instanceof AstNodeImpl {

ql/lib/codeql/actions/ast/internal/Ast.qll

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -813,28 +813,24 @@ abstract class SimpleReferenceExpressionImpl extends ExpressionImpl {
813813
}
814814

815815
private string stepsCtxRegex() {
816-
result = wrapRegexp("steps\\.([A-Za-z0-9_-]+)\\.outputs\\.([A-Za-z0-9_-]+)")
816+
result = Utils::wrapRegexp("steps\\.([A-Za-z0-9_-]+)\\.outputs\\.([A-Za-z0-9_-]+)")
817817
}
818818

819819
private string needsCtxRegex() {
820-
result = wrapRegexp("needs\\.([A-Za-z0-9_-]+)\\.outputs\\.([A-Za-z0-9_-]+)")
820+
result = Utils::wrapRegexp("needs\\.([A-Za-z0-9_-]+)\\.outputs\\.([A-Za-z0-9_-]+)")
821821
}
822822

823823
private string jobsCtxRegex() {
824-
result = wrapRegexp("jobs\\.([A-Za-z0-9_-]+)\\.outputs\\.([A-Za-z0-9_-]+)")
824+
result = Utils::wrapRegexp("jobs\\.([A-Za-z0-9_-]+)\\.outputs\\.([A-Za-z0-9_-]+)")
825825
}
826826

827-
private string envCtxRegex() { result = wrapRegexp("env\\.([A-Za-z0-9_-]+)") }
827+
private string envCtxRegex() { result = Utils::wrapRegexp("env\\.([A-Za-z0-9_-]+)") }
828828

829-
private string matrixCtxRegex() { result = wrapRegexp("matrix\\.([A-Za-z0-9_-]+)") }
829+
private string matrixCtxRegex() { result = Utils::wrapRegexp("matrix\\.([A-Za-z0-9_-]+)") }
830830

831831
private string inputsCtxRegex() {
832-
result = wrapRegexp(["inputs\\.([A-Za-z0-9_-]+)", "github\\.event\\.inputs\\.([A-Za-z0-9_-]+)"])
833-
}
834-
835-
bindingset[regex]
836-
private string wrapRegexp(string regex) {
837-
result = ["\\b" + regex + "\\b", "fromJSON\\(" + regex + "\\)", "toJSON\\(" + regex + "\\)"]
832+
result =
833+
Utils::wrapRegexp(["inputs\\.([A-Za-z0-9_-]+)", "github\\.event\\.inputs\\.([A-Za-z0-9_-]+)"])
838834
}
839835

840836
/**

ql/lib/codeql/actions/dataflow/FlowSources.qll

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private predicate isExternalUserControlledIssue(string context) {
2828
exists(string reg |
2929
reg = ["\\bgithub\\.event\\.issue\\.title\\b", "\\bgithub\\.event\\.issue\\.body\\b"]
3030
|
31-
Utils::normalizeExpr(context).regexpMatch(reg)
31+
Utils::normalizeExpr(context).regexpMatch(Utils::wrapRegexp(reg))
3232
)
3333
}
3434

@@ -45,18 +45,20 @@ private predicate isExternalUserControlledPullRequest(string context) {
4545
"\\bgithub\\.event\\.pull_request\\.head\\.ref\\b", "\\bgithub\\.head_ref\\b"
4646
]
4747
|
48-
Utils::normalizeExpr(context).regexpMatch(reg)
48+
Utils::normalizeExpr(context).regexpMatch(Utils::wrapRegexp(reg))
4949
)
5050
}
5151

5252
bindingset[context]
5353
private predicate isExternalUserControlledReview(string context) {
54-
Utils::normalizeExpr(context).regexpMatch("\\bgithub\\.event\\.review\\.body\\b")
54+
Utils::normalizeExpr(context)
55+
.regexpMatch(Utils::wrapRegexp("\\bgithub\\.event\\.review\\.body\\b"))
5556
}
5657

5758
bindingset[context]
5859
private predicate isExternalUserControlledComment(string context) {
59-
Utils::normalizeExpr(context).regexpMatch("\\bgithub\\.event\\.comment\\.body\\b")
60+
Utils::normalizeExpr(context)
61+
.regexpMatch(Utils::wrapRegexp("\\bgithub\\.event\\.comment\\.body\\b"))
6062
}
6163

6264
bindingset[context]
@@ -68,7 +70,7 @@ private predicate isExternalUserControlledGollum(string context) {
6870
"\\bgithub\\.event\\.pages\\[[0-9]+\\]\\.title\\b"
6971
]
7072
|
71-
Utils::normalizeExpr(context).regexpMatch(reg)
73+
Utils::normalizeExpr(context).regexpMatch(Utils::wrapRegexp(reg))
7274
)
7375
}
7476

@@ -89,7 +91,7 @@ private predicate isExternalUserControlledCommit(string context) {
8991
"\\bgithub\\.event\\.commits\\[[0-9]+\\]\\.committer\\.name\\b",
9092
]
9193
|
92-
Utils::normalizeExpr(context).regexpMatch(reg)
94+
Utils::normalizeExpr(context).regexpMatch(Utils::wrapRegexp(reg))
9395
)
9496
}
9597

@@ -98,7 +100,7 @@ private predicate isExternalUserControlledDiscussion(string context) {
98100
exists(string reg |
99101
reg = ["\\bgithub\\.event\\.discussion\\.title\\b", "\\bgithub\\.event\\.discussion\\.body\\b"]
100102
|
101-
Utils::normalizeExpr(context).regexpMatch(reg)
103+
Utils::normalizeExpr(context).regexpMatch(Utils::wrapRegexp(reg))
102104
)
103105
}
104106

@@ -118,7 +120,7 @@ private predicate isExternalUserControlledWorkflowRun(string context) {
118120
"\\bgithub\\.event\\.workflow_run\\.head_commit\\.committer\\.name\\b",
119121
]
120122
|
121-
Utils::normalizeExpr(context).regexpMatch(reg)
123+
Utils::normalizeExpr(context).regexpMatch(Utils::wrapRegexp(reg))
122124
)
123125
}
124126

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Issue Comment Created
2+
3+
on:
4+
issue_comment:
5+
types:
6+
- created
7+
8+
jobs:
9+
jira:
10+
runs-on: ubuntu-latest
11+
if: ${{ github.event.comment.body == '/jira ticket' }}
12+
steps:
13+
- run: echo ${{ github.event.comment.body }}
14+
15+
- name: Login
16+
uses: atlassian/gajira-login@v3
17+
env:
18+
JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }}
19+
JIRA_USER_EMAIL: ${{ secrets.JIRA_USER_EMAIL }}
20+
JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}
21+
22+
- name: SearchParam
23+
run: echo 'summary ~ ${{ toJSON(github.event.issue.title)}} AND project=${{ secrets.JIRA_PROJECT }}'
24+
25+
- name: Search
26+
id: search
27+
uses: tomhjp/[email protected]
28+
with:
29+
jql: 'summary ~ ${{ toJSON(github.event.issue.title)}} AND project=${{ secrets.JIRA_PROJECT }}'
30+
31+
- name: Log
32+
run: echo "Found issue ${{ steps.search.outputs.issue }}"
33+
34+
- name: Create
35+
id: create
36+
if: steps.search.outputs.issue == ''
37+
uses: atlassian/gajira-create@v3
38+
with:
39+
project: ${{ secrets.JIRA_PROJECT }}
40+
issuetype: Task
41+
summary: '${{ github.event.repository.name }}: ${{ github.event.issue.title }}'
42+
description: |
43+
*Issue Link:* ${{ github.event.issue.html_url }}
44+
45+
${{ github.event.issue.body }}
46+
fields: '{"customfield_10006": ${{ toJSON(secrets.JIRA_EPIC_TICKET) }}, "customfield_17401":{"value":${{ toJSON( secrets.JIRA_LAYER_CAKE )}}}}'
47+
48+
- name: Add Comment
49+
if: steps.search.outputs.issue == '' && steps.create.outputs.issue != ''
50+
uses: actions/github-script@v6
51+
with:
52+
github-token: ${{secrets.GITHUB_TOKEN}}
53+
script: |
54+
github.rest.issues.createComment({
55+
issue_number: context.issue.number,
56+
owner: context.repo.owner,
57+
repo: context.repo.repo,
58+
body: '👋 Thanks, Jira [${{steps.create.outputs.issue}}] ticket created.'
59+
})

ql/test/query-tests/Security/CWE-094/CodeInjection.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ nodes
131131
| .github/workflows/issues.yaml:17:19:17:36 | env.job_env | semmle.label | env.job_env |
132132
| .github/workflows/issues.yaml:18:19:18:37 | env.step_env | semmle.label | env.step_env |
133133
| .github/workflows/issues.yaml:20:20:20:50 | github.event.issue.title | semmle.label | github.event.issue.title |
134+
| .github/workflows/json_wrap.yml:13:20:13:51 | github.event.comment.body | semmle.label | github.event.comment.body |
135+
| .github/workflows/json_wrap.yml:23:31:23:68 | toJSON(github.event.issue.title) | semmle.label | toJSON(github.event.issue.title) |
134136
| .github/workflows/pull_request_review.yml:7:19:7:56 | github.event.pull_request.title | semmle.label | github.event.pull_request.title |
135137
| .github/workflows/pull_request_review.yml:8:19:8:55 | github.event.pull_request.body | semmle.label | github.event.pull_request.body |
136138
| .github/workflows/pull_request_review.yml:9:19:9:61 | github.event.pull_request.head.label | semmle.label | github.event.pull_request.head.label |
@@ -234,6 +236,8 @@ subpaths
234236
| .github/workflows/issues.yaml:15:19:15:39 | env.global_env | .github/workflows/issues.yaml:4:16:4:46 | github.event.issue.title | .github/workflows/issues.yaml:15:19:15:39 | env.global_env | Potential code injection in $@, which may be controlled by an external user. | .github/workflows/issues.yaml:15:19:15:39 | env.global_env | ${{ env.global_env }} |
235237
| .github/workflows/issues.yaml:17:19:17:36 | env.job_env | .github/workflows/issues.yaml:10:17:10:47 | github.event.issue.title | .github/workflows/issues.yaml:17:19:17:36 | env.job_env | Potential code injection in $@, which may be controlled by an external user. | .github/workflows/issues.yaml:17:19:17:36 | env.job_env | ${{ env.job_env }} |
236238
| .github/workflows/issues.yaml:18:19:18:37 | env.step_env | .github/workflows/issues.yaml:20:20:20:50 | github.event.issue.title | .github/workflows/issues.yaml:18:19:18:37 | env.step_env | Potential code injection in $@, which may be controlled by an external user. | .github/workflows/issues.yaml:18:19:18:37 | env.step_env | ${{ env.step_env }} |
239+
| .github/workflows/json_wrap.yml:13:20:13:51 | github.event.comment.body | .github/workflows/json_wrap.yml:13:20:13:51 | github.event.comment.body | .github/workflows/json_wrap.yml:13:20:13:51 | github.event.comment.body | Potential code injection in $@, which may be controlled by an external user. | .github/workflows/json_wrap.yml:13:20:13:51 | github.event.comment.body | ${{ github.event.comment.body }} |
240+
| .github/workflows/json_wrap.yml:23:31:23:68 | toJSON(github.event.issue.title) | .github/workflows/json_wrap.yml:23:31:23:68 | toJSON(github.event.issue.title) | .github/workflows/json_wrap.yml:23:31:23:68 | toJSON(github.event.issue.title) | Potential code injection in $@, which may be controlled by an external user. | .github/workflows/json_wrap.yml:23:31:23:68 | toJSON(github.event.issue.title) | ${{ toJSON(github.event.issue.title)}} |
237241
| .github/workflows/pull_request_review.yml:7:19:7:56 | github.event.pull_request.title | .github/workflows/pull_request_review.yml:7:19:7:56 | github.event.pull_request.title | .github/workflows/pull_request_review.yml:7:19:7:56 | github.event.pull_request.title | Potential code injection in $@, which may be controlled by an external user. | .github/workflows/pull_request_review.yml:7:19:7:56 | github.event.pull_request.title | ${{ github.event.pull_request.title }} |
238242
| .github/workflows/pull_request_review.yml:8:19:8:55 | github.event.pull_request.body | .github/workflows/pull_request_review.yml:8:19:8:55 | github.event.pull_request.body | .github/workflows/pull_request_review.yml:8:19:8:55 | github.event.pull_request.body | Potential code injection in $@, which may be controlled by an external user. | .github/workflows/pull_request_review.yml:8:19:8:55 | github.event.pull_request.body | ${{ github.event.pull_request.body }} |
239243
| .github/workflows/pull_request_review.yml:9:19:9:61 | github.event.pull_request.head.label | .github/workflows/pull_request_review.yml:9:19:9:61 | github.event.pull_request.head.label | .github/workflows/pull_request_review.yml:9:19:9:61 | github.event.pull_request.head.label | Potential code injection in $@, which may be controlled by an external user. | .github/workflows/pull_request_review.yml:9:19:9:61 | github.event.pull_request.head.label | ${{ github.event.pull_request.head.label }} |

ql/test/query-tests/Security/CWE-094/PrivilegedCodeInjection.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ nodes
131131
| .github/workflows/issues.yaml:17:19:17:36 | env.job_env | semmle.label | env.job_env |
132132
| .github/workflows/issues.yaml:18:19:18:37 | env.step_env | semmle.label | env.step_env |
133133
| .github/workflows/issues.yaml:20:20:20:50 | github.event.issue.title | semmle.label | github.event.issue.title |
134+
| .github/workflows/json_wrap.yml:13:20:13:51 | github.event.comment.body | semmle.label | github.event.comment.body |
135+
| .github/workflows/json_wrap.yml:23:31:23:68 | toJSON(github.event.issue.title) | semmle.label | toJSON(github.event.issue.title) |
134136
| .github/workflows/pull_request_review.yml:7:19:7:56 | github.event.pull_request.title | semmle.label | github.event.pull_request.title |
135137
| .github/workflows/pull_request_review.yml:8:19:8:55 | github.event.pull_request.body | semmle.label | github.event.pull_request.body |
136138
| .github/workflows/pull_request_review.yml:9:19:9:61 | github.event.pull_request.head.label | semmle.label | github.event.pull_request.head.label |
@@ -229,6 +231,8 @@ subpaths
229231
| .github/workflows/issues.yaml:15:19:15:39 | env.global_env | .github/workflows/issues.yaml:4:16:4:46 | github.event.issue.title | .github/workflows/issues.yaml:15:19:15:39 | env.global_env | Potential privileged code injection in $@, which may be controlled by an external user. | .github/workflows/issues.yaml:15:19:15:39 | env.global_env | ${{ env.global_env }} |
230232
| .github/workflows/issues.yaml:17:19:17:36 | env.job_env | .github/workflows/issues.yaml:10:17:10:47 | github.event.issue.title | .github/workflows/issues.yaml:17:19:17:36 | env.job_env | Potential privileged code injection in $@, which may be controlled by an external user. | .github/workflows/issues.yaml:17:19:17:36 | env.job_env | ${{ env.job_env }} |
231233
| .github/workflows/issues.yaml:18:19:18:37 | env.step_env | .github/workflows/issues.yaml:20:20:20:50 | github.event.issue.title | .github/workflows/issues.yaml:18:19:18:37 | env.step_env | Potential privileged code injection in $@, which may be controlled by an external user. | .github/workflows/issues.yaml:18:19:18:37 | env.step_env | ${{ env.step_env }} |
234+
| .github/workflows/json_wrap.yml:13:20:13:51 | github.event.comment.body | .github/workflows/json_wrap.yml:13:20:13:51 | github.event.comment.body | .github/workflows/json_wrap.yml:13:20:13:51 | github.event.comment.body | Potential privileged code injection in $@, which may be controlled by an external user. | .github/workflows/json_wrap.yml:13:20:13:51 | github.event.comment.body | ${{ github.event.comment.body }} |
235+
| .github/workflows/json_wrap.yml:23:31:23:68 | toJSON(github.event.issue.title) | .github/workflows/json_wrap.yml:23:31:23:68 | toJSON(github.event.issue.title) | .github/workflows/json_wrap.yml:23:31:23:68 | toJSON(github.event.issue.title) | Potential privileged code injection in $@, which may be controlled by an external user. | .github/workflows/json_wrap.yml:23:31:23:68 | toJSON(github.event.issue.title) | ${{ toJSON(github.event.issue.title)}} |
232236
| .github/workflows/pull_request_review.yml:7:19:7:56 | github.event.pull_request.title | .github/workflows/pull_request_review.yml:7:19:7:56 | github.event.pull_request.title | .github/workflows/pull_request_review.yml:7:19:7:56 | github.event.pull_request.title | Potential privileged code injection in $@, which may be controlled by an external user. | .github/workflows/pull_request_review.yml:7:19:7:56 | github.event.pull_request.title | ${{ github.event.pull_request.title }} |
233237
| .github/workflows/pull_request_review.yml:8:19:8:55 | github.event.pull_request.body | .github/workflows/pull_request_review.yml:8:19:8:55 | github.event.pull_request.body | .github/workflows/pull_request_review.yml:8:19:8:55 | github.event.pull_request.body | Potential privileged code injection in $@, which may be controlled by an external user. | .github/workflows/pull_request_review.yml:8:19:8:55 | github.event.pull_request.body | ${{ github.event.pull_request.body }} |
234238
| .github/workflows/pull_request_review.yml:9:19:9:61 | github.event.pull_request.head.label | .github/workflows/pull_request_review.yml:9:19:9:61 | github.event.pull_request.head.label | .github/workflows/pull_request_review.yml:9:19:9:61 | github.event.pull_request.head.label | Potential privileged code injection in $@, which may be controlled by an external user. | .github/workflows/pull_request_review.yml:9:19:9:61 | github.event.pull_request.head.label | ${{ github.event.pull_request.head.label }} |

0 commit comments

Comments
 (0)