Skip to content

fix(endpoint-auth): return after passing secret error to next() - #871

Merged
paulrobertlloyd merged 1 commit into
getindiekit:mainfrom
rmdes:fix/secret-middleware-double-next
Aug 15, 2026
Merged

fix(endpoint-auth): return after passing secret error to next()#871
paulrobertlloyd merged 1 commit into
getindiekit:mainfrom
rmdes:fix/secret-middleware-double-next

Conversation

@rmdes

@rmdes rmdes commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Problem

hasSecret passes an error to next() when SECRET is unset, but doesn't
return, so execution falls through to the unconditional next() on the
following line:

if (!process.env.SECRET) {
  const error = IndiekitError.notImplemented(
    response.locals.__("NotImplementedError.secret"),
  );

  next(error);   // ← dispatches to the error handler
}

next();          // ← …and then continues down the normal stack

Express treats those as two separate dispatches: the first jumps to the
error-handling middleware, the second carries on through the normal stack.
Both run for the same request, so the misconfiguration is reported and the
route executes anyway — ending in a doubled response or
ERR_HTTP_HEADERS_SENT, depending on which finishes first.

Fix

Return the error dispatch so the middleware stops there.

-    next(error);
+    return next(error);

Notes

Only reachable when the server is started without SECRET, so it doesn't
affect a correctly configured instance — but it turns a clear "secret not
set" message into a confusing double-handled request, which is exactly the
wrong experience for someone in the middle of setting Indiekit up.

Found while working on the endpoint-auth package for a separate
redirect_uri fix; kept as its own commit since the two are unrelated.

@paulrobertlloyd
paulrobertlloyd force-pushed the main branch 2 times, most recently from ade3f7b to 2b7c903 Compare August 15, 2026 15:39
hasSecret calls next(error) when SECRET is unset, then falls through and
calls next() again. Express dispatches to the error handler on the first
call and continues down the normal stack on the second, so the request is
handled twice — rendering the error and then running the route, or
failing with ERR_HTTP_HEADERS_SENT depending on which finishes first.

Return the error call so the middleware stops there.
@paulrobertlloyd
paulrobertlloyd force-pushed the fix/secret-middleware-double-next branch from 3c0904a to bac0f1f Compare August 15, 2026 15:52
@paulrobertlloyd
paulrobertlloyd merged commit ad11106 into getindiekit:main Aug 15, 2026
1 check failed
@paulrobertlloyd

Copy link
Copy Markdown
Collaborator

Thanks @rmdes (and @aciccarello for the review)

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

Successfully merging this pull request may close these issues.

3 participants