Skip to content

Commit ed61eb0

Browse files
authored
Deal with analysis messages that have links to locations (#1195)
1 parent 50d495b commit ed61eb0

7 files changed

Lines changed: 322 additions & 257 deletions

File tree

extensions/ql-vscode/src/pure/sarif-utils.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,35 +127,49 @@ export function parseSarifLocation(
127127
userVisibleFile
128128
} as ParsedSarifLocation;
129129
} else {
130-
const region = physicalLocation.region;
131-
// We assume that the SARIF we're given always has startLine
132-
// This is not mandated by the SARIF spec, but should be true of
133-
// SARIF output by our own tools.
134-
const startLine = region.startLine!;
135-
136-
// These defaults are from SARIF 2.1.0 spec, section 3.30.2, "Text Regions"
137-
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/sarif-v2.1.0-cs01.html#_Ref493492556
138-
const endLine = region.endLine === undefined ? startLine : region.endLine;
139-
const startColumn = region.startColumn === undefined ? 1 : region.startColumn;
140-
141-
// We also assume that our tools will always supply `endColumn` field, which is
142-
// fortunate, since the SARIF spec says that it defaults to the end of the line, whose
143-
// length we don't know at this point in the code.
144-
//
145-
// It is off by one with respect to the way vscode counts columns in selections.
146-
const endColumn = region.endColumn! - 1;
130+
const region = parseSarifRegion(physicalLocation.region);
147131

148132
return {
149133
uri: effectiveLocation,
150134
userVisibleFile,
151-
startLine,
152-
startColumn,
153-
endLine,
154-
endColumn,
135+
...region
155136
};
156137
}
157138
}
158139

140+
export function parseSarifRegion(
141+
region: Sarif.Region
142+
): {
143+
startLine: number,
144+
endLine: number,
145+
startColumn: number,
146+
endColumn: number
147+
} {
148+
// The SARIF we're given should have a startLine, but we
149+
// fall back to 1, just in case something has gone wrong.
150+
const startLine = region.startLine ?? 1;
151+
152+
// These defaults are from SARIF 2.1.0 spec, section 3.30.2, "Text Regions"
153+
// https://docs.oasis-open.org/sarif/sarif/v2.1.0/cs01/sarif-v2.1.0-cs01.html#_Ref493492556
154+
const endLine = region.endLine === undefined ? startLine : region.endLine;
155+
const startColumn = region.startColumn === undefined ? 1 : region.startColumn;
156+
157+
// Our tools should always supply `endColumn` field, which is fortunate, since
158+
// the SARIF spec says that it defaults to the end of the line, whose
159+
// length we don't know at this point in the code. We fall back to 1,
160+
// just in case something has gone wrong.
161+
//
162+
// It is off by one with respect to the way vscode counts columns in selections.
163+
const endColumn = (region.endColumn ?? 1) - 1;
164+
165+
return {
166+
startLine,
167+
startColumn,
168+
endLine,
169+
endColumn
170+
};
171+
}
172+
159173
export function isNoLocation(loc: ParsedSarifLocation): loc is NoLocation {
160174
return 'hint' in loc;
161175
}

extensions/ql-vscode/src/remote-queries/sample-data.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,35 @@ export const sampleRemoteQueryResult: RemoteQueryResult = {
101101

102102
const createAnalysisInterpretedResults = (n: number) => Array(n).fill(
103103
{
104-
message: 'This shell command depends on an uncontrolled [absolute path](1).',
104+
message: {
105+
tokens: [
106+
{
107+
t: 'text',
108+
text: 'This shell command depends on an uncontrolled '
109+
},
110+
{
111+
t: 'location',
112+
text: 'absolute path',
113+
location: {
114+
filePath: 'npm-packages/meteor-installer/config.js',
115+
codeSnippet: {
116+
startLine: 33,
117+
endLine: 37,
118+
text: '\nconst meteorLocalFolder = \'.meteor\';\nconst meteorPath = path.resolve(rootPath, meteorLocalFolder);\n\nmodule.exports = {\n'
119+
},
120+
highlightedRegion: {
121+
startLine: 35,
122+
startColumn: 20,
123+
endColumn: 61
124+
}
125+
}
126+
},
127+
{
128+
t: 'text',
129+
text: '.'
130+
}
131+
]
132+
},
105133
shortDescription: 'Shell command built from environment values',
106134
severity: 'Error',
107135
filePath: 'npm-packages/meteor-installer/config.js',

0 commit comments

Comments
 (0)