Skip to content

Commit

Permalink
Extract helper for checking if it's windows
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Oct 16, 2024
1 parent fb9c084 commit f9c1395
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
3 changes: 3 additions & 0 deletions library/helpers/isWindows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isWindows() {
return process.platform === "win32";
}
53 changes: 31 additions & 22 deletions library/sinks/Path.doubleWrapping.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
import { join, normalize, resolve } from "path/posix";
import * as t from "tap";
import { isWindows } from "../helpers/isWindows";
import { isWrapped } from "../helpers/wrap";
import { Path } from "./Path";
import { createTestAgent } from "../helpers/createTestAgent";

t.test("it works", { skip: process.platform === "win32" }, async (t) => {
const agent = createTestAgent();
t.test(
"it works",
{ skip: isWindows() ? "path is not the same as path/posix" : false },
async (t) => {
const agent = createTestAgent();

agent.start([new Path()]);
agent.start([new Path()]);

const { join, resolve, normalize } = require("path/posix");
const { join, resolve, normalize } = require("path/posix");

// Path required after path/posix
require("path");
// Path required after path/posix
require("path");

const checkForDoubleWrapping = [join, resolve, normalize];
for (const fn of checkForDoubleWrapping) {
if (isWrapped(fn) && isWrapped(fn.__original)) {
t.fail(`${fn.name} is double wrapped!`);
const checkForDoubleWrapping = [join, resolve, normalize];
for (const fn of checkForDoubleWrapping) {
if (isWrapped(fn) && isWrapped(fn.__original)) {
t.fail(`${fn.name} is double wrapped!`);
}
}
}
});
);

t.test("it works", { skip: process.platform !== "win32" }, async (t) => {
const agent = createTestAgent();
t.test(
"it works",
{ skip: !isWindows() ? "path is not the same as path/win32" : false },
async (t) => {
const agent = createTestAgent();

agent.start([new Path()]);
agent.start([new Path()]);

const { join, resolve, normalize } = require("path/win32");
const { join, resolve, normalize } = require("path/win32");

// Path required after path/win32
require("path");
// Path required after path/win32
require("path");

const checkForDoubleWrapping = [join, resolve, normalize];
for (const fn of checkForDoubleWrapping) {
if (isWrapped(fn) && isWrapped(fn.__original)) {
t.fail(`${fn.name} is double wrapped!`);
const checkForDoubleWrapping = [join, resolve, normalize];
for (const fn of checkForDoubleWrapping) {
if (isWrapped(fn) && isWrapped(fn.__original)) {
t.fail(`${fn.name} is double wrapped!`);
}
}
}
});
);
7 changes: 2 additions & 5 deletions library/sinks/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Hooks } from "../agent/hooks/Hooks";
import { wrapExport } from "../agent/hooks/wrapExport";
import { WrapPackageInfo } from "../agent/hooks/WrapPackageInfo";
import { Wrapper } from "../agent/Wrapper";
import { isWindows } from "../helpers/isWindows";
import { checkContextForPathTraversal } from "../vulnerabilities/path-traversal/checkContextForPathTraversal";
import type * as path from "path";

Expand Down Expand Up @@ -48,17 +49,13 @@ export class Path implements Wrapper {
}
}

private isWindows() {
return process.platform === "win32";
}

private wrapMainModule(exports: typeof path, pkgInfo: WrapPackageInfo) {
// If `path/win32` or `path/posix` was not required before `path`, we should wrap the functions in `path`
if (!this.patchedWin32 && !this.patchedPosix) {
this.wrapFunctions(exports, pkgInfo);
}

if (this.isWindows()) {
if (isWindows()) {
// `require("path").join` is the same as `require("path/win32").join`
this.patchedWin32 = true;

Check warning on line 60 in library/sinks/Path.ts

View check run for this annotation

Codecov / codecov/patch

library/sinks/Path.ts#L59-L60

Added lines #L59 - L60 were not covered by tests
} else {
Expand Down

0 comments on commit f9c1395

Please sign in to comment.