Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Custom rules to SAST scan #474

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
"pattern": "^$|^\\d+\\.\\d+\\.\\d+$",
"markdownDescription": "Specifies the JFrog Scanners version to use. (format X.X.X). By default the latest scanners version is used."
},
"jfrog.customRulesPath": {
"type": "string",
"scope": "resource",
"markdownDescription": "Absolute Path to a local custom rules file. The file should be in JSON format and contain the additional custom rules to be applied during the scan."
},
"jfrog.xray.exclusions": {
"type": "string",
"default": "**/*{.git,test,venv,node_modules,target}*",
Expand Down
3 changes: 3 additions & 0 deletions src/main/scanLogic/scanRunners/sastScan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AnalyzerUtils } from '../../treeDataProviders/utils/analyzerUtils';
import { StepProgress } from '../../treeDataProviders/utils/stepProgress';
import { Severity } from '../../types/severity';
import { ScanResults } from '../../types/workspaceIssuesDetails';
import { Configuration } from '../../utils/configuration';
import { AppsConfigModule } from '../../utils/jfrogAppsConfig/jfrogAppsConfig';
import { Translators } from '../../utils/translators';
import { AnalyzerManager } from './analyzerManager';
Expand All @@ -26,6 +27,7 @@ import { BinaryEnvParams, JasRunner, RunArgs } from './jasRunner';
*/
export interface SastScanRequest extends AnalyzeScanRequest {
language: LanguageType;
user_rules: string;
exclude_patterns: string[];
excluded_rules: string[];
}
Expand Down Expand Up @@ -92,6 +94,7 @@ export class SastRunner extends JasRunner {
type: this._scanType,
roots: this._config.GetSourceRoots(this._scanType),
language: this._config.GetScanLanguage(),
user_rules: Configuration.getSastCustomRulesPath(this._logManager),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see user_rules is assigned here - but where is it used? who calls this param inside Configuration object?

Copy link
Contributor Author

@attiasas attiasas May 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hadarshjfrog, The configuration value is coming from the settings in the vscode as shared at the details
image

The value is than fetched using Configuration.getSastCustomRulesPath and we pass it to the SAST scanner that uses it while running. (user_rules is a new attribute in the scanner config)

excluded_rules: this._config.getExcludeRules(),
exclude_patterns: this._config.GetExcludePatterns(this._scanType)
} as SastScanRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export abstract class CodeIssueTreeNode extends IssueTreeNode {
return this._regionWithIssue;
}

// For vscode the minimum value (i.e first row/col) is 0.
// For vscode the minimum value (i.e first row/col) is 0.
// For analyzers, the minimum value is 1. (some uses 0 as well)
private toVscodePosition(position: vscode.Position): vscode.Position {
let line: number = position.line > 0 ? position.line - 1 : 0;
Expand Down
21 changes: 20 additions & 1 deletion src/main/utils/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import { LogLevel } from '../log/logManager';
import * as fs from 'fs';
import { LogLevel, LogManager } from '../log/logManager';
export class Configuration {
public static jfrogSectionConfigurationKey: string = 'jfrog';
public static readonly JFROG_IDE_RELEASES_REPO_ENV: string = 'JFROG_IDE_RELEASES_REPO';
Expand Down Expand Up @@ -73,6 +74,24 @@ export class Configuration {
return version;
}

public static getSastCustomRulesPath(logManager?: LogManager): string {
let customRulesPath: string = vscode.workspace.getConfiguration(this.jfrogSectionConfigurationKey).get('customRulesPath', '');
if (customRulesPath === '') {
return '';
}
let fileExists: boolean = fs.existsSync(customRulesPath);
if (!fileExists) {
if (logManager) {
logManager.logMessage('Custom rules file not found: ' + customRulesPath, 'WARN');
}
return '';
}
if (logManager) {
logManager.logMessage('Using custom rules from: ' + customRulesPath, 'DEBUG');
}
return customRulesPath;
}

/**
* @returns the log level
*/
Expand Down
Loading