Skip to content

fix: bypass auth middleware for REST API v1 and agent endpoints#48

Merged
TerrifiedBug merged 1 commit intomainfrom
fix/api-v1-middleware-bypass
Mar 7, 2026
Merged

fix: bypass auth middleware for REST API v1 and agent endpoints#48
TerrifiedBug merged 1 commit intomainfrom
fix/api-v1-middleware-bypass

Conversation

@TerrifiedBug
Copy link
Copy Markdown
Owner

Summary

  • The Next.js auth middleware was intercepting /api/v1/* and /api/agent/* requests, redirecting them to /login (HTTP 307) before the route handlers could process Bearer token / enrollment token authentication
  • Added both paths to the authorized callback bypass in auth.config.ts
  • Restored src/middleware.ts (previously deleted) with correct matcher exclusions for api/v1 and api/agent

Test plan

  • Deploy and test GET /api/v1/pipelines with a service account Bearer token — should return JSON instead of 307 redirect
  • Verify GET /api/v1/nodes, /api/v1/secrets, /api/v1/alerts/rules, /api/v1/audit all work
  • Verify agent enrollment endpoint still works
  • Verify unauthenticated requests to /api/v1/* return 401 (not 307 redirect)
  • Verify web UI pages still redirect to login when not authenticated

The Next.js auth middleware was intercepting /api/v1/* and /api/agent/*
requests and redirecting them to /login before route handlers could
process Bearer token authentication. This adds both paths to the
middleware bypass list and restores the middleware.ts entry point
(previously deleted) with the correct matcher exclusions.
@github-actions github-actions bot added the fix label Mar 7, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 7, 2026

Greptile Summary

This PR fixes a regression where Next.js auth middleware was issuing HTTP 307 redirects to /login for /api/v1/* and /api/agent/* requests, breaking Bearer token and enrollment token clients that can't follow HTML redirects. The fix is applied at two layers: the middleware.ts matcher now excludes those path prefixes so the Edge middleware never runs for them, and the authorized callback in auth.config.ts is updated with matching bypass conditions as a defensive fallback (relevant when authConfig is reused in server-side auth() calls via auth.ts).

Key observations:

  • Correct approach: Both changed paths have their own auth — /api/v1/* enforces Bearer token validation via authenticateApiKey in api-handler.ts (returns 401 on failure), and /api/agent/* routes verify enrollment/node tokens directly in their handlers.
  • Two-layer defense: The matcher exclusion is the primary gate (middleware doesn't run at all); the authorized callback bypass is a redundant-but-safe backstop, consistent with the shared authConfig pattern.
  • Matcher breadth: The negative-lookahead pattern (?!...api/v1...) will also exclude any hypothetical future path whose post-slash prefix matches api/v1 (e.g., /api/v10). This is unlikely to be a problem in practice but worth keeping in mind as the API evolves.

Confidence Score: 4/5

  • Safe to merge — the bypass is correct, both affected path groups enforce their own authentication, and no unauthenticated access is introduced.
  • The changes are minimal, targeted, and well-understood. The underlying API routes already perform proper token validation, so removing the NextAuth session check doesn't open an unguarded path. The matcher regex and the authorized callback are consistent with each other. Minor deduction for the matcher's prefix-based exclusion potentially being slightly over-broad if new /api/v1x routes are added in the future.
  • No files require special attention.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    REQ[Incoming Request] --> MATCHER{middleware.ts matcher\nexcludes api/v1, api/agent?}
    MATCHER -- Yes, excluded --> HANDLER[Route Handler]
    MATCHER -- No, runs middleware --> AUTH_CB{authorized callback\nin auth.config.ts}
    AUTH_CB -- isApiV1 or isAgentApi --> HANDLER
    AUTH_CB -- isAuthPage / isHealth / isSetupApi --> HANDLER
    AUTH_CB -- not logged in --> LOGIN[Redirect to /login 307]
    AUTH_CB -- logged in --> HANDLER

    HANDLER --> API_V1{/api/v1/* ?}
    HANDLER --> AGENT{/api/agent/* ?}
    HANDLER --> OTHER[Other authenticated route]

    API_V1 --> BEARER[authenticateApiKey\nBearer token check\n→ 401 if missing/invalid]
    AGENT --> ENROLL_TOKEN[verifyEnrollmentToken\nor node Bearer token\n→ 401 if invalid]
Loading

Last reviewed commit: 00ad0c7

@TerrifiedBug TerrifiedBug merged commit 1bfd0a5 into main Mar 7, 2026
12 checks passed
@TerrifiedBug TerrifiedBug deleted the fix/api-v1-middleware-bypass branch March 7, 2026 17:54
TerrifiedBug added a commit that referenced this pull request Mar 7, 2026
Next.js 16 replaced middleware.ts with proxy.ts. PR #48 reintroduced
middleware.ts alongside the existing proxy.ts, causing the Docker
build to fail with "Both middleware file and proxy file are detected".

- Delete src/middleware.ts
- Add api/v1 exclusion to proxy.ts matcher (was only in middleware.ts)
TerrifiedBug added a commit that referenced this pull request Mar 7, 2026
Next.js 16 replaced middleware.ts with proxy.ts. PR #48 reintroduced
middleware.ts alongside the existing proxy.ts, causing the Docker
build to fail with "Both middleware file and proxy file are detected".

- Delete src/middleware.ts
- Add api/v1 exclusion to proxy.ts matcher (was only in middleware.ts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant