Something went wrong. Try again.
A local-first, resumable GitHub-to-Tangled issue migrator. Built with Next.js and shadcn/ui. MIT licensed.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899import { afterEach, describe, expect, test } from 'bun:test';import { mkdtempSync, existsSync, rmSync, statSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { spawnSync } from 'node:child_process';import { NextRequest } from 'next/server';import { proxy } from '../proxy';import { localRuntime } from './runtime';import { GET, POST } from '../app/api/[...path]/route';
const names = ['TANGLED_MIGRATOR_ORIGIN', 'TANGLED_MIGRATOR_STATE_DIR', 'TANGLED_MIGRATOR_NONCE', 'NODE_ENV'];const previous = Object.fromEntries(names.map(name => [name, process.env[name]]));const directories: string[] = [];const runtimeGlobal = globalThis as typeof globalThis & { tangledMigrator?: ReturnType<typeof localRuntime> };function directory() { const path = mkdtempSync(join(tmpdir(), 'migrator-runtime-')); directories.push(path); return path; }function configure(path = directory()) { process.env.TANGLED_MIGRATOR_ORIGIN = 'http://127.0.0.1:43148'; process.env.TANGLED_MIGRATOR_STATE_DIR = path; process.env.TANGLED_MIGRATOR_NONCE = 'c'.repeat(64); return path;}function request(host = '127.0.0.1:43148') { return new NextRequest('http://127.0.0.1:43148/', { headers: { Host: host } }); }afterEach(() => { delete runtimeGlobal.tangledMigrator; for (const name of names) { if (previous[name] === undefined) delete process.env[name]; else process.env[name] = previous[name]; } for (const path of directories.splice(0)) rmSync(path, { recursive: true, force: true });});
describe('native Next integration', () => { test('rejects an untrusted Host before a page can expose its local nonce', () => { configure(); const denied = proxy(request('rebinding.example')); expect(denied.status).toBe(403); expect(denied.headers.get('x-middleware-next')).toBeNull(); delete process.env.TANGLED_MIGRATOR_ORIGIN; expect(proxy(request()).status).toBe(403); });
test('serves local pages with no-store and frame protections without production eval', () => { configure(); Object.assign(process.env, { NODE_ENV: 'production' }); const allowed = proxy(request()); expect(allowed.headers.get('x-middleware-next')).toBe('1'); expect(allowed.headers.get('Cache-Control')).toBe('no-store'); expect(allowed.headers.get('Referrer-Policy')).toBe('no-referrer'); expect(allowed.headers.get('Content-Security-Policy')).toContain("frame-ancestors 'none'"); expect(allowed.headers.get('Content-Security-Policy')).not.toContain('unsafe-eval'); });
test('requires the local launcher and retains one stateful API handler between Next requests', async () => { delete process.env.TANGLED_MIGRATOR_ORIGIN; delete process.env.TANGLED_MIGRATOR_STATE_DIR; delete process.env.TANGLED_MIGRATOR_NONCE; expect(() => localRuntime()).toThrow('Start the local application'); const path = configure(); const first = localRuntime(); expect(localRuntime()).toBe(first); expect(statSync(path).mode & 0o777).toBe(0o700); const denied = await GET(new Request('http://127.0.0.1:43148/api/state', { headers: { Host: '127.0.0.1:43148' } })); expect(denied.status).toBe(403); const response = await GET(new Request('http://127.0.0.1:43148/api/state', { headers: { Host: '127.0.0.1:43148', 'X-Local-Nonce': 'c'.repeat(64) } })); expect(response.status).toBe(200); expect((await response.json()).configuration).toBeNull(); expect(response.headers.get('Cache-Control')).toBe('no-store'); });
test('canonicalizes Next internal origins only after checking the public Host', async () => { configure(); const headers = { Host: '127.0.0.1:43148', 'X-Local-Nonce': 'c'.repeat(64) }; const response = await GET(new Request('http://localhost:43148/api/state', { headers })); expect(response.status).toBe(200); expect((await response.json()).configuration).toBeNull(); const hostile = await GET(new Request('http://localhost:43148/api/state', { headers: { ...headers, Host: 'attacker.example' } })); expect(hostile.status).toBe(403); });
test('preserves streamed request bodies across Next origin normalization', async () => { configure(); const body = new ReadableStream<Uint8Array>({ start(controller) { controller.enqueue(new TextEncoder().encode('{')); controller.close(); } }); const init: RequestInit & { duplex: 'half' } = { method: 'POST', body, duplex: 'half', headers: { Host: '127.0.0.1:43148', Origin: 'http://127.0.0.1:43148', 'X-Local-Nonce': 'c'.repeat(64), 'Content-Type': 'application/json' } }; const response = await POST(new Request('http://localhost:43148/api/configure', init)); expect(response.status).toBe(400); expect((await response.json()).error.code).toBe('INVALID_JSON'); });
test('launcher help and invalid ports do not initialize private state or start Next', () => { const path = join(directory(), 'not-created'); const launcher = join(import.meta.dir, 'index.ts'); const options = { encoding: 'utf8' as const, timeout: 10_000, env: { PATH: process.env.PATH ?? '', NODE_ENV: 'test' as const } }; const help = spawnSync(process.execPath, [launcher, '--help', '--state-dir', path], options); expect(help.status).toBe(0); expect(help.stdout).toContain('Runs Next.js only on 127.0.0.1'); expect(existsSync(path)).toBe(false); const invalid = spawnSync(process.execPath, [launcher, '--port', '1', '--state-dir', path], options); expect(invalid.status).not.toBe(0); expect(invalid.stderr).toContain('Port must be between 1024 and 65535'); expect(existsSync(path)).toBe(false); });});