refactor(web): migrate to Bun and TypeScript with README screenshots
This commit is contained in:
parent
87372041dd
commit
d6a9745df0
28 changed files with 3152 additions and 386 deletions
|
|
@ -1,43 +0,0 @@
|
|||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { request, isSession, safeAvatar, ApiError } from '../src/api.js';
|
||||
import { messages, detectLanguage } from '../src/i18n.js';
|
||||
|
||||
test('catalogs match and never silently fall back to a missing translation',()=>{
|
||||
assert.deepEqual(Object.keys(messages.ru).sort(),Object.keys(messages.en).sort());
|
||||
for (const catalog of Object.values(messages)) for (const value of Object.values(catalog)) assert.ok(typeof value==='string' && value.length>0);
|
||||
});
|
||||
test('language preference is bounded and follows the OS without storage',()=>{
|
||||
assert.equal(detectLanguage('en',['ru-RU']),'en');
|
||||
assert.equal(detectLanguage('bad',['ru-RU']),'ru');
|
||||
assert.equal(detectLanguage(undefined,['pl-PL']),'en');
|
||||
assert.equal(detectLanguage('RU',['en-US']),'en');
|
||||
});
|
||||
test('API keeps authentication on the same origin and sends CSRF only in header',async()=>{
|
||||
let observed;
|
||||
const result=await request('/v1/auth/logout',{method:'POST',csrf:'fixture-csrf',fetcher:async(path,options)=>{
|
||||
observed={path,options};return {ok:true,status:204};
|
||||
}});
|
||||
assert.equal(result,null);
|
||||
assert.equal(observed.path,'/v1/auth/logout');
|
||||
assert.equal(observed.options.credentials,'same-origin');
|
||||
assert.equal(observed.options.cache,'no-store');
|
||||
assert.equal(observed.options.headers['X-CSRF-Token'],'fixture-csrf');
|
||||
assert.equal(observed.options.method,'POST');
|
||||
await assert.rejects(request('https://evil.example/v1/auth/session'),TypeError);
|
||||
await assert.rejects(request('//evil.example/v1/auth/session'),TypeError);
|
||||
});
|
||||
test('API failures expose a generic code and preserve status for expired sessions',async()=>{
|
||||
await assert.rejects(request('/v1/auth/session',{fetcher:async()=>({ok:false,status:401})}),error=>error instanceof ApiError && error.status===401 && error.message==='request_failed');
|
||||
});
|
||||
test('session validation rejects malformed authenticated payloads',()=>{
|
||||
assert.ok(isSession({authenticated:false}));
|
||||
assert.ok(isSession({authenticated:true,csrf_token:'fixture',user:{user_id:'fixture',handle:'test',display_name:'Test'}}));
|
||||
assert.equal(isSession({authenticated:true,user:{}}),false);
|
||||
assert.equal(isSession({authenticated:'true'}),false);
|
||||
assert.equal(isSession({authenticated:true,csrf_token:'',user:{user_id:'fixture',handle:'test',display_name:'Test'}}),false);
|
||||
});
|
||||
test('avatars allow only uncredentialed HTTPS GitHub image origin',()=>{
|
||||
assert.equal(safeAvatar('https://avatars.githubusercontent.com/u/1?v=4'),'https://avatars.githubusercontent.com/u/1?v=4');
|
||||
for (const url of ['javascript:alert(1)','data:image/svg+xml,<svg/>','http://avatars.githubusercontent.com/u/1','https://avatars.githubusercontent.com.evil.example/u/1','https://evil@avatars.githubusercontent.com/u/1','https://avatars.githubusercontent.com:8443/u/1','https://evil.example/u/1',null]) assert.equal(safeAvatar(url),null);
|
||||
});
|
||||
107
tests/api.test.ts
Normal file
107
tests/api.test.ts
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
import { test } from "bun:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { request, isSession, isInfo, safeAvatar, ApiError } from "../src/api";
|
||||
import { messages, detectLanguage } from "../src/i18n";
|
||||
|
||||
test("catalogs match and never silently fall back to a missing translation", () => {
|
||||
assert.deepEqual(
|
||||
Object.keys(messages.ru).sort(),
|
||||
Object.keys(messages.en).sort(),
|
||||
);
|
||||
for (const catalog of Object.values(messages))
|
||||
for (const value of Object.values(catalog))
|
||||
assert.ok(typeof value === "string" && value.length > 0);
|
||||
});
|
||||
test("language preference is bounded and follows the OS without storage", () => {
|
||||
assert.equal(detectLanguage("en", ["ru-RU"]), "en");
|
||||
assert.equal(detectLanguage("bad", ["ru-RU"]), "ru");
|
||||
assert.equal(detectLanguage(undefined, ["pl-PL"]), "en");
|
||||
assert.equal(detectLanguage("RU", ["en-US"]), "en");
|
||||
});
|
||||
test("API keeps authentication on the same origin and sends CSRF only in header", async () => {
|
||||
let observed: { path: string; options: RequestInit } | undefined;
|
||||
const result = await request("/v1/auth/logout", {
|
||||
method: "POST",
|
||||
csrf: "fixture-csrf",
|
||||
fetcher: async (path, options) => {
|
||||
observed = { path, options };
|
||||
return new Response(null, { status: 204 });
|
||||
},
|
||||
});
|
||||
assert.equal(result, null);
|
||||
assert.ok(observed);
|
||||
assert.equal(observed.path, "/v1/auth/logout");
|
||||
assert.equal(observed.options.credentials, "same-origin");
|
||||
assert.equal(observed.options.cache, "no-store");
|
||||
assert.equal(
|
||||
new Headers(observed.options.headers).get("X-CSRF-Token"),
|
||||
"fixture-csrf",
|
||||
);
|
||||
assert.equal(observed.options.method, "POST");
|
||||
await assert.rejects(
|
||||
request("https://evil.example/v1/auth/session"),
|
||||
TypeError,
|
||||
);
|
||||
await assert.rejects(request("//evil.example/v1/auth/session"), TypeError);
|
||||
});
|
||||
test("API failures expose a generic code and preserve status for expired sessions", async () => {
|
||||
await assert.rejects(
|
||||
request("/v1/auth/session", {
|
||||
fetcher: async () => new Response(null, { status: 401 }),
|
||||
}),
|
||||
(error: unknown) =>
|
||||
error instanceof ApiError &&
|
||||
error.status === 401 &&
|
||||
error.message === "request_failed",
|
||||
);
|
||||
});
|
||||
test("session validation rejects malformed authenticated payloads", () => {
|
||||
assert.ok(isSession({ authenticated: false }));
|
||||
assert.ok(
|
||||
isSession({
|
||||
authenticated: true,
|
||||
csrf_token: "fixture",
|
||||
user: { user_id: "fixture", handle: "test", display_name: "Test" },
|
||||
}),
|
||||
);
|
||||
assert.equal(isSession({ authenticated: true, user: {} }), false);
|
||||
assert.equal(isSession({ authenticated: "true" }), false);
|
||||
assert.equal(
|
||||
isSession({
|
||||
authenticated: true,
|
||||
csrf_token: "",
|
||||
user: { user_id: "fixture", handle: "test", display_name: "Test" },
|
||||
}),
|
||||
false,
|
||||
);
|
||||
});
|
||||
test("avatars allow only uncredentialed HTTPS GitHub image origin", () => {
|
||||
assert.equal(
|
||||
safeAvatar("https://avatars.githubusercontent.com/u/1?v=4"),
|
||||
"https://avatars.githubusercontent.com/u/1?v=4",
|
||||
);
|
||||
for (const url of [
|
||||
"javascript:alert(1)",
|
||||
"data:image/svg+xml,<svg/>",
|
||||
"http://avatars.githubusercontent.com/u/1",
|
||||
"https://avatars.githubusercontent.com.evil.example/u/1",
|
||||
"https://evil@avatars.githubusercontent.com/u/1",
|
||||
"https://avatars.githubusercontent.com:8443/u/1",
|
||||
"https://evil.example/u/1",
|
||||
null,
|
||||
])
|
||||
assert.equal(safeAvatar(url), null);
|
||||
});
|
||||
|
||||
test("Hub capabilities reject malformed JSON before enabling sign-in", () => {
|
||||
assert.ok(isInfo({ capabilities: { github_auth: "enabled" } }));
|
||||
for (const value of [
|
||||
null,
|
||||
false,
|
||||
{},
|
||||
{ capabilities: null },
|
||||
{ capabilities: { github_auth: true } },
|
||||
]) {
|
||||
assert.equal(isInfo(value), false);
|
||||
}
|
||||
});
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { once } from 'node:events';
|
||||
import http from 'node:http';
|
||||
function fetchRaw(port,path){return new Promise((resolve,reject)=>{http.get({host:'127.0.0.1',port,path},response=>{let body='';response.on('data',chunk=>body+=chunk);response.on('end',()=>resolve({status:response.statusCode,body}));}).on('error',reject);});}
|
||||
test('preview serves only public/source assets and blocks encoded traversal',async t=>{
|
||||
const port=24176;
|
||||
const process=spawn(globalThis.process.execPath,['scripts/serve.mjs'],{env:{...globalThis.process.env,PORT:String(port)},stdio:['ignore','pipe','pipe']});
|
||||
t.after(()=>process.kill('SIGTERM'));
|
||||
await Promise.race([once(process.stdout,'data'),once(process,'exit').then(()=>{throw new Error('preview failed to start');})]);
|
||||
for(const path of ['/','/account','/account/','/src/app.js','/assets/vnox-mark.png']) assert.equal((await fetchRaw(port,path)).status,200,path);
|
||||
for(const path of ['/src/%2e%2e%2fpackage.json','/src/%2e%2e%2f.env','/src/%2e%2e%2fLICENSE']) assert.equal((await fetchRaw(port,path)).status,403,path);
|
||||
assert.equal((await fetchRaw(port,'/package.json')).status,404);
|
||||
assert.equal((await fetchRaw(port,'/v1/auth/session')).status,503);
|
||||
});
|
||||
72
tests/server.test.ts
Normal file
72
tests/server.test.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import { test, expect } from "bun:test";
|
||||
import { spawn } from "node:child_process";
|
||||
import { once } from "node:events";
|
||||
import http from "node:http";
|
||||
|
||||
function fetchRaw(
|
||||
port: number,
|
||||
path: string,
|
||||
): Promise<{ status: number | undefined; body: string }> {
|
||||
return new Promise((resolve, reject) => {
|
||||
http
|
||||
.get({ host: "127.0.0.1", port, path }, (response) => {
|
||||
let body = "";
|
||||
response.on("data", (chunk) => {
|
||||
body += chunk;
|
||||
});
|
||||
response.on("end", () =>
|
||||
resolve({ status: response.statusCode, body }),
|
||||
);
|
||||
})
|
||||
.on("error", reject);
|
||||
});
|
||||
}
|
||||
|
||||
test("Bun preview serves browser JavaScript and blocks private files/encoded traversal", async () => {
|
||||
const child = spawn(process.execPath, ["scripts/serve.ts"], {
|
||||
env: { ...process.env, PORT: "0" },
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
try {
|
||||
const output = await Promise.race([
|
||||
once(child.stdout, "data").then(([value]) => String(value)),
|
||||
once(child, "exit").then(() => {
|
||||
throw new Error("Preview failed to start");
|
||||
}),
|
||||
]);
|
||||
const port = Number(output.match(/127\.0\.0\.1:(\d+)/)?.[1]);
|
||||
expect(port).toBeGreaterThan(0);
|
||||
for (const path of [
|
||||
"/",
|
||||
"/account",
|
||||
"/account/",
|
||||
"/src/app.js",
|
||||
"/src/styles.css",
|
||||
"/assets/vnox-mark.png",
|
||||
]) {
|
||||
expect((await fetchRaw(port, path)).status).toBe(200);
|
||||
}
|
||||
for (const path of [
|
||||
"/src/%2e%2e%2fpackage.json",
|
||||
"/src/%2e%2e%2f.env",
|
||||
"/src/%2e%2e%2fLICENSE",
|
||||
]) {
|
||||
expect((await fetchRaw(port, path)).status).toBe(403);
|
||||
}
|
||||
for (const path of [
|
||||
"/package.json",
|
||||
"/src/app.ts",
|
||||
"/src/api.ts",
|
||||
"/scripts/serve.ts",
|
||||
]) {
|
||||
expect((await fetchRaw(port, path)).status).toBe(404);
|
||||
}
|
||||
expect((await fetchRaw(port, "/v1/auth/session")).status).toBe(503);
|
||||
const javascript = await fetchRaw(port, "/src/app.js");
|
||||
expect(javascript.body).not.toContain("import type");
|
||||
expect(javascript.body).toContain("vnox-web-language");
|
||||
} finally {
|
||||
child.kill("SIGTERM");
|
||||
await once(child, "exit");
|
||||
}
|
||||
}, 15000);
|
||||
Loading…
Add table
Add a link
Reference in a new issue