Skip to content

Commit

Permalink
Escaping special regex chars (#844)
Browse files Browse the repository at this point in the history
* Escaping special regex chars

* Separating test function into its own file

* Uncommenting line

* Added functionality to prevent double escaping and made testing function more debug friendly

---------

Co-authored-by: Reinier Cruz <[email protected]>
  • Loading branch information
ReinierCC and Reinier Cruz authored Aug 22, 2024
1 parent d414829 commit 5c1e306
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/panels/TcpDumpPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ const tcpDumpCommandBase = "tcpdump --snapshot-length=0 -vvv";
const captureDir = "/tmp";
const captureFilePrefix = "vscodenodecap_";
const captureFileBasePath = `${captureDir}/${captureFilePrefix}`;
const captureFilePathRegex = `${captureFileBasePath.replace(/\//g, "\\$&")}(.*)\\.cap`; // Matches the part of the filename after the prefix
const captureFileBasePathEscaped = escapeRegExp(captureFileBasePath);
const captureFilePathRegex = `${captureFileBasePathEscaped}(.*)\\.cap`; // Matches the part of the filename after the prefix

// Escape all regex meta characters to ensure sanitation and '/' for later use in regex pattern
export function escapeRegExp(input: string): string {
return input.replace(/(\\)?([.*+?^${}()|[\]\\/])/g, (match, backslash, char) => {
// If there's a backslash before the character, return the match unchanged. (To prevent double escaping)
return backslash ? match : `\\${char}`;
});
} //Reference for regex syntax chars: https://262.ecma-international.org/13.0/index.html#prod-SyntaxCharacter

function getPodName(node: NodeName) {
return `debug-${node}`;
Expand Down
32 changes: 32 additions & 0 deletions src/tests/suite/TcpDumpPanel.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as assert from "assert";
import { escapeRegExp } from "../../panels/TcpDumpPanel";

describe("testEscapeRegExp", () => {
it("should escape special regex characters", () => {
const input = "a.b*c?d+e^f$g|h(i)j{k}l[m]n\\o";
const expected = "a\\.b\\*c\\?d\\+e\\^f\\$g\\|h\\(i\\)j\\{k\\}l\\[m\\]n\\\\o";
const escapedInput = escapeRegExp(input);
assert.equal(escapedInput, expected);
});

it("should not double escape already escaped characters", () => {
const input = "a\\.b\\*c\\?d\\+e\\^f\\$g\\|h\\(i\\)j\\{k\\}l\\[m\\]n\\\\o";
const expected = "a\\.b\\*c\\?d\\+e\\^f\\$g\\|h\\(i\\)j\\{k\\}l\\[m\\]n\\\\o";
const escapedInput = escapeRegExp(input);
assert.equal(escapedInput, expected);
});

it("should return an empty string if input is empty", () => {
const input = "";
const expected = "";
const escapedInput = escapeRegExp(input);
assert.equal(escapedInput, expected);
});

it("should return the same string if no special characters", () => {
const input = "abcdefg";
const expected = "abcdefg";
const escapedInput = escapeRegExp(input);
assert.equal(escapedInput, expected);
});
});

0 comments on commit 5c1e306

Please sign in to comment.