Skip to content

Commit

Permalink
fix broken for node v14
Browse files Browse the repository at this point in the history
  • Loading branch information
yaakov123 committed Jul 7, 2022
1 parent 3e397a7 commit 4643a1f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/overrides/cluster.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import cluster from "cluster";
import { hasOwnProperty } from "../natives/$object";
import { reflectApply } from "../natives/$proxy";
import { createProxy } from "../proxy";
import { inject } from "../utils/inject";

function onClusterSetup(target: any, thisArg: any, argArray: any[]) {
const filePath = argArray[0]?.exec;
console.log(argArray);
if (!filePath) {
return reflectApply(target, thisArg, argArray);
}
Expand All @@ -16,13 +16,13 @@ function onClusterSetup(target: any, thisArg: any, argArray: any[]) {
}

export function overrideCluster() {
if (Object.hasOwn(cluster, "setupMaster")) {
if (hasOwnProperty(cluster, "setupMaster")) {
cluster.setupMaster = createProxy(cluster.setupMaster, {
apply: onClusterSetup,
});
}

if (Object.hasOwn(cluster, "setupPrimary")) {
if (hasOwnProperty(cluster, "setupPrimary")) {
cluster.setupPrimary = createProxy(cluster.setupPrimary, {
apply: onClusterSetup,
});
Expand Down
24 changes: 24 additions & 0 deletions src/test/fake_modules/allowed-network/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import http from "http";

export function allowedHttp() {
return new Promise((resolve, reject) => {
const chunks: any[] = [];

const options = {
hostname: "jsonplaceholder.typicode.com",
path: "/todos/1",
};

const req = http.request(options, (res) => {
res.on("data", (chunk: any) => chunks.push(chunk));

res.once("end", () => {
resolve("");
});
});

req.once("error", reject);

req.end();
});
}
11 changes: 11 additions & 0 deletions src/test/overrides/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
maliciousHttpRequest,
maliciousLookupBypass,
} from "../fake_modules/malicious-network";
import { allowedHttp } from "../fake_modules/allowed-network";

describe("network", () => {
beforeAll(() => {
Expand All @@ -17,6 +18,16 @@ describe("network", () => {
expect(maliciousHttpRequest()).rejects.toThrowError();
});

test("it should allow http.request with allowed host", async () => {
hagana.setAllowedHosts(["jsonplaceholder.typicode.com"]);
try {
await allowedHttp();
expect(true).toBe(true);
} catch {
expect(true).toBe(false);
}
});

// test("it should prevent lookup bypass", async () => {
// hagana.setAllowedHosts(["httpbin.org"]);
// await maliciousLookupBypass();
Expand Down

0 comments on commit 4643a1f

Please sign in to comment.