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

feat!: Allow ArcjetContext extension via new argument to core protect() #841

Merged
merged 2 commits into from
May 30, 2024
Merged
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
2 changes: 1 addition & 1 deletion arcjet-bun/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
props ?? {},
) as ArcjetRequest<ExtraProps<Rules>>;

return aj.protect(req);
return aj.protect({}, req);
},
handler(
fetch: (
Expand Down
2 changes: 1 addition & 1 deletion arcjet-next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
ExtraProps<Rules>
>;

return aj.protect(req);
return aj.protect({}, req);
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion arcjet-node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
ExtraProps<Rules>
>;

return aj.protect(req);
return aj.protect({}, req);
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion arcjet-sveltekit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
ExtraProps<Rules>
>;

return aj.protect(req);
return aj.protect({}, req);
},
});
}
Expand Down
6 changes: 5 additions & 1 deletion arcjet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const server = http.createServer(async function (
req: http.IncomingMessage,
res: http.ServerResponse,
) {
// Any sort of additional context that might want to be included for the
// execution of `protect()`. This is mostly only useful for writing adapters.
const ctx = {};

// Construct an object with Arcjet request details
const path = new URL(req.url || "", `http://${req.headers.host}`);
const details = {
Expand All @@ -71,7 +75,7 @@ const server = http.createServer(async function (
path: path.pathname,
};

const decision = await aj.protect(details);
const decision = await aj.protect(ctx, details);
console.log(decision);

if (decision.isDenied()) {
Expand Down
23 changes: 19 additions & 4 deletions arcjet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ export type ExtraProps<Rules> = Rules extends []
? UnionToIntersection<PropsForRule<Rules[number]>>
: never;

/**
* Additional context that can be provided by adapters.
*
* Among other things, this could include the Arcjet API Key if it were only
* available in a runtime handler or IP details provided by a platform.
*/
export type ArcjetAdapterContext = Record<string, unknown>;

/**
* @property {string} ip - The IP address of the client.
* @property {string} method - The HTTP method of the request.
Expand Down Expand Up @@ -1064,10 +1072,14 @@ export interface Arcjet<Props extends PlainObject> {
* Make a decision about how to handle a request. This will analyze the
* request locally where possible and call the Arcjet decision API.
*
* @param {ArcjetAdapterContext} ctx - Additional context for this function call.
* @param {ArcjetRequest} request - Details about the {@link ArcjetRequest} that Arcjet needs to make a decision.
* @returns An {@link ArcjetDecision} indicating Arcjet's decision about the request.
*/
protect(request: ArcjetRequest<Props>): Promise<ArcjetDecision>;
protect(
ctx: ArcjetAdapterContext,
request: ArcjetRequest<Props>,
): Promise<ArcjetDecision>;

/**
* Augments the client with another rule. Useful for varying rules based on
Expand Down Expand Up @@ -1112,6 +1124,7 @@ export default function arcjet<

async function protect<Props extends PlainObject>(
rules: ArcjetRule[],
ctx: ArcjetAdapterContext,
request: ArcjetRequest<Props>,
) {
// This goes against the type definition above, but users might call
Expand Down Expand Up @@ -1149,7 +1162,7 @@ export default function arcjet<
logger.debug("fingerprint (%s): %s", runtime(), fingerprint);
logger.timeEnd("fingerprint");

const context: ArcjetContext = { key, fingerprint };
const context: ArcjetContext = { key, ...ctx, fingerprint };

if (rules.length < 1) {
// TODO(#607): Error if no rules configured after deprecation period
Expand Down Expand Up @@ -1372,9 +1385,10 @@ export default function arcjet<
return withRule(rule);
},
async protect(
ctx: ArcjetContext,
request: ArcjetRequest<ExtraProps<typeof rules>>,
): Promise<ArcjetDecision> {
return protect(rules, request);
return protect(rules, ctx, request);
},
});
}
Expand All @@ -1387,9 +1401,10 @@ export default function arcjet<
return withRule(rule);
},
async protect(
ctx: ArcjetContext,
request: ArcjetRequest<ExtraProps<typeof rootRules>>,
): Promise<ArcjetDecision> {
return protect(rootRules, request);
return protect(rootRules, ctx, request);
},
});
}
31 changes: 17 additions & 14 deletions arcjet/test/index.edge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,23 @@ describe("Arcjet: Env = Edge runtime", () => {

const aj2 = aj.withRule(foobarbaz());

const decision = await aj2.protect({
abc: 123,
requested: 1,
email: "",
ip: "",
method: "",
protocol: "",
host: "",
path: "",
headers: new Headers(),
extra: {},
userId: "user123",
foobar: 123,
});
const decision = await aj2.protect(
{},
{
abc: 123,
requested: 1,
email: "",
ip: "",
method: "",
protocol: "",
host: "",
path: "",
headers: new Headers(),
extra: {},
userId: "user123",
foobar: 123,
},
);

expect(decision.isErrored()).toBe(false);
});
Expand Down
Loading
Loading