Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC required packages #454

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions library/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class Agent {
private preventedPrototypePollution = false;
private incompatiblePackages: Record<string, string> = {};
private wrappedPackages: Record<string, WrappedPackage> = {};
private requiredPackages: Record<string, string[]> = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make much difference, but perhaps we should use maps more often in the lib.
https://www.zhenghao.io/posts/object-vs-map

private timeoutInMS = 10000;
private hostnames = new Hostnames(200);
private users = new Users(1000);
Expand Down Expand Up @@ -268,6 +269,7 @@ export class Agent {
this.routes.clear();
this.hostnames.clear();
this.users.clear();
this.requiredPackages = {};
const response = await this.api.report(
this.token,
{
Expand Down Expand Up @@ -365,6 +367,7 @@ export class Agent {
},
{}
),
requiredPackages: this.requiredPackages,
incompatiblePackages: {
prototypePollution: this.incompatiblePackages,
},
Expand Down Expand Up @@ -433,11 +436,24 @@ export class Agent {
this.logger.log(`Failed to wrap module ${module}: ${error.message}`);
}

onPackageRequired(name: string, version: string) {
if (!this.requiredPackages[name]) {
this.requiredPackages[name] = [];
}

if (this.requiredPackages[name].includes(version)) {
return;
}

this.requiredPackages[name].push(version);
}

onPackageWrapped(name: string, details: WrappedPackage) {
if (this.wrappedPackages[name]) {
// Already reported as wrapped
return;
}

this.wrappedPackages[name] = details;

if (details.version) {
Expand Down
6 changes: 5 additions & 1 deletion library/agent/api/Event.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Kind } from "../Attack";
import { Source } from "../Source";

type Version = string;
type PackageName = string;

export type AgentInfo = {
dryMode: boolean;
hostname: string;
version: string;
library: string;
packages: Record<string, string>;
packages: Record<PackageName, Version>;
requiredPackages: Record<PackageName, Version[]>;
ipAddress: string;
preventedPrototypePollution: boolean;
incompatiblePackages: {
Expand All @@ -31,7 +35,7 @@
time: number;
};

export type User = {

Check warning on line 38 in library/agent/api/Event.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

exported declaration 'User' not used within other modules
id: string;
name?: string;
};
Expand Down
26 changes: 13 additions & 13 deletions library/agent/hooks/wrapRequire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,6 @@ function patchPackage(this: mod, id: string, originalExports: unknown) {
.map((pkg) => pkg.getVersions())
.flat();

// We don't want to patch this package because we do not have any hooks for it
if (!versionedPackages.length) {
return originalExports;
}

// Read the package.json of the required package
const packageJson = originalRequire(
`${pathInfo.base}/package.json`
Expand All @@ -207,19 +202,24 @@ function patchPackage(this: mod, id: string, originalExports: unknown) {
);
}

const agent = getInstance();
agent?.onPackageRequired(moduleName, installedPkgVersion);

// We don't want to patch this package because we do not have any hooks for it
if (!versionedPackages.length) {
return originalExports;
}

// Check if the installed package version is supported (get all matching versioned packages)
const matchingVersionedPackages = versionedPackages.filter((pkg) =>
satisfiesVersion(pkg.getRange(), installedPkgVersion)
);

const agent = getInstance();
if (agent) {
// Report to the agent that the package was wrapped or not if it's version is not supported
agent.onPackageWrapped(moduleName, {
version: installedPkgVersion,
supported: !!matchingVersionedPackages.length,
});
}
// Report to the agent that the package was wrapped or not if it's version is not supported
agent?.onPackageWrapped(moduleName, {
version: installedPkgVersion,
supported: !!matchingVersionedPackages.length,
});

if (!matchingVersionedPackages.length) {
// We don't want to patch this package version
Expand Down
Loading