Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .changeset/floppy-dogs-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
"@voltagent/server-core": patch
"@voltagent/server-hono": patch
---

feat: add authNext and deprecate legacy auth

Add a new `authNext` policy that splits routes into public, console, and user access. All routes are protected by default; use `publicRoutes` to opt out.

AuthNext example:

```ts
import { jwtAuth } from "@voltagent/server-core";
import { honoServer } from "@voltagent/server-hono";

const server = honoServer({
authNext: {
provider: jwtAuth({ secret: process.env.JWT_SECRET! }),
publicRoutes: ["GET /health"],
},
});
```

Behavior summary:

- When `authNext` is set, all routes are private by default.
- Console endpoints (agents, workflows, tools, docs, observability, updates) require a Console Access Key.
- Execution endpoints require a user token (JWT).

Console access uses `VOLTAGENT_CONSOLE_ACCESS_KEY`:

```bash
VOLTAGENT_CONSOLE_ACCESS_KEY=your-console-key
```

```bash
curl http://localhost:3141/agents \
-H "x-console-access-key: your-console-key"
```

Legacy `auth` remains supported but is deprecated. Use `authNext` for new integrations.
13 changes: 7 additions & 6 deletions examples/with-auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { openai } from "@ai-sdk/openai";
import { Agent, Memory, VoltAgent } from "@voltagent/core";
import { createPinoLogger } from "@voltagent/logger";
import { honoServer, jwtAuth } from "@voltagent/server-hono";
import { authNext, honoServer, jwtAuth } from "@voltagent/server-hono";

