Skip to content

Commit

Permalink
feat: use language server for html vulnerability counts [HEAD-282] (#371
Browse files Browse the repository at this point in the history
)

Signed-off-by: Bastian Doetsch <[email protected]>
Co-authored-by: Jason Luong <[email protected]>
  • Loading branch information
bastiandoetsch and j-luong authored Aug 30, 2023
1 parent 925b2e7 commit dca2c24
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 235 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Snyk Security - Code and Open Source Dependencies Changelog

## [1.21.4]

### Changed
- Use Language Server to retrieve vulnerability count for HTML files

## [1.21.1]
### Fixed
- Snyk Learn links

## [1.21.0]
### Fixed
- Plugin Initialization

## [1.20.3]

### Removed
Expand Down
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@
"string-argv": "^0.3.1",
"uuid": "^8.3.2",
"validate-npm-package-name": "^3.0.0",
"vscode-languageclient": "8.1.0"
"vscode-languageclient": "8.1.0",
"vscode-languageserver-textdocument":"^1.0.8"
}
}
12 changes: 12 additions & 0 deletions src/snyk/common/vscode/textdocument.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as vlt from 'vscode-languageserver-textdocument';
import { LSPTextDocument } from './types';

export interface ITextDocumentAdapter {
create(uri: string, language: string, version: number, content: string): LSPTextDocument;
}

export class TextDocumentAdapter implements ITextDocumentAdapter {
create(uri: string, language: string, version: number, content: string): LSPTextDocument {
return vlt.TextDocument.create(uri, language, version, content);
}
}
4 changes: 4 additions & 0 deletions src/snyk/common/vscode/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import * as lsc from 'vscode-languageclient/node';
import * as lst from 'vscode-languageserver-textdocument';

