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

Unable to access NODE_ENV or ARCJET_ENV from Gatsby #2986

Open
davidmytton opened this issue Jan 24, 2025 · 0 comments
Open

Unable to access NODE_ENV or ARCJET_ENV from Gatsby #2986

davidmytton opened this issue Jan 24, 2025 · 0 comments

Comments

@davidmytton
Copy link
Contributor

If you use Arcjet within a Gatsby function, the Arcjet client does not pick up any environment variables.

This means that when running locally, it will always log the ✦Aj WARN Client IP address is missing. If this is a dev environment set the ARCJET_ENV env var to "development" warning and fail to build the characteristics with ✦Aj ERROR Failed to build fingerprint. Please verify your Characteristics. because it can't find a valid IP address.

I am able to access the environment variables from within the function so I can set process.env.ARCJET_KEY with a normal instantiation of the client. I tried placing the client inside and outside of the handler, but it still seems unable to detect the environment variables. A console.log shows they are all set.

As a workaround I can hard code the local IP if we detect we're in development:

const ip = process.env.NODE_ENV === 'development' ? '1.1.1.1' : undefined;
const decision = await aj.protect(ip ? { ...req, socket: { remoteAddress: ip } } : req);

Here's a simple Gatsby function demonstrating the issue. It can be placed in src/api/arcjet.ts to be accessed from http://localhost:8000/api/arcjet

import arcjet, { slidingWindow } from "@arcjet/node";
import type { GatsbyFunctionRequest, GatsbyFunctionResponse } from 'gatsby';

const aj = arcjet({
    key: process.env.ARCJET_KEY!,
    rules: [
        slidingWindow({
            mode: "LIVE",
            interval: "10m",
            max: 500,
        }),
    ],
});

const handler = async (
    req: GatsbyFunctionRequest,
    res: GatsbyFunctionResponse
) => {
    const decision = await aj.protect(req);
    console.log("IP Address", req.socket.remoteAddress);
    console.log("Arcjet ENV", process.env.ARCJET_ENV);

    return res.json({ arcjet: decision });
}


export default handler;

The output is:

warn ✦Aj WARN Client IP address is missing. If this is a dev environment set the ARCJET_ENV env var to "development"
Executed function "/api/arcjet" in 5ms

 ERROR  UNKNOWN

✦Aj ERROR Failed to build fingerprint. Please verify your Characteristics.
      error: {}

IP Address ::1
Arcjet ENV development
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant