Something went wrong. Try again.
An experimental Wallabag client
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162import { describe, it, expect, vi, afterEach } from 'vitest';import worker from './index';
const env = { RATE_LIMITER: { limit: vi.fn().mockResolvedValue({ success: true }) }} as unknown as Parameters<typeof worker.fetch>[1];const ctx = { waitUntil: vi.fn(), passThroughOnException: vi.fn() } as unknown as ExecutionContext;
function req(url: string, method = 'GET', body?: string): Request { return new Request(url, { method, body, headers: { 'Content-Type': 'application/json' } });}
afterEach(() => { vi.unstubAllGlobals();});
describe('OPTIONS preflight', () => { it('returns 204 with all CORS headers', async () => { const res = await worker.fetch(req('https://proxy.example.com/proxy', 'OPTIONS'), env, ctx); expect(res.status).toBe(204); expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*'); expect(res.headers.get('Access-Control-Allow-Methods')).toContain('GET'); expect(res.headers.get('Access-Control-Allow-Methods')).toContain('PATCH'); expect(res.headers.get('Access-Control-Allow-Headers')).toContain('Authorization'); });});
describe('path validation', () => { it('returns 404 for paths other than /proxy', async () => { const res = await worker.fetch(req('https://proxy.example.com/other'), env, ctx); expect(res.status).toBe(404); });
it('returns 404 for root path', async () => { const res = await worker.fetch(req('https://proxy.example.com/'), env, ctx); expect(res.status).toBe(404); });});
describe('target validation', () => { it('returns 400 when target param is missing', async () => { const res = await worker.fetch(req('https://proxy.example.com/proxy'), env, ctx); expect(res.status).toBe(400); });
it('returns 400 for non-HTTPS target', async () => { const target = encodeURIComponent('http://wallabag.example.com/api/entries'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(400); expect(await res.text()).toMatch(/https/i); });
it('returns 400 for localhost target', async () => { const target = encodeURIComponent('https://localhost/api/entries'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(400); });
it('returns 400 for 127.0.0.1 target', async () => { const target = encodeURIComponent('https://127.0.0.1/api/entries'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(400); });
it('returns 400 for RFC1918 10.x.x.x target', async () => { const target = encodeURIComponent('https://10.0.0.1/api/entries'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(400); });
it('returns 400 for RFC1918 192.168.x.x target', async () => { const target = encodeURIComponent('https://192.168.1.1/api/entries'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(400); });
it('returns 400 for .local hostname', async () => { const target = encodeURIComponent('https://mywallabag.local/api/entries'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(400); });
it('returns 400 for path not starting with /api/ or /oauth/', async () => { const target = encodeURIComponent('https://wallabag.example.com/admin/settings'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(400); });
it('allows /api/ paths on public hostnames', async () => { vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue(new Response('{"ok":true}', { status: 200 })) ); const target = encodeURIComponent('https://wallabag.example.com/api/entries'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(200); });
it('allows /oauth/ paths on public hostnames', async () => { vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue(new Response('{}', { status: 200 })) ); const target = encodeURIComponent('https://wallabag.example.com/oauth/v2/token'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(200); });});
describe('CORS headers on proxied responses', () => { it('adds CORS headers to a successful upstream response', async () => { vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue( new Response('{"data":1}', { status: 200, headers: { 'content-type': 'application/json' } }) ) ); const target = encodeURIComponent('https://wallabag.example.com/api/entries'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(200); expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*'); expect(res.headers.get('content-type')).toBe('application/json'); });
it('forwards upstream error status codes with CORS headers', async () => { vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue(new Response('Unauthorized', { status: 401 })) ); const target = encodeURIComponent('https://wallabag.example.com/api/entries'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(401); expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*'); });
it('returns 502 when upstream fetch throws', async () => { vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('Connection refused'))); const target = encodeURIComponent('https://wallabag.example.com/api/entries'); const res = await worker.fetch(req(`https://proxy.example.com/proxy?target=${target}`), env, ctx); expect(res.status).toBe(502); });});
describe('rate limiting', () => { it('returns 429 with CORS headers when limiter rejects', async () => { const throttledEnv = { RATE_LIMITER: { limit: vi.fn().mockResolvedValue({ success: false }) } } as unknown as Parameters<typeof worker.fetch>[1]; const target = encodeURIComponent('https://wallabag.example.com/api/entries'); const res = await worker.fetch( req(`https://proxy.example.com/proxy?target=${target}`), throttledEnv, ctx ); expect(res.status).toBe(429); expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*'); });});