Skip to content

Commit b363f77

Browse files
committed
Tidy up how we display paths
1 parent f55f46f commit b363f77

3 files changed

Lines changed: 51 additions & 65 deletions

File tree

extensions/ql-vscode/src/remote-queries/remote-queries-markdown-generation.ts

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,18 @@ function generateMarkdownForInterpretedResult(interpretedResult: AnalysisAlert,
8989
...generateMarkdownForCodeSnippet(codeSnippet, language, highlightedRegion),
9090
);
9191
}
92-
const alertMessageLines = buildMarkdownAlertMessage(interpretedResult, language);
93-
lines.push(...alertMessageLines);
92+
const alertMessage = generateMarkdownForAlertMessage(interpretedResult);
93+
lines.push(alertMessage, '');
94+
95+
// If available, show paths
96+
const hasPathResults = interpretedResult.codeFlows.length > 0;
97+
if (hasPathResults) {
98+
const pathLines = generateMarkdownForPathResults(interpretedResult, language);
99+
lines.push(...pathLines);
100+
}
94101

95102
// Padding between results
96103
lines.push(
97-
'',
98104
'----------------------------------------',
99105
'',
100106
);
@@ -142,50 +148,36 @@ function highlightCodeLines(
142148
return `${partiallyHighlightedLine.plainSection1}<strong>${partiallyHighlightedLine.highlightedSection}</strong>${partiallyHighlightedLine.plainSection2}`;
143149
}
144150