// VS Code core type mappings
export type Disposable = vscode.Disposable;
Expand Down Expand Up @@ -57,3 +58,6 @@ export type ConfigurationParams = lsc.ConfigurationParams;
export type CancellationToken = lsc.CancellationToken;
export type ConfigurationRequestHandlerSignature = lsc.ConfigurationRequest.HandlerSignature;
export type ResponseError<D = void> = lsc.ResponseError<D>;
export type InlineValueContext = lsc.InlineValueContext;
export type InlineValueText = lsc.InlineValueText;
export type LSPTextDocument = lst.TextDocument;
15 changes: 7 additions & 8 deletions src/snyk/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import { HoverAdapter } from './common/vscode/hover';
import { LanguageClientAdapter } from './common/vscode/languageClient';
import { vsCodeLanguages } from './common/vscode/languages';
import SecretStorageAdapter from './common/vscode/secretStorage';
import { TextDocumentAdapter } from './common/vscode/textdocument';
import { ThemeColorAdapter } from './common/vscode/theme';
import { Range, Uri } from './common/vscode/types';
import { UriAdapter } from './common/vscode/uri';
Expand All @@ -74,10 +75,8 @@ import { CodeSuggestionWebviewProvider } from './snykCode/views/suggestion/codeS
import { IacService } from './snykIac/iacService';
import IacIssueTreeProvider from './snykIac/views/iacIssueTreeProvider';
import { IacSuggestionWebviewProvider } from './snykIac/views/suggestion/iacSuggestionWebviewProvider';
import { NpmTestApi } from './snykOss/api/npmTestApi';
import { EditorDecorator } from './snykOss/editor/editorDecorator';
import { OssService } from './snykOss/services/ossService';
import { NpmModuleInfoFetchService } from './snykOss/services/vulnerabilityCount/npmModuleInfoFetchService';
import { OssVulnerabilityCountService } from './snykOss/services/vulnerabilityCount/ossVulnerabilityCountService';
import { ModuleVulnerabilityCountProvider } from './snykOss/services/vulnerabilityCount/vulnerabilityCountProvider';
import { OssVulnerabilityTreeProvider } from './snykOss/views/ossVulnerabilityTreeProvider';
Expand Down Expand Up @@ -361,16 +360,16 @@ class SnykExtension extends SnykLib implements IExtension {

this.initDependencyDownload();

const npmModuleInfoFetchService = new NpmModuleInfoFetchService(
configuration,
Logger,
new NpmTestApi(Logger, vsCodeWorkspace),
);
this.ossVulnerabilityCountService = new OssVulnerabilityCountService(
vsCodeWorkspace,
vsCodeWindow,
vsCodeLanguages,
new ModuleVulnerabilityCountProvider(this.ossService, npmModuleInfoFetchService),
new ModuleVulnerabilityCountProvider(
this.ossService,
languageClientAdapter,
new UriAdapter(),
new TextDocumentAdapter(),
),
this.ossService,
Logger,
new EditorDecorator(vsCodeWindow, vsCodeLanguages, new ThemeColorAdapter()),
Expand Down
40 changes: 0 additions & 40 deletions src/snyk/snykOss/api/npmTestApi.ts

This file was deleted.

9 changes: 7 additions & 2 deletions src/snyk/snykOss/messages/vulnerabilityCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ export const messages = {
vulnerabilities: 'vulnerabilities',
showMostSevereVulnerability: 'Show the most severe vulnerability (Snyk)',

decoratorMessage: (vulnerabilityCount: number): string =>
`${vulnerabilityCount} ${vulnerabilityCount > 1 ? 'vulnerabilities' : 'vulnerability'}`,
decoratorMessage: (vulnerabilityCount: string): string => {
const vulnerabilityCountNumber = Number.parseInt(vulnerabilityCount, 10);
if (isNaN(vulnerabilityCountNumber)) {
return vulnerabilityCount;
}
return `${vulnerabilityCountNumber} ${vulnerabilityCountNumber > 1 ? 'vulnerabilities' : 'vulnerability'}`;
},

diagnosticMessagePrefix: (module: ModuleVulnerabilityCount): string =>
`Dependency ${module.name}${module.version ? `@${module.version}` : ''} has `,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ export type Range = {
column: number;
};
};

export type TestedImportedModule = ImportedModule & {
tested: boolean;
vulnerabilityCount?: number;
};

export enum ModuleVulnerabilityCountSeverity {
Low = 'low',
Medium = 'medium',
Expand All @@ -41,7 +35,7 @@ export type ModuleVulnerabilityCount = {
line: number | null;
range: Range | null;
hasCount: boolean;
count?: number;
count?: string;
severityCounts?: SeverityCounts;
mostSevereVulnerabilityId?: string;
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class OssVulnerabilityCountService implements Disposable {
let emitter = this.fileEmitters.get(fileName);
if (emitter) {
emitter.removeAllListeners();
this.editorDecorator.resetDecorations(fileName);
} else {
emitter = new VulnerabilityCountEmitter();
this.fileEmitters.set(fileName, emitter);
Expand Down Expand Up @@ -215,7 +216,7 @@ export class OssVulnerabilityCountService implements Disposable {
emitter.startScanning(modules);

const promises = modules
.map(module => this.vulnerabilityCountProvider.getVulnerabilityCount(module, language))
.map(module => this.vulnerabilityCountProvider.getVulnerabilityCount(fileName, module, language))
.map(promise =>
promise.then(module => {
emitter.scanned(module);
Expand Down Expand Up @@ -253,7 +254,7 @@ export class OssVulnerabilityCountService implements Disposable {
],
module,
);
message += module.count > 1 ? ` ${messages.vulnerabilities}.` : ` ${messages.vulnerabilities}.`;
message += messages.decoratorMessage(module.count);
return message;
}

Expand All @@ -262,7 +263,7 @@ export class OssVulnerabilityCountService implements Disposable {
module: ModuleVulnerabilityCount,
): string {
if (!module.severityCounts) {
return module.count ? module.count.toString() : '';
return module.count ? module.count : '';
}

const content: string[] = [];
Expand Down
Loading

0 comments on commit dca2c24

Please sign in to comment.