Skip to content

Commit

Permalink
fix: show most severe vulnerability action is displayed (#402)
Browse files Browse the repository at this point in the history
Signed-off-by: Bastian Doetsch <[email protected]>
  • Loading branch information
bastiandoetsch authored Nov 29, 2023
1 parent 274027a commit 44c0e41
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
14 changes: 11 additions & 3 deletions src/snyk/snykOss/providers/ossCodeActionsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ICodeActionAdapter, ICodeActionKindAdapter } from '../../common/vscode/
import { IVSCodeLanguages } from '../../common/vscode/languages';
import { CodeActionContext } from '../../common/vscode/types';
import { DIAGNOSTICS_OSS_COLLECTION_NAME_LS } from '../../snykCode/constants/analysis';
import { getOssIssueCommandArg } from './ossIssueCommandHelper';

export class OssCodeActionsProvider extends CodeActionsProvider<OssIssueData> {
constructor(
Expand Down Expand Up @@ -38,7 +39,7 @@ export class OssCodeActionsProvider extends CodeActionsProvider<OssIssueData> {
return;
}

const mostSevereVulnerability = this.getMostSevereVulnerability(vulnerabilities);
const mostSevereVulnerability = this.getMostSevereVulnerability(vulnerabilities, folderPath);
if (!mostSevereVulnerability) {
return;
}
Expand Down Expand Up @@ -142,7 +143,10 @@ export class OssCodeActionsProvider extends CodeActionsProvider<OssIssueData> {
return vulnerabilities;
}

private getMostSevereVulnerability(vulnerabilities: Issue<OssIssueData>[]): Issue<OssIssueData> | undefined {
private getMostSevereVulnerability(
vulnerabilities: Issue<OssIssueData>[],
folderPath: string,
): Issue<OssIssueData> | undefined {
// iterate vulnerabilities and get the most severe one
// if there are multiple of the same severity, get the first one
let highestSeverity = this.issueSeverityToRanking(IssueSeverity.Low);
Expand All @@ -155,6 +159,10 @@ export class OssCodeActionsProvider extends CodeActionsProvider<OssIssueData> {
}
}

return mostSevereVulnerability;
if (!mostSevereVulnerability) {
return;
}

return getOssIssueCommandArg(mostSevereVulnerability, folderPath, vulnerabilities);
}
}
26 changes: 26 additions & 0 deletions src/snyk/snykOss/providers/ossIssueCommandHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import marked from 'marked';
import { Issue, OssIssueData } from '../../common/languageServer/types';
import { OssIssueCommandArg } from '../interfaces';

export function getOssIssueCommandArg(
vuln: Issue<OssIssueData>,
folderPath: string,
filteredVulns: Issue<OssIssueData>[],
): OssIssueCommandArg {
const matchingIdVulnerabilities = filteredVulns.filter(v => v.id === vuln.id);
let overviewHtml = '';

try {
// TODO: marked.parse does not sanitize the HTML. See: https://marked.js.org/#usage
overviewHtml = marked.parse(vuln.additionalData.description);
} catch (error) {
overviewHtml = '<p>There was a problem rendering the vulnerability overview</p>';
}

return {
...vuln,
matchingIdVulnerabilities,
overviewHtml,
folderPath,
};
}
28 changes: 2 additions & 26 deletions src/snyk/snykOss/providers/ossVulnerabilityTreeProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ from 'lodash';
import * as marked from 'marked';
import { Command, Uri } from 'vscode';
import { OpenCommandIssueType, OpenIssueCommandArg } from '../../common/commands/types';
import { IConfiguration } from '../../common/configuration/configuration';
Expand All @@ -14,7 +13,7 @@ import { ProductIssueTreeProvider } from '../../common/views/issueTreeProvider';
import { TreeNode } from '../../common/views/treeNode';
import { IVSCodeLanguages } from '../../common/vscode/languages';
import { messages } from '../constants/messages';
import { OssIssueCommandArg } from '../interfaces';
import { getOssIssueCommandArg } from './ossIssueCommandHelper';

export default class OssIssueTreeProvider extends ProductIssueTreeProvider<OssIssueData> {
constructor(
Expand Down Expand Up @@ -195,32 +194,9 @@ export default class OssIssueTreeProvider extends ProductIssueTreeProvider<OssIs
arguments: [
{
issueType: OpenCommandIssueType.OssVulnerability,
issue: this.getOssIssueCommandArg(issue, folderPath, filteredIssues),
issue: getOssIssueCommandArg(issue, folderPath, filteredIssues),
} as OpenIssueCommandArg,
],
};
}

getOssIssueCommandArg(
vuln: Issue<OssIssueData>,
folderPath: string,
filteredVulns: Issue<OssIssueData>[],
): OssIssueCommandArg {
const matchingIdVulnerabilities = filteredVulns.filter(v => v.id === vuln.id);
let overviewHtml = '';

try {
// TODO: marked.parse does not sanitize the HTML. See: https://marked.js.org/#usage
overviewHtml = marked.parse(vuln.additionalData.description);
} catch (error) {
overviewHtml = '<p>There was a problem rendering the vulnerability overview</p>';
}

return {
...vuln,
matchingIdVulnerabilities,
overviewHtml,
folderPath,
};
}
}

0 comments on commit 44c0e41

Please sign in to comment.