Skip to content

Commit

Permalink
- исправлена ошибка из-за которой в окне Проблемы (Problems) не вывод…
Browse files Browse the repository at this point in the history
…илась информация о неуспешной сборке графа корреляции при пустом файле rule.co (special thanks @jjack_the_reaper).
  • Loading branch information
DmitryOffsec committed Jul 17, 2024
1 parent d4c38c4 commit 44cd78e
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 56 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 3.17.14 (Pre-Release)

- исправлена ошибка из-за которой в окне Проблемы (Problems) не выводилась информация о неуспешной сборке графа корреляции при пустом файле rule.co (special thanks @jjack_the_reaper).

## 3.17.13 (Pre-Release)

- добавлена возможность автоматического обновления окна интеграционных тестов при их изменении файлов на диске (появляется запрос к пользователю), а также предложение о закрытии окна в случае удаления правила;
Expand Down
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Language client",
"author": "Dmitry Fedosov (@DmitryOffsec)",
"license": "MIT",
"version": "3.17.13",
"version": "3.17.14",
"repository": {
"type": "git",
"url": "https://github.com/Security-Experts-Community/vscode-xp"
Expand Down
1 change: 1 addition & 0 deletions client/src/helpers/regExpHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export class RegExpHelper {
continue;
}

// TODO: добавить валидацию lineNumber, если будет меньше нуля, то будет исключение.
functionCalls.push(new vscode.Range(
new vscode.Position(lineNumber, beginIndex),
new vscode.Position(lineNumber, endIndex)
Expand Down
33 changes: 33 additions & 0 deletions client/src/helpers/vsCodeApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@ import { FileSystemHelper } from './fileSystemHelper';

export class VsCodeApiHelper {

public static createDiagnostic(
startLine: number,
startOffset: number,
endLine: number,
endOffset: number,
description: string) : vscode.Diagnostic {

// Если файл пусто, KBT возвращает не one-based offset, а zero-based
let startLineAfterCorrection = startLine;
if(startLine < 0) {
startLineAfterCorrection = 0;
}

let endLineAfterCorrection = endLine;
if(endLine < 0) {
endLineAfterCorrection = 0;
}

const startPosition = new vscode.Position(startLineAfterCorrection, startOffset);
const endPosition = new vscode.Position(endLineAfterCorrection, endOffset);

const diagnostic = new vscode.Diagnostic(
new vscode.Range(
startPosition,
endPosition
),
description
);

diagnostic.source = 'xp';
return diagnostic;
}

public static showDifferencesBetweenTwoFiles(leftFileUri: vscode.Uri, rightFileUri: vscode.Uri, title?: string) : Thenable<unknown> {
return vscode.commands.executeCommand("vscode.diff",
leftFileUri,
Expand Down
44 changes: 16 additions & 28 deletions client/src/models/siemj/siemJOutputParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FileSystemHelper } from '../../helpers/fileSystemHelper';
import { TestHelper } from '../../helpers/testHelper';
import { Log } from '../../extension';
import { Configuration } from '../configuration';
import { VsCodeApiHelper } from '../../helpers/vsCodeApiHelper';

export class FileDiagnostics {
public uri : vscode.Uri;
Expand Down Expand Up @@ -85,24 +86,17 @@ export class SiemJOutputParser {
}

const ruleFilePath = (m[1] as string).trim();
const ruleLineNumber = parseInt(m[2]) - 1;
const ruleCharNumber = parseInt(m[3]);
const fileLineNumber = parseInt(m[2]) - 1;
const fileCharNumber = parseInt(m[3]);
const errorDescription = (m[4] as string).trim();

// Выделяем строку с начала, так как в выводе координаты только одного символа.
const startPosition = new vscode.Position(ruleLineNumber, 0);
const endPosition = new vscode.Position(ruleLineNumber, ruleCharNumber);

const diagnostic = new vscode.Diagnostic(
new vscode.Range(
startPosition,
endPosition
),
const diagnostic = VsCodeApiHelper.createDiagnostic(
fileLineNumber, 0,
fileLineNumber, fileCharNumber,
errorDescription
);

diagnostic.source = 'xp';

if(errorDescription.includes("warning: ")) {
diagnostic.severity = vscode.DiagnosticSeverity.Warning;
diagnostic.message = diagnostic.message.replace("warning: ", "");
Expand Down Expand Up @@ -149,17 +143,8 @@ export class SiemJOutputParser {
const filePath = (m[2] as string).trim();

// В ошибке нет информации, где именно ошибка, указываем на начало файла.
const startPosition = new vscode.Position(0, 0);
const endPosition = new vscode.Position(0, 0);

const diagnostic = new vscode.Diagnostic(
new vscode.Range(
startPosition,
endPosition
),
errorDescription,
vscode.DiagnosticSeverity.Error
);
const diagnostic = VsCodeApiHelper.createDiagnostic(0, 0, 0, 0, errorDescription);
diagnostic.severity = vscode.DiagnosticSeverity.Error;

const newRuleFileDiag = this.addFileDiagnostics(result, filePath, diagnostic);
fileDiagnostics.push(newRuleFileDiag);
Expand Down Expand Up @@ -250,21 +235,24 @@ export class SiemJOutputParser {

/**
* Меняет начальное смещение ошибки на первый не пробельный символ, так как исходная ошибка возвращается в виде одного символа.
* @param FileDiagnostics список диагностик для файлов.
* @param fileDiagnostics список диагностик для файлов.
* @returns скорректированные диагностики.
*/
private async correctDiagnosticBeginCharRanges(FileDiagnostics : FileDiagnostics[]) : Promise<FileDiagnostics[]> {
for(const rfd of FileDiagnostics) {
private async correctDiagnosticBeginCharRanges(fileDiagnostics : FileDiagnostics[]) : Promise<FileDiagnostics[]> {
for(const rfd of fileDiagnostics) {
const ruleFilePath = rfd.uri.fsPath;
if(!fs.existsSync(ruleFilePath)) {
Log.warn(`The path to the file ${ruleFilePath} from the error does not exist`);
continue;
}

const ruleContent = await FileSystemHelper.readContentFile(ruleFilePath);
rfd.diagnostics = TestHelper.correctWhitespaceCharacterFromErrorLines(ruleContent, rfd.diagnostics);
if(ruleContent) {
rfd.diagnostics = TestHelper.correctWhitespaceCharacterFromErrorLines(ruleContent, rfd.diagnostics);
}
}

return FileDiagnostics;
return fileDiagnostics;
}

private readonly TESTS_SUCCESS_SUBSTRING = "All tests OK";
Expand Down
19 changes: 6 additions & 13 deletions client/src/models/tests/correlationUnitTestOutputParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode';
import { UnitTestOutputParser } from './unitTestOutputParser';
import { TestHelper } from '../../helpers/testHelper';
import { diffJson } from 'diff';
import { VsCodeApiHelper } from '../../helpers/vsCodeApiHelper';

export class CorrelationUnitTestOutputParser implements UnitTestOutputParser {

Expand Down Expand Up @@ -96,19 +97,11 @@ export class CorrelationUnitTestOutputParser implements UnitTestOutputParser {
const errorDescription = (m[4] as string).trim();

// Выделяем строку с начала, так как в выводе координаты только одного символа.
const startPosition = new vscode.Position(ruleLineNumber, 0);
const endPosition = new vscode.Position(ruleLineNumber, ruleCharNumber);

// TODO: обернуть для избежания копипаста.
const diagnostic: vscode.Diagnostic = {
severity: vscode.DiagnosticSeverity.Error,
range: new vscode.Range(
startPosition,
endPosition
),
message: errorDescription,
source: 'xp'
};
const diagnostic = VsCodeApiHelper.createDiagnostic(
ruleLineNumber, 0,
ruleLineNumber, ruleCharNumber,
errorDescription);
diagnostic.severity = vscode.DiagnosticSeverity.Error;
diagnostics.push(diagnostic);
}
return diagnostics;
Expand Down
15 changes: 8 additions & 7 deletions client/src/models/tests/normalizationUnitTestOutputParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode';
import { UnitTestOutputParser } from './unitTestOutputParser';
import { XpException } from '../xpException';
import { VsCodeApiHelper } from '../../helpers/vsCodeApiHelper';

export class NormalizationUnitTestOutputParser implements UnitTestOutputParser {
parseFailedOutput(output: string, expectation: string): string {
Expand All @@ -10,17 +11,17 @@ export class NormalizationUnitTestOutputParser implements UnitTestOutputParser {
throw new XpException('Method not implemented.');
}

private _diagnostics: vscode.Diagnostic[];
private diagnostics: vscode.Diagnostic[];

/**
* Разбирает ошибки из вывода модульных тестов нормализаций.
* @param testOutput вывод модульных тестов нормализаций.
* @returns список локаций ошибок.
*/
public parse(testOutput : string) : vscode.Diagnostic[] {
this._diagnostics = [];
this.diagnostics = [];
this.patterns.forEach((handler) => {handler(testOutput);});
return this._diagnostics;
return this.diagnostics;
}

private constructDiagnostics(ruleLineNumber: number, ruleCharNumber: number, errorDescription: string) {
Expand All @@ -46,8 +47,8 @@ export class NormalizationUnitTestOutputParser implements UnitTestOutputParser {
const ruleLineNumber = 0;
const ruleCharNumber = 0;
const errorDescription = (m[1] as string).trim();
const diagnostic = this.constructDiagnostics(ruleLineNumber, ruleCharNumber, errorDescription);
this._diagnostics.push(diagnostic);
const diagnostic = VsCodeApiHelper.createDiagnostic(ruleLineNumber, ruleCharNumber, ruleLineNumber, ruleCharNumber, errorDescription);
this.diagnostics.push(diagnostic);
}
},
(testOutput: string) => {
Expand All @@ -57,8 +58,8 @@ export class NormalizationUnitTestOutputParser implements UnitTestOutputParser {
const ruleCharNumber = parseInt(m[2]);
const errorDescription = (m[3] as string).trim();
// Почему-то интерфейс прибавляет 1 к строке и столбцу, вычтем для точности
const diagnostic = this.constructDiagnostics(ruleLineNumber, ruleCharNumber, errorDescription);
this._diagnostics.push(diagnostic);
const diagnostic = VsCodeApiHelper.createDiagnostic(ruleLineNumber, ruleCharNumber, ruleLineNumber, ruleCharNumber, errorDescription);
this.diagnostics.push(diagnostic);
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions client/src/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ export async function setTestContent(content: string): Promise<boolean> {
return editor.edit(eb => eb.replace(all, content));
}

export function toRange(sLine: number, sChar: number, eLine: number, eChar: number) {
export function toRange(sLine: number, sChar: number, eLine: number, eChar: number): vscode.Range {
const start = new vscode.Position(sLine, sChar);
const end = new vscode.Position(eLine, eChar);
return new vscode.Range(start, end);
}

export async function testDiagnostics(docUri: vscode.Uri, expectedDiagnostics: vscode.Diagnostic[]) {
export async function testDiagnostics(docUri: vscode.Uri, expectedDiagnostics: vscode.Diagnostic[]) : Promise<void> {
await activate(docUri);

const actualDiagnostics = vscode.languages.getDiagnostics(docUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Log } from '../../../extension';
import { ContentTreeProvider } from '../../contentTree/contentTreeProvider';
import { FileSystemHelper } from '../../../helpers/fileSystemHelper';
import { RegExpHelper } from '../../../helpers/regExpHelper';
import { VsCodeApiHelper } from '../../../helpers/vsCodeApiHelper';

export class RunIntegrationTestsCommand extends Command {

Expand Down Expand Up @@ -126,9 +127,10 @@ export class RunIntegrationTestsCommand extends Command {

this.params.config.addDiagnosticCollection(
ruleFilePath,
new vscode.Diagnostic(
// TODO: указывать в какое-то более разумное место.
new vscode.Range(new vscode.Position(0,0), new vscode.Position(0,0)),
// TODO: указывать в какое-то более разумное место.
VsCodeApiHelper.createDiagnostic(
0, 0,
0, 0,
this.params.config.getMessage(`View.IntegrationTests.Message.NotEnoughRequiredFields`, testNumber, requiredCorrField)
)
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Dmitry Fedosov (@DmitryOffsec)",
"icon": "resources/xp.png",
"license": "MIT",
"version": "3.17.13",
"version": "3.17.14",
"repository": {
"type": "git",
"url": "https://github.com/Security-Experts-Community/vscode-xp"
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "server",
"description": "Language server",
"version": "3.17.13",
"version": "3.17.14",
"author": "Dmitry Fedosov (@DmitryOffsec)",
"license": "MIT",
"engines": {
Expand Down

0 comments on commit 44cd78e

Please sign in to comment.