🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
/**
* @vitest-environment node
*/
import { readFile } from 'node:fs/promises';
import { NextRequest } from 'next/server';
import { describe, expect, it, vi } from 'vitest';
import { config as proxyConfig } from '@/proxy';
import { createRouteMatcher } from './createRouteMatcher';
import { defineConfig } from './define-config';
vi.mock('@/auth', () => ({
auth: { api: { getSession: vi.fn().mockResolvedValue({ user: { id: 'user-1' } }) } },
}));
const { middleware } = defineConfig();
const run = async (url: string, userAgent?: string) => {
const res = await middleware(
new NextRequest(url, userAgent ? { headers: { 'user-agent': userAgent } } : undefined),
);
return res?.headers.get('x-middleware-rewrite');
};
const MOBILE_USER_AGENT =
'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148 Safari/604.1';
describe('defineConfig locale path-traversal hardening', () => {
it('rewrites a normal locale into /spa-auth/<locale>', async () => {
const rewrite = await run('http://localhost:3010/signin?hl=ja-JP');
expect(new URL(rewrite!).pathname).toBe('/spa-auth/ja-JP/signin');
});
it('falls back to en-US for a traversal locale (plain)', async () => {
const rewrite = await run('http://localhost:3010/signin?hl=../../api/dev/x');
const { pathname } = new URL(rewrite!);
expect(pathname.startsWith('/spa-auth/')).toBe(true);
expect(pathname).toBe('/spa-auth/en-US/signin');
});
it('falls back to en-US for a traversal locale (percent-encoded)', async () => {
const rewrite = await run('http://localhost:3010/signin?hl=..%2F..%2Fapi%2Fdev%2Fx');
const { pathname } = new URL(rewrite!);
expect(pathname.startsWith('/spa-auth/')).toBe(true);
expect(pathname).toBe('/spa-auth/en-US/signin');
});
it('does not treat workspace slugs beginning with an auth route as auth SPA pages', async () => {
const rewrite = await run(
'http://localhost:3010/oauth-preview-e2e-20260716/settings/oauth-apps?hl=en-US',
);
expect(new URL(rewrite!).pathname).toMatch(
/^\/spa\/[^/]+\/oauth-preview-e2e-20260716\/settings\/oauth-apps$/,
);
});
});
describe('defineConfig Workbench SPA rewrite', () => {
it('routes verify through Workbench for every user agent', async () => {
const mobileVerify = await run(
'http://localhost:3010/verify/run-1?hl=en-US',
MOBILE_USER_AGENT,
);
const desktopVerify = await run('http://localhost:3010/verify/run-1?hl=en-US');
expect(new URL(mobileVerify!).pathname).toBe('/spa-workbench/en-US/verify/run-1');
expect(new URL(desktopVerify!).pathname).toBe('/spa-workbench/en-US/verify/run-1');
});
it('keeps acceptance on the main SPA', async () => {
const mobileAcceptance = await run(
'http://localhost:3010/acceptance/acceptance-1?hl=en-US',
MOBILE_USER_AGENT,
);
const desktopAcceptance = await run('http://localhost:3010/acceptance/acceptance-1?hl=en-US');
expect(new URL(mobileAcceptance!).pathname).toMatch(/^\/spa\/[^/]+\/acceptance\/acceptance-1$/);
expect(new URL(desktopAcceptance!).pathname).toMatch(
/^\/spa\/[^/]+\/acceptance\/acceptance-1$/,
);
});
it('keeps the agent documents index in the Main Mobile SPA', async () => {
const detail = await run(
'http://localhost:3010/agent/agt_1/docs/doc_1?hl=en-US',
MOBILE_USER_AGENT,
);
const index = await run('http://localhost:3010/agent/agt_1/docs?hl=en-US', MOBILE_USER_AGENT);
expect(new URL(detail!).pathname).toBe('/spa-workbench/en-US/agent/agt_1/docs/doc_1');
expect(new URL(index!).pathname).toMatch(/^\/spa\/[^/]+\/agent\/agt_1\/docs$/);
});
});
describe('defineConfig Share SPA rewrite', () => {
it('routes share pages through the Share SPA for every user agent', async () => {
const mobileTopic = await run(
'http://localhost:3010/share/t/topic-1?hl=en-US',
MOBILE_USER_AGENT,
);
const desktopTopic = await run('http://localhost:3010/share/t/topic-1?hl=en-US');
const desktopPage = await run('http://localhost:3010/share/page/docs_1?hl=en-US');
const desktopArtifact = await run('http://localhost:3010/share/artifact/42?hl=en-US');
expect(new URL(mobileTopic!).pathname).toBe('/spa-share/en-US/share/t/topic-1');
expect(new URL(desktopTopic!).pathname).toBe('/spa-share/en-US/share/t/topic-1');
expect(new URL(desktopPage!).pathname).toBe('/spa-share/en-US/share/page/docs_1');
expect(new URL(desktopArtifact!).pathname).toBe('/spa-share/en-US/share/artifact/42');
});
it('leaves non-share paths that merely start with the prefix in the main SPA', async () => {
const rewrite = await run('http://localhost:3010/shared-workspace/settings?hl=en-US');
expect(new URL(rewrite!).pathname).toMatch(/^\/spa\/[^/]+\/shared-workspace\/settings$/);
});
});
describe('Acceptance installation guide', () => {
it('serves the public Markdown asset without authentication or SPA rewrites', async () => {
const { auth } = await import('@/auth');
vi.mocked(auth.api.getSession).mockClear();
const response = await middleware(new NextRequest('http://localhost:3010/acceptance/skill.md'));
expect(response?.headers.get('x-middleware-next')).toBe('1');
expect(response?.headers.get('x-middleware-rewrite')).toBeNull();
expect(response?.headers.get('location')).toBeNull();
expect(auth.api.getSession).not.toHaveBeenCalled();
const guide = await readFile('public/acceptance/skill.md', 'utf8');
expect(guide).toContain('npm install -g @lobehub/cli');
expect(guide).toContain('lh login');
expect(guide).toContain('lh acceptance install');
expect(guide).toContain('.agents/skills/acceptance/SKILL.md');
});
});
describe('OIDC protocol endpoints', () => {
const callMiddleware = async (path: string) => {
const { auth } = await import('@/auth');
vi.mocked(auth.api.getSession).mockClear();
const response = await middleware(new NextRequest(`http://localhost:3010${path}`));
return { getSession: vi.mocked(auth.api.getSession), response };
};
it.each([
'/oidc/.well-known/openid-configuration',
'/oidc/jwks',
'/oidc/me',
'/oidc/acceptance/callback?error=invalid_target&state=s',
'/oauth/connector/client-metadata.json',
'/oidc/token/revocation',
'/oidc/token/introspection',
])('serves %s without sending an unauthenticated client to sign-in', async (path) => {
const { getSession, response } = await callMiddleware(path);
expect(getSession).not.toHaveBeenCalled();
expect(response?.headers.get('x-middleware-next')).toBe('1');
expect(response?.headers.get('location')).toBeNull();
expect(response?.headers.get('x-middleware-rewrite')).toBeNull();
});
it('still gates the authorization endpoint on a session', async () => {
const { getSession } = await callMiddleware('/oidc/auth?client_id=lca_1&response_type=code');
expect(getSession).toHaveBeenCalled();
});
});
describe('project deep links', () => {
it.each(['/projects', '/project/environment-demo', '/project/environment-demo/tasks'])(
'routes %s into the SPA on a direct request',
async (path) => {
expect(
createRouteMatcher(proxyConfig.matcher)(new NextRequest(`http://localhost:3010${path}`)),
).toBe(true);
const rewrite = await run(`http://localhost:3010${path}`);
expect(new URL(rewrite!).pathname).toMatch(new RegExp(`/spa/[^/]+${path}$`));
},
);
});
describe('channel deep links', () => {
it.each(['/channels', '/channels/chn_123'])(
'keeps %s reachable after a full page reload',
async (path) => {
const request = new NextRequest(`http://localhost:3010${path}`);
expect(createRouteMatcher(proxyConfig.matcher)(request)).toBe(true);
expect(new URL((await run(request.url))!).pathname).toMatch(new RegExp(`/spa/[^/]+${path}$`));
},
);
});