// Import Memory and TelemetryStore from core
import { AiSdkEmbeddingAdapter, InMemoryVectorAdapter } from "@voltagent/core";
Expand Down Expand Up @@ -34,11 +34,12 @@ const agent = new Agent({
new VoltAgent({
agents: { agent },
server: honoServer({
auth: jwtAuth({
secret: "super-secret",
defaultPrivate: true,
publicRoutes: ["GET /api/health"],
}),
authNext: {
provider: jwtAuth({
secret: "super-secret",
}),
publicRoutes: ["/api/health"],
},
configureApp: (app) => {
app.get("/api/health", (c) => c.json({ status: "ok" }));
app.get("/api/protected", (c) => c.json({ message: "This is protected" }));
Expand Down
1 change: 1 addition & 0 deletions packages/server-core/src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export * from "./types";
export * from "./defaults";
export * from "./next";
export * from "./utils";

// Export auth providers
Expand Down
47 changes: 47 additions & 0 deletions packages/server-core/src/auth/next.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, it } from "vitest";
import { resolveAuthNextAccess } from "./next";
import type { AuthProvider } from "./types";

const mockProvider: AuthProvider = {
type: "jwt",
verifyToken: async () => ({ id: "user-1" }),
publicRoutes: ["GET /provider-public"],
};

describe("resolveAuthNextAccess", () => {
it("treats explicit publicRoutes as public", () => {
const config = { provider: mockProvider, publicRoutes: ["GET /health"] };
expect(resolveAuthNextAccess("GET", "/health", config)).toBe("public");
});

it("treats provider publicRoutes as public", () => {
const config = { provider: mockProvider };
expect(resolveAuthNextAccess("GET", "/provider-public", config)).toBe("public");
});

it("treats default console routes as console", () => {
const config = { provider: mockProvider };
expect(resolveAuthNextAccess("GET", "/agents", config)).toBe("console");
});

it("treats non-console routes as user", () => {
const config = { provider: mockProvider };
expect(resolveAuthNextAccess("POST", "/agents/test-agent/text", config)).toBe("user");
});

it("treats websocket test connection as console", () => {
const config = { provider: mockProvider };
expect(resolveAuthNextAccess("WS", "/ws", config)).toBe("console");
});

it("allows publicRoutes to override console routes", () => {
const config = { provider: mockProvider, publicRoutes: ["GET /agents"] };
expect(resolveAuthNextAccess("GET", "/agents", config)).toBe("public");
});

it("uses custom consoleRoutes when provided", () => {
const config = { provider: mockProvider, consoleRoutes: ["GET /custom-console"] };
expect(resolveAuthNextAccess("GET", "/custom-console", config)).toBe("console");
expect(resolveAuthNextAccess("GET", "/agents", config)).toBe("user");
});
});
85 changes: 85 additions & 0 deletions packages/server-core/src/auth/next.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { DEFAULT_PUBLIC_ROUTES, pathMatches } from "./defaults";
import type { AuthProvider } from "./types";

export type AuthNextAccess = "public" | "console" | "user";

export interface AuthNextRoutesConfig {
publicRoutes?: string[];
consoleRoutes?: string[];
}

export interface AuthNextConfig<TRequest = any> extends AuthNextRoutesConfig {
provider: AuthProvider<TRequest>;
}

export function isAuthNextConfig<TRequest>(
value: AuthProvider<TRequest> | AuthNextConfig<TRequest>,
): value is AuthNextConfig<TRequest> {
return typeof (value as AuthNextConfig<TRequest>).provider !== "undefined";
}

export function normalizeAuthNextConfig<TRequest>(
value: AuthProvider<TRequest> | AuthNextConfig<TRequest>,
): AuthNextConfig<TRequest> {
return isAuthNextConfig(value) ? value : { provider: value };
}

/**
* Console routes require console access when authNext is enabled.
*/
export const DEFAULT_CONSOLE_ROUTES = [
...DEFAULT_PUBLIC_ROUTES,
"GET /agents/:id/history",
"GET /workflows/executions",
"GET /workflows/:id/executions/:executionId/state",
"GET /api/logs",
"POST /setup-observability",
"/observability/*",
"GET /updates",
"POST /updates",
"POST /updates/:packageName",
"WS /ws",
"WS /ws/logs",
"WS /ws/observability/**",
];

function routeMatches(method: string, path: string, routePattern: string): boolean {
const parts = routePattern.split(" ");
if (parts.length === 2) {
const [routeMethod, routePath] = parts;
if (method.toUpperCase() !== routeMethod.toUpperCase()) {
return false;
}
return pathMatches(path, routePath);
}

return pathMatches(path, routePattern);
}

function matchesAnyRoute(method: string, path: string, routes?: string[]): boolean {
if (!routes || routes.length === 0) {
return false;
}

return routes.some((route) => routeMatches(method, path, route));
}

export function resolveAuthNextAccess<TRequest>(
method: string,
path: string,
authNext: AuthNextConfig<TRequest> | AuthProvider<TRequest>,
): AuthNextAccess {
const config = normalizeAuthNextConfig(authNext);
const publicRoutes = [...(config.publicRoutes ?? []), ...(config.provider.publicRoutes ?? [])];

if (matchesAnyRoute(method, path, publicRoutes)) {
return "public";
}

const consoleRoutes = config.consoleRoutes ?? DEFAULT_CONSOLE_ROUTES;
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Inconsistent route configuration behavior: publicRoutes merges config with provider routes, but consoleRoutes completely replaces defaults when provided. If a user sets consoleRoutes: ["/my-admin"], they'll lose all DEFAULT_CONSOLE_ROUTES (like "WS /ws", "GET /api/logs", etc.). Consider merging instead: const consoleRoutes = [...DEFAULT_CONSOLE_ROUTES, ...(config.consoleRoutes ?? [])];

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/server-core/src/auth/next.ts, line 79:

<comment>Inconsistent route configuration behavior: `publicRoutes` merges config with provider routes, but `consoleRoutes` completely replaces defaults when provided. If a user sets `consoleRoutes: [&quot;/my-admin&quot;]`, they&#39;ll lose all DEFAULT_CONSOLE_ROUTES (like &quot;WS /ws&quot;, &quot;GET /api/logs&quot;, etc.). Consider merging instead: `const consoleRoutes = [...DEFAULT_CONSOLE_ROUTES, ...(config.consoleRoutes ?? [])];`</comment>

<file context>
@@ -0,0 +1,85 @@
+    return &quot;public&quot;;
+  }
+
+  const consoleRoutes = config.consoleRoutes ?? DEFAULT_CONSOLE_ROUTES;
+  if (matchesAnyRoute(method, path, consoleRoutes)) {
+    return &quot;console&quot;;
</file context>
Fix with Cubic

if (matchesAnyRoute(method, path, consoleRoutes)) {
return "console";
}

return "user";
}
7 changes: 6 additions & 1 deletion packages/server-core/src/auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export function isDevRequest(req: Request): boolean {
* // Production with console key
* NODE_ENV=production + x-console-access-key=valid-key → true
*
* // Production with console key in query param
* NODE_ENV=production + ?key=valid-key → true
*
* // Production without key
* NODE_ENV=production + no key → false
*
Expand All @@ -68,9 +71,11 @@ export function hasConsoleAccess(req: Request): boolean {

// 2. Console Access Key check (for production)
const consoleKey = req.headers.get("x-console-access-key");
const url = new URL(req.url, "http://localhost");
const queryKey = url.searchParams.get("key");
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Security: Passing access keys in URL query parameters exposes credentials to logging, browser history, Referer headers, and proxy caches. Sensitive authentication tokens should only be transmitted via headers (like the existing x-console-access-key header) or POST body, not in URLs. Consider removing query parameter authentication for production security.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/server-core/src/auth/utils.ts, line 75:

<comment>Security: Passing access keys in URL query parameters exposes credentials to logging, browser history, Referer headers, and proxy caches. Sensitive authentication tokens should only be transmitted via headers (like the existing `x-console-access-key` header) or POST body, not in URLs. Consider removing query parameter authentication for production security.</comment>

<file context>
@@ -68,9 +71,11 @@ export function hasConsoleAccess(req: Request): boolean {
   // 2. Console Access Key check (for production)
   const consoleKey = req.headers.get(&quot;x-console-access-key&quot;);
+  const url = new URL(req.url, &quot;http://localhost&quot;);
+  const queryKey = url.searchParams.get(&quot;key&quot;);
   const configuredKey = process.env.VOLTAGENT_CONSOLE_ACCESS_KEY;
 
</file context>
Fix with Cubic

const configuredKey = process.env.VOLTAGENT_CONSOLE_ACCESS_KEY;

if (configuredKey && consoleKey === configuredKey) {
if (configuredKey && (consoleKey === configuredKey || queryKey === configuredKey)) {
return true;
}

Expand Down
Loading