Skip to content

Commit b7be98b

Browse files
authored
Merge pull request #3414 from github/koesie10/verify-valid-sarif
Add check for id property when running variant analysis
2 parents 96b7722 + b168ce7 commit b7be98b

4 files changed

Lines changed: 48 additions & 23 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const SARIF_RESULTS_QUERY_KINDS = [
2+
"problem",
3+
"alert",
4+
"path-problem",
5+
"path-alert",
6+
];
7+
8+
/**
9+
* Returns whether this query kind supports producing SARIF results.
10+
*/
11+
export function isSarifResultsQueryKind(kind: string | undefined): boolean {
12+
if (!kind) {
13+
return false;
14+
}
15+
16+
return SARIF_RESULTS_QUERY_KINDS.includes(kind);
17+
}

extensions/ql-vscode/src/variant-analysis/code-scanning-pack.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { QueryLanguage } from "../common/query-language";
44
import type { CodeQLCliServer } from "../codeql-cli/cli";
55
import type { QlPackDetails } from "./ql-pack-details";
66
import { getQlPackFilePath } from "../common/ql";
7+
import { isSarifResultsQueryKind } from "../common/query-metadata";
78

89
export async function resolveCodeScanningQueryPack(
910
logger: BaseLogger,
@@ -64,10 +65,7 @@ async function filterToOnlyProblemQueries(
6465
const problemQueries: string[] = [];
6566
for (const query of queries) {
6667
const queryMetadata = await cliServer.resolveMetadata(query);
67-
if (
68-
queryMetadata.kind === "problem" ||
69-
queryMetadata.kind === "path-problem"
70-
) {
68+
if (isSarifResultsQueryKind(queryMetadata.kind)) {
7169
problemQueries.push(query);
7270
} else {
7371
void logger.log(`Skipping non-problem query ${query}`);

extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import { tryGetQueryMetadata } from "../codeql-cli/query-metadata";
9696
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
9797
import { findVariantAnalysisQlPackRoot } from "./ql";
9898
import { resolveCodeScanningQueryPack } from "./code-scanning-pack";
99+
import { isSarifResultsQueryKind } from "../common/query-metadata";
99100

100101
const maxRetryCount = 3;
101102

@@ -310,21 +311,6 @@ export class VariantAnalysisManager
310311
message: "Getting credentials",
311312
});
312313

313-
const {
314-
actionBranch,
315-
base64Pack,
316-
repoSelection,
317-
controllerRepo,
318-
queryStartTime,
319-
} = await prepareRemoteQueryRun(
320-
this.cliServer,
321-
this.app.credentials,
322-
qlPackDetails,
323-
progress,
324-
token,
325-
this.dbManager,
326-
);
327-
328314
// For now we get the metadata for the first query in the pack.
329315
// and use that in the submission and query history. In the future
330316
// we'll need to consider how to handle having multiple queries.
@@ -343,6 +329,32 @@ export class VariantAnalysisManager
343329
);
344330
}
345331

332+
// It's not possible to interpret a BQRS file to SARIF without an id property.
333+
if (
334+
queryMetadata?.kind &&
335+
isSarifResultsQueryKind(queryMetadata.kind) &&
336+
!queryMetadata.id
337+
) {
338+
throw new UserCancellationException(
339+
`${firstQueryFile} does not have the required @id property for a ${queryMetadata.kind} query.`,
340+
);
341+
}
342+
343+
const {
344+
actionBranch,
345+
base64Pack,
346+
repoSelection,
347+
controllerRepo,
348+
queryStartTime,
349+
} = await prepareRemoteQueryRun(
350+
this.cliServer,
351+
this.app.credentials,
352+
qlPackDetails,
353+
progress,
354+
token,
355+
this.dbManager,
356+
);
357+
346358
const queryText = await readFile(firstQueryFile, "utf8");
347359

348360
const queries: VariantAnalysisQueries | undefined =

extensions/ql-vscode/src/view/variant-analysis/RepositoriesSearchSortRow.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { RepositoriesSort } from "./RepositoriesSort";
1111
import { RepositoriesFilter } from "./RepositoriesFilter";
1212
import { RepositoriesResultFormat } from "./RepositoriesResultFormat";
1313
import type { ResultFormat } from "../../variant-analysis/shared/variant-analysis-result-format";
14+
import { isSarifResultsQueryKind } from "../../common/query-metadata";
1415

1516
type Props = {
1617
filterSortValue: RepositoriesFilterSortState;
@@ -47,10 +48,7 @@ const RepositoriesResultFormatColumn = styled(RepositoriesResultFormat)`
4748
function showResultFormatColumn(
4849
variantAnalysisQueryKind: string | undefined,
4950
): boolean {
50-
return (
51-
variantAnalysisQueryKind === "problem" ||
52-
variantAnalysisQueryKind === "path-problem"
53-
);
51+
return isSarifResultsQueryKind(variantAnalysisQueryKind);
5452
}
5553

5654
export const RepositoriesSearchSortRow = ({

0 commit comments

Comments
 (0)