Skip to content

Commit

Permalink
Restructure library
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Feb 14, 2024
1 parent 8c26669 commit 2a0ae10
Show file tree
Hide file tree
Showing 19 changed files with 126 additions and 161 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,14 @@ We believe that there are legitimate cases of prototype changes, but they should
```js
import { protect, preventPrototypePollution } from '@aikidosec/guard';

// Before main imports
protect();

import express from 'express';

// After main imports
preventPrototypePollution();

const app = express();

app.get("/", (req, res) => {
Expand All @@ -130,9 +134,6 @@ app.get("/", (req, res) => {

app.listen(3000, () => {
console.log("Server is running on port 3000");

// Your app is initialized, now it's time to prevent prototype pollution
preventPrototypePollution();
});
```

Expand Down
3 changes: 1 addition & 2 deletions benchmarks/express-mongodb/withGuard.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const { protect, preventPrototypePollution } = require("@aikidosec/guard");
const { protect } = require("@aikidosec/guard");

protect();

require("./createApp")(4000).then(() => {
preventPrototypePollution();
console.log("Listening on port 4000");
console.log("Secured with @aikidosec/guard!");
});
4 changes: 0 additions & 4 deletions library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,5 @@
"require-in-the-middle": "^7.2.0",
"shimmer": "^1.2.1",
"ulid": "^2.3.0"
},
"optionalDependencies": {
"express": "^4.0.0",
"mongodb": "^4.0.0 || ^5.0.0 || ^6.0.0"
}
}
3 changes: 3 additions & 0 deletions library/src/agent/API.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function generateAttackEvent(): Event {
name: "os",
version: "version",
},
serverless: false,
},
};
}
Expand Down Expand Up @@ -85,6 +86,7 @@ function generateStartedEvent(): Event {
name: "os",
version: "version",
},
serverless: false,
},
};
}
Expand Down Expand Up @@ -131,6 +133,7 @@ function generateHeartbeatEvent(): Event {
name: "os",
version: "version",
},
serverless: false,
},
};
}
Expand Down
3 changes: 2 additions & 1 deletion library/src/agent/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type AgentInfo = {
version: string;
};
nodeEnv: string;
serverless: boolean;
};

type Started = {
Expand All @@ -44,7 +45,7 @@ export type Kind = "nosql_injection";
type DetectedAttack = {
type: "detected_attack";
request: {
method: string;
method: string | undefined;
ipAddress: string | undefined;
userAgent: string | undefined;
url: string | undefined;
Expand Down
64 changes: 23 additions & 41 deletions library/src/agent/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IDGeneratorFixed } from "./IDGenerator";
import { LoggerNoop } from "./Logger";
import { address } from "ip";

t.test("it sends install event once", async (t) => {
t.test("it sends started event", async (t) => {
const logger = new LoggerNoop();
const api = new APIForTesting();
const token = new Token("123");
Expand All @@ -15,13 +15,14 @@ t.test("it sends install event once", async (t) => {
logger,
api,
token,
[],
new IDGeneratorFixed("id"),
false
false,
{
mongodb: "1.0.0",
}
);
agent.start();

await new Promise((resolve) => setImmediate(resolve));
t.match(api.getEvents(), [
{
type: "started",
Expand All @@ -31,29 +32,9 @@ t.test("it sends install event once", async (t) => {
hostname: hostname(),
version: "1.0.0",
ipAddress: address(),
packages: {},
preventedPrototypePollution: false,
nodeEnv: "",
os: {
name: platform(),
version: release(),
packages: {
mongodb: "1.0.0",
},
},
},
]);

agent.start();
await new Promise((resolve) => setImmediate(resolve));
t.match(api.getEvents(), [
{
type: "started",
agent: {
id: "id",
dryMode: false,
hostname: hostname(),
version: "1.0.0",
ipAddress: address(),
packages: {},
preventedPrototypePollution: false,
nodeEnv: "",
os: {
Expand All @@ -64,7 +45,6 @@ t.test("it sends install event once", async (t) => {
},
]);

// Stop setInterval from heartbeat
agent.stop();
});

Expand All @@ -77,17 +57,19 @@ t.test("when prevent prototype pollution is enabled", async (t) => {
logger,
api,
token,
[],
new IDGeneratorFixed("id"),
false
true,
{}
);
agent.start();
// @ts-expect-error Private property
t.same(agent.info.preventedPrototypePollution, false);
agent.onPrototypePollutionPrevented();
// @ts-expect-error Private property
t.same(agent.info.preventedPrototypePollution, true);
agent.stop();
agent.start();
t.match(api.getEvents(), [
{
agent: {
preventedPrototypePollution: true,
},
},
]);
});

t.test("it does not start interval in serverless mode", async () => {
Expand All @@ -99,9 +81,9 @@ t.test("it does not start interval in serverless mode", async () => {
logger,
api,
token,
[],
new IDGeneratorFixed("id"),
true
true,
{}
);

// This would otherwise keep the process running
Expand All @@ -117,9 +99,9 @@ t.test("it keeps track of stats", async () => {
logger,
api,
token,
[],
new IDGeneratorFixed("id"),
true
true,
{}
);

agent.start();
Expand Down Expand Up @@ -181,9 +163,9 @@ t.test("it keeps tracks of stats in dry mode", async () => {
logger,
api,
token,
[],
new IDGeneratorFixed("id"),
true
true,
{}
);

agent.start();
Expand Down
Loading

0 comments on commit 2a0ae10

Please sign in to comment.