Skip to content

Commit 0ce6420

Browse files
committed
Validate CODEQL_ACTION_CSRA_ASSESSMENT_ID value
1 parent 2adcb64 commit 0ce6420

File tree

7 files changed

+125
-20
lines changed

7 files changed

+125
-20
lines changed

lib/analyze-action.js

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action-post.js

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-sarif-action.js

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/analyses.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import {
88
AnalysisKind,
99
CodeScanning,
1010
compatibilityMatrix,
11+
CSRA,
1112
getAnalysisConfig,
1213
getAnalysisKinds,
1314
parseAnalysisKinds,
1415
supportedAnalysisKinds,
1516
} from "./analyses";
17+
import { EnvVar } from "./environment";
1618
import { getRunnerLogger } from "./logging";
1719
import { setupTests } from "./testing-utils";
20+
import { AssessmentPayload } from "./upload-lib/types";
1821
import { ConfigurationError } from "./util";
1922

2023
setupTests(test);
@@ -118,3 +121,61 @@ test("Code Scanning configuration does not accept other SARIF extensions", (t) =
118121
t.false(CodeScanning.sarifPredicate(sarifPath));
119122
}
120123
});
124+
125+
test("CSRA configuration transforms SARIF upload payload", (t) => {
126+
process.env[EnvVar.CSRA_ASSESSMENT_ID] = "1";
127+
const payload = CSRA.transformPayload({
128+
commit_oid: "abc",
129+
sarif: "sarif",
130+
ref: "ref",
131+
workflow_run_attempt: 1,
132+
workflow_run_id: 1,
133+
checkout_uri: "uri",
134+
tool_names: [],
135+
}) as AssessmentPayload;
136+
137+
const expected: AssessmentPayload = { sarif: "sarif", assessment_id: 1 };
138+
t.deepEqual(expected, payload);
139+
});
140+
141+
test("CSRA configuration throws for negative assessment IDs", (t) => {
142+
process.env[EnvVar.CSRA_ASSESSMENT_ID] = "-1";
143+
t.throws(
144+
() =>
145+
CSRA.transformPayload({
146+
commit_oid: "abc",
147+
sarif: "sarif",
148+
ref: "ref",
149+
workflow_run_attempt: 1,
150+
workflow_run_id: 1,
151+
checkout_uri: "uri",
152+
tool_names: [],
153+
}),
154+
{
155+
instanceOf: Error,
156+
message: (msg) =>
157+
msg.startsWith(`${EnvVar.CSRA_ASSESSMENT_ID} must not be negative: `),
158+
},
159+
);
160+
});
161+
162+
test("CSRA configuration throws for invalid IDs", (t) => {
163+
process.env[EnvVar.CSRA_ASSESSMENT_ID] = "foo";
164+
t.throws(
165+
() =>
166+
CSRA.transformPayload({
167+
commit_oid: "abc",
168+
sarif: "sarif",
169+
ref: "ref",
170+
workflow_run_attempt: 1,
171+
workflow_run_id: 1,
172+
checkout_uri: "uri",
173+
tool_names: [],
174+
}),
175+
{
176+
instanceOf: Error,
177+
message: (msg) =>
178+
msg.startsWith(`${EnvVar.CSRA_ASSESSMENT_ID} must not be NaN: `),
179+
},
180+
);
181+
});

src/analyses.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
getOptionalInput,
44
getRequiredInput,
55
} from "./actions-util";
6+
import { EnvVar } from "./environment";
67
import { Logger } from "./logging";
78
import {
89
AssessmentPayload,
@@ -187,10 +188,18 @@ export const CodeQuality: AnalysisConfig = {
187188
* @param payload The base payload.
188189
*/
189190
function addAssessmentId(payload: UploadPayload): AssessmentPayload {
190-
const assessmentId = parseInt(
191-
getRequiredEnvParam("CODEQL_ACTION_CSRA_ASSESSMENT_ID"),
192-
10,
193-
);
191+
const rawAssessmentId = getRequiredEnvParam(EnvVar.CSRA_ASSESSMENT_ID);
192+
const assessmentId = parseInt(rawAssessmentId, 10);
193+
if (Number.isNaN(assessmentId)) {
194+
throw new Error(
195+
`${EnvVar.CSRA_ASSESSMENT_ID} must not be NaN: ${rawAssessmentId}`,
196+
);
197+
}
198+
if (assessmentId < 0) {
199+
throw new Error(
200+
`${EnvVar.CSRA_ASSESSMENT_ID} must not be negative: ${rawAssessmentId}`,
201+
);
202+
}
194203
return { sarif: payload.sarif, assessment_id: assessmentId };
195204
}
196205

src/environment.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,7 @@ export enum EnvVar {
141141
* `getAnalysisKey`, but can also be set manually for testing and non-standard applications.
142142
*/
143143
ANALYSIS_KEY = "CODEQL_ACTION_ANALYSIS_KEY",
144+
145+
/** Used by CSRA to communicate the assessment ID to the CodeQL Action. */
146+
CSRA_ASSESSMENT_ID = "CODEQL_ACTION_CSRA_ASSESSMENT_ID",
144147
}

0 commit comments

Comments
 (0)