@@ -0,0 +1,76 @@
|
||||
// Native Fortify, CSRF and Inertia protocol for owned loopback QA only.
|
||||
export function createNativeInertiaClient(origin, check) {
|
||||
const base = new URL(origin);
|
||||
if (base.protocol !== "http:" || !["localhost", "127.0.0.1"].includes(base.hostname) || !base.port) throw Error("Owned loopback client required");
|
||||
const cookies = new Map();
|
||||
let version = '';
|
||||
const request = async (route, options = {}) => {
|
||||
if (!route.startsWith('/') || new URL(route, base).origin !== base.origin) throw Error('External HTTP destination refused');
|
||||
const response = await fetch(new URL(route, base), { ...options, redirect: 'manual', headers: {
|
||||
Accept: 'application/json', Cookie: [...cookies].map(([k, v]) => `${k}=${v}`).join('; '),
|
||||
...(options.headers?.['X-Inertia'] === 'true' ? { 'X-Inertia-Version': version } : {}), ...options.headers,
|
||||
} });
|
||||
for (const header of response.headers.getSetCookie()) {
|
||||
const pair = header.split(';', 1)[0];
|
||||
const index = pair.indexOf('=');
|
||||
if (index > 0) cookies.set(pair.slice(0, index), pair.slice(index + 1));
|
||||
}
|
||||
// A checker may inspect only status/headers. Drain the network body before
|
||||
// a closed PHP socket can leave Undici paused, retaining a readable clone.
|
||||
const buffered = response.clone();
|
||||
await response.arrayBuffer();
|
||||
return buffered;
|
||||
};
|
||||
const csrf = () => ({ 'X-XSRF-TOKEN': decodeURIComponent(cookies.get('XSRF-TOKEN') ?? '') });
|
||||
const login = async email => {
|
||||
const initial = await request('/login', { headers: { Accept: 'text/html' } });
|
||||
check(initial.status === 200, 'native login page');
|
||||
const initialPage = (await initial.text()).match(/<script data-page="app" type="application\/json">([\s\S]*?)<\/script>/);
|
||||
check(initialPage !== null, 'native initial page protocol');
|
||||
version = JSON.parse(initialPage[1]).version;
|
||||
check(typeof version === 'string' && version.length > 0, 'native asset version');
|
||||
const payload = JSON.stringify({ email, password: 'password', remember: false });
|
||||
const invalid = await request('/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: payload });
|
||||
check(invalid.status === 419, 'missing CSRF refused before login');
|
||||
const response = await request('/login', { method: 'POST', headers: { 'Content-Type': 'application/json', ...csrf() }, body: payload });
|
||||
check([200, 302].includes(response.status), 'native Fortify credentials accepted');
|
||||
const stale = await request('/dashboard', { headers: { 'X-Inertia': 'true', 'X-Inertia-Version': 'deliberately-stale-synthetic-version' } });
|
||||
check(stale.status === 409 && stale.headers.has('X-Inertia-Location'), 'stale asset protocol refused without changing access rules');
|
||||
const dashboard = await request('/dashboard', { headers: { 'X-Inertia': 'true' } });
|
||||
check(dashboard.status === 200, `authenticated dashboard status ${dashboard.status}`);
|
||||
const state = await dashboard.json();
|
||||
check(state.props.auth?.user?.email === email, 'session retains the exact native actor');
|
||||
};
|
||||
const page = async (route, component) => {
|
||||
const response = await request(route, { headers: { 'X-Inertia': 'true' } });
|
||||
let error = '';
|
||||
if (response.status !== 200) {
|
||||
try { error = String((await response.clone().json()).message ?? '').slice(0, 180); } catch { /* status remains diagnostic */ }
|
||||
}
|
||||
check(response.status === 200, `${component} native route status ${response.status} ${error}`);
|
||||
check(response.headers.get('X-Inertia') === 'true', `${component} native Inertia response`);
|
||||
const page = await response.json();
|
||||
check(page.component === component, `${component} component`);
|
||||
check(page.encryptHistory === true, `${component} encrypted history`);
|
||||
if (component.startsWith('purchase-controls/')) check(response.headers.get('Cache-Control')?.includes('private') && response.headers.get('Cache-Control')?.includes('no-store'), 'purchase controls private response');
|
||||
return page;
|
||||
};
|
||||
const logout = async () => {
|
||||
check([200, 204, 302].includes((await request('/logout', { method: 'POST', headers: csrf() })).status), 'native logout');
|
||||
check((await request('/auditoria/eventos')).status === 401, 'closed session cannot read metadata');
|
||||
};
|
||||
const post = async (route, payload, expected = 302) => {
|
||||
const response = await request(route, { method: 'POST', headers: { 'Content-Type': 'application/json', ...csrf() }, body: JSON.stringify(payload) });
|
||||
const allowed = Array.isArray(expected) ? expected : [expected];
|
||||
let detail = '';
|
||||
if (!allowed.includes(response.status)) {
|
||||
try {
|
||||
const error = await response.clone().json();
|
||||
detail = String(error.message ?? '').slice(0, 180) + ' fields=' + Object.keys(error.errors ?? {}).join(',');
|
||||
} catch { /* status is still diagnostic */ }
|
||||
}
|
||||
check(allowed.includes(response.status), `native command ${route} status ${response.status} ${detail}`);
|
||||
return response;
|
||||
};
|
||||
return { request, login, page, logout, post };
|
||||
}
|
||||
Reference in New Issue
Block a user