Skip to content

Commit

Permalink
feat: show isSpoofed() in bot examples
Browse files Browse the repository at this point in the history
  • Loading branch information
e-moran committed Nov 29, 2024
1 parent 1f220fc commit 29fe5b3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
28 changes: 19 additions & 9 deletions examples/express-bots/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,27 @@ const aj = arcjet({

app.get('/', async (req, res) => {
const decision = await aj.protect(req);
// We need to check that the bot is who they say they are.
if (decision.reason.isSpoofed()) {
return NextResponse.json(
{ error: "You are pretending to be a good bot!" },
{ status: 403, headers },
);
}

if (decision.isDenied() && decision.reason.isBot()) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({
error: "You are a bot",
detected: decision.reason.denied[0]
}));
} else {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: `Hello world!` }));
if (decision.isBot()) {
// We want to check for disallowed bots, or spoofed bots
if (decision.isDenied() || decision.reason.isSpoofed()) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({
error: "You are a bot",
detected: decision.reason.denied[0]
}));
}
}

res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: `Hello world!` }));
})

app.listen(port, () => {
Expand Down
12 changes: 10 additions & 2 deletions examples/nextjs-bot-categories/app/api/arcjet/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,16 @@ export async function GET(req: Request) {
if (decision.reason.isBot()) {
// WARNING: This is illustrative! Don't share this metadata with users;
// otherwise they may use it to subvert bot detection!
headers.set("X-Arcjet-Bot-Allowed", decision.reason.allowed.join(", "))
headers.set("X-Arcjet-Bot-Denied", decision.reason.denied.join(", "))
headers.set("X-Arcjet-Bot-Allowed", decision.reason.allowed.join(", "))
headers.set("X-Arcjet-Bot-Denied", decision.reason.denied.join(", "))

// We need to check that the bot is who they say they are.
if (decision.reason.isSpoofed()) {
return NextResponse.json(
{ error: "You are pretending to be a good bot!" },
{ status: 403, headers },
);
}
}

if (decision.isDenied()) {
Expand Down

0 comments on commit 29fe5b3

Please sign in to comment.