145-
function buildMarkdownAlertMessage(
146-
interpretedResult: AnalysisAlert,
147-
language: string
148-
): string[] {
149-
const hasPathResults = interpretedResult.codeFlows.length > 0;
150-
if (hasPathResults) {
151-
// For path-problem queries, the "alert message" is an expandable section containing the path results.
152-
return buildMarkdownPathResults(interpretedResult, language);
153-
} else {
154-
let alertMessage = '';
155-
// For regular problem queries (no paths), the alert message is just a message
156-
// containing a link to the affected file.
157-
for (const token of interpretedResult.message.tokens) {
158-
if (token.t === 'text') {
159-
alertMessage += token.text;
160-
} else if (token.t === 'location') {
161-
alertMessage += createMarkdownRemoteFileRef(
162-
token.location.fileLink,
163-
token.location.highlightedRegion?.startLine,
164-
token.location.highlightedRegion?.endLine,
165-
token.text
166-
);
167-
}
151+
function generateMarkdownForAlertMessage(
152+
interpretedResult: AnalysisAlert
153+
): string {
154+
let alertMessage = '';
155+
for (const token of interpretedResult.message.tokens) {
156+
if (token.t === 'text') {
157+
alertMessage += token.text;
158+
} else if (token.t === 'location') {
159+
alertMessage += createMarkdownRemoteFileRef(
160+
token.location.fileLink,
161+
token.location.highlightedRegion?.startLine,
162+
token.location.highlightedRegion?.endLine,
163+
token.text
164+
);
168165
}
169-
// Italicize the alert message
170-
return [`*${alertMessage}*`];
171166
}
167+
// Italicize the alert message
168+
return `*${alertMessage}*`;
172169
}
173170

174-
function buildMarkdownPathResults(
171+
function generateMarkdownForPathResults(
175172
interpretedResult: AnalysisAlert,
176173
language: string
177174
): MarkdownFile {
178-
let alertMessage = '';
179-
for (const token of interpretedResult.message.tokens) {
180-
alertMessage += token.text;
181-
}
182175
const pathLines: MarkdownFile = [];
183-
pathLines.push('#### Paths', '');
184176
for (const codeFlow of interpretedResult.codeFlows) {
185177
const stepCount = codeFlow.threadFlows.length;
186-
pathLines.push(`Path with ${stepCount} steps`);
187-
let index = 1;
188-
for (const threadFlow of codeFlow.threadFlows) {
178+
pathLines.push(`#### Path with ${stepCount} steps`);
179+
for (let i = 0; i < codeFlow.threadFlows.length; i++) {
180+
const threadFlow = codeFlow.threadFlows[i];
189181
const link = createMarkdownRemoteFileRef(
190182
threadFlow.fileLink,
191183
threadFlow.highlightedRegion?.startLine,
@@ -198,11 +190,10 @@ function buildMarkdownPathResults(
198190
);
199191
// Indent the snippet to fit with the numbered list.
200192
const codeSnippetIndented = codeSnippet.map((line) => ` ${line}`);
201-
pathLines.push(`${index}. ${link}`, ...codeSnippetIndented);
202-
index++;
193+
pathLines.push(`${i + 1}. ${link}`, ...codeSnippetIndented);
203194
}
204195
}
205-
return buildExpandableMarkdownSection(`<i>${alertMessage}</i>`, pathLines);
196+
return buildExpandableMarkdownSection('Show paths', pathLines);
206197
}
207198

208199
/**

extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/data/interpreted-results/path-problem/results-repo1.md

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
}
99
</code></pre>
1010

11-
<details>
12-
<summary><i>This shell command depends on an uncontrolled absolute path.</i></summary>
11+
*This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/src/Security/CWE-078/examples/shell-command-injection-from-environment.js#L4-L4).*
1312

14-
#### Paths
13+
<details>
14+
<summary>Show paths</summary>
1515

16-
Path with 5 steps
16+
#### Path with 5 steps
1717
1. [javascript/ql/src/Security/CWE-078/examples/shell-command-injection-from-environment.js](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/src/Security/CWE-078/examples/shell-command-injection-from-environment.js#L4-L4)
1818
<pre><code class="javascript"> path = require("path");
1919
function cleanupTemp() {
@@ -56,7 +56,6 @@ Path with 5 steps
5656

5757
</details>
5858

59-
6059
----------------------------------------
6160

6261
[javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L6-L6)
@@ -68,12 +67,12 @@ Path with 5 steps
6867
execa.shell('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
6968
</code></pre>
7069

71-
<details>
72-
<summary><i>This shell command depends on an uncontrolled absolute path.</i></summary>
70+
*This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L6-L6).*
7371

74-
#### Paths
72+
<details>
73+
<summary>Show paths</summary>
7574

76-
Path with 3 steps
75+
#### Path with 3 steps
7776
1. [javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L6-L6)
7877
<pre><code class="javascript">(function() {
7978
cp.execFileSync('rm', ['-rf', path.join(__dirname, "temp")]); // GOOD
@@ -101,7 +100,6 @@ Path with 3 steps
101100

102101
</details>
103102

104-
105103
----------------------------------------
106104

107105
[javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L8-L8)
@@ -113,12 +111,12 @@ Path with 3 steps
113111

114112
</code></pre>
115113

116-
<details>
117-
<summary><i>This shell command depends on an uncontrolled absolute path.</i></summary>
114+
*This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L8-L8).*
118115

119-
#### Paths
116+
<details>
117+
<summary>Show paths</summary>
120118

121-
Path with 3 steps
119+
#### Path with 3 steps
122120
1. [javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L8-L8)
123121
<pre><code class="javascript"> cp.execSync('rm -rf ' + path.join(__dirname, "temp")); // BAD
124122

@@ -146,7 +144,6 @@ Path with 3 steps
146144

147145
</details>
148146

149-
150147
----------------------------------------
151148

152149
[javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L9-L9)
@@ -158,12 +155,12 @@ Path with 3 steps
158155
const safe = "\"" + path.join(__dirname, "temp") + "\"";
159156
</code></pre>
160157

161-
<details>
162-
<summary><i>This shell command depends on an uncontrolled absolute path.</i></summary>
158+
*This shell command depends on an uncontrolled [absolute path](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L9-L9).*
163159

164-
#### Paths
160+
<details>
161+
<summary>Show paths</summary>
165162

166-
Path with 3 steps
163+
#### Path with 3 steps
167164
1. [javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js](https://github.com/github/codeql/blob/48015e5a2e6202131f2d1062cc066dc33ed69a9b/javascript/ql/test/query-tests/Security/CWE-078/tst_shell-command-injection-from-environment.js#L9-L9)
168165
<pre><code class="javascript">
169166
execa.shell('rm -rf ' + path.join(__dirname, "temp")); // NOT OK
@@ -191,5 +188,4 @@ Path with 3 steps
191188

192189
</details>
193190

194-
195191
----------------------------------------

extensions/ql-vscode/test/pure-tests/remote-queries/markdown-generation/data/interpreted-results/path-problem/results-repo2.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
}
1010
</code></pre>
1111

12-
<details>
13-
<summary><i>This shell command depends on an uncontrolled absolute path.</i></summary>
12+
*This shell command depends on an uncontrolled [absolute path](https://github.com/meteor/meteor/blob/73b538fe201cbfe89dd0c709689023f9b3eab1ec/npm-packages/meteor-installer/config.js#L39-L39).*
1413

15-
#### Paths
14+
<details>
15+
<summary>Show paths</summary>
1616

17-
Path with 7 steps
17+
#### Path with 7 steps
1818
1. [npm-packages/meteor-installer/config.js](https://github.com/meteor/meteor/blob/73b538fe201cbfe89dd0c709689023f9b3eab1ec/npm-packages/meteor-installer/config.js#L39-L39)
1919
<pre><code class="javascript">
2020
const meteorLocalFolder = '.meteor';
@@ -86,5 +86,4 @@ Path with 7 steps
8686

8787
</details>
8888

89-
9089
----------------------------------------

0 commit comments

Comments
 (0)