arcjet-js: Astro: Redirects cause adapter to fail

It seems that redirects defined in astro.config.mjs cause our adapter to fail. This is due to the context.isPrerendered flag being set to false yet context.clientAddress throws when accessed. This generally makes sense because a redirect doesn’t produce a static page itself.

Astro doesn’t seem to have anything like isRedirect on the context but you can discover redirects during the build process. I experimented with using codegen to make an isDynamic function that consumed the discovered redirects but I’m not sure I’m happy with users needed to import the utility explictly.

Instead, we could use this generated logic to warn and produce an Allow decision on static pages. Alternatively, we could produce an Error decision like we do when IP isn’t defined. I’m not sure which is better.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21 (21 by maintainers)

Commits related to this issue

Most upvoted comments

This was mostly solved by @qw-in fixing the bug upstream in Astro, but we’ve also added a warning.

I’ll be bumping the peerDep minimum to help guide users to the fixed version.

Feel free to submit it, but I’m pretty staunchly against us supplying APIs for bugs in a framework. I think our time would be better spent fixing the bug in Astro.

thoughts on a small export from the sdk to enable this now? It should never be hit in production (at runtime I mean)

I already documented this in the root issue:

I experimented with using codegen to make an isDynamic function that consumed the discovered redirects but I’m not sure I’m happy with users needed to import the utility explictly.

Found it, working on repro now

21:43:07 ▶ /bot-protection/bot-types
21:43:07   └─ /bot-protection/bot-types/index.html[StaticClientAddressNotAvailable] `Astro.clientAddress` is only available on pages that are server-rendered.
  Hint:
    See https://docs.astro.build/en/guides/on-demand-rendering/ for more information on how to enable SSR.
  Error reference:
    https://docs.astro.build/en/reference/errors/static-client-address-not-available/
  Location:
    /workspaces/arcjet-docs/node_modules/astro/dist/core/render-context.js:520:11
  Stack trace:
    at RenderContext.getClientAddress (file:///workspaces/arcjet-docs/node_modules/astro/dist/core/render-context.js:520:11)
    at file:///workspaces/arcjet-docs/dist/_astro-internal_middleware.mjs:651:48
    at file:///workspaces/arcjet-docs/dist/_astro-internal_middleware.mjs:286:18
    at applyHandle (file:///workspaces/arcjet-docs/dist/_astro-internal_middleware.mjs:248:22)
    at applyHandle (file:///workspaces/arcjet-docs/node_modules/astro/dist/core/middleware/sequence.js:21:22)

@qw-in Thanks for trying to reproduce! I wonder if they inadvertently fixed the problem with some other change. I grepped their changelog and saw this mention of clientAddress. Maybe it solved the issue—it may be useful to bisect the Astro versions to know which is the known-good-version.