feat(auth): harden session management and security
- Implement server-side session revocation on logout - Add IP extraction utility and admin setup error handling - Improve error codes and response consistency for auth endpoints - Refine landing page redirect logic to handle transient DB outages - Update deployment documentation and configuration defaults
This commit is contained in:
1 parent
935f12a1e9
commit
89e4d68690
81 files changed
+11530
-6916
No files matched your search
+127
-2
@@ -1,4 +1,4 @@
|
||||
const CACHE_NAME = 'datastack-cache-v2';
|
||||
const CACHE_NAME = 'datastack-cache-v3';
|
||||
const STATIC_ASSETS = [
|
||||
'/',
|
||||
'/manifest.json',
|
||||
@@ -9,6 +9,92 @@ const STATIC_ASSETS = [
|
||||
'/apple-touch-icon.png',
|
||||
];
|
||||
|
||||
function buildOfflineShellResponse(pathname) {
|
||||
const safePath = String(pathname || '/').replace(/[<>&"']/g, '');
|
||||
const html = `<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>DataStack — Offline</title>
|
||||
<style>
|
||||
:root { color-scheme: dark light; }
|
||||
body {
|
||||
margin: 0;
|
||||
min-height: 100vh;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
padding: 16px;
|
||||
background: #0a0a0b;
|
||||
color: #f4f4f5;
|
||||
font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
|
||||
}
|
||||
.panel {
|
||||
width: 100%;
|
||||
max-width: 360px;
|
||||
border: 1px solid #27272a;
|
||||
background: #111113;
|
||||
padding: 20px;
|
||||
}
|
||||
.brand {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.mark {
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
background: #10b981;
|
||||
display: inline-block;
|
||||
}
|
||||
h1 { font-size: 15px; margin: 0 0 8px; font-weight: 600; }
|
||||
p { font-size: 12.5px; line-height: 1.5; color: #a1a1aa; margin: 0 0 16px; }
|
||||
code { color: #e4e4e7; font-family: ui-monospace, SFMono-Regular, monospace; font-size: 11.5px; }
|
||||
.actions { display: flex; gap: 8px; }
|
||||
button, a {
|
||||
appearance: none;
|
||||
border: 1px solid #27272a;
|
||||
background: #18181b;
|
||||
color: #f4f4f5;
|
||||
padding: 7px 12px;
|
||||
font-size: 12px;
|
||||
text-decoration: none;
|
||||
cursor: pointer;
|
||||
}
|
||||
button.primary {
|
||||
border-color: #10b981;
|
||||
background: #10b981;
|
||||
color: #09090b;
|
||||
font-weight: 600;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main class="panel">
|
||||
<div class="brand"><span class="mark"></span>DataStack</div>
|
||||
<h1>Network connection unavailable</h1>
|
||||
<p>Unable to reach <code>${safePath}</code> while offline. Live ingestion metrics and authenticated admin routes require a connection to the DataStack server.</p>
|
||||
<div class="actions">
|
||||
<button type="button" class="primary" onclick="window.location.reload()">Retry</button>
|
||||
<a href="/">Overview</a>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
return new Response(html, {
|
||||
status: 503,
|
||||
statusText: 'Service Unavailable',
|
||||
headers: {
|
||||
'Content-Type': 'text/html; charset=utf-8',
|
||||
'Cache-Control': 'no-store',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME).then((cache) => {
|
||||
@@ -29,6 +115,28 @@ self.addEventListener('activate', (event) => {
|
||||
self.clients.claim();
|
||||
});
|
||||
|
||||
self.addEventListener('message', (event) => {
|
||||
if (event.data && event.data.type === 'CLEAR_AUTH_CACHE') {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME).then(async (cache) => {
|
||||
const requests = await cache.keys();
|
||||
await Promise.all(
|
||||
requests
|
||||
.filter((req) => {
|
||||
try {
|
||||
const reqUrl = new URL(req.url);
|
||||
return reqUrl.pathname === '/vcon' || reqUrl.pathname.startsWith('/vcon/');
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
.map((req) => cache.delete(req))
|
||||
);
|
||||
}).catch(() => {})
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
const { request } = event;
|
||||
if (request.method !== 'GET') return;
|
||||
@@ -50,10 +158,24 @@ self.addEventListener('fetch', (event) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// Never cache authenticated /vcon/* responses, but provide an offline HTML shell on PWA launch / navigation failure
|
||||
if (url.pathname === '/vcon' || url.pathname.startsWith('/vcon/')) {
|
||||
if (request.mode !== 'navigate') {
|
||||
return;
|
||||
}
|
||||
event.respondWith(
|
||||
fetch(request).catch(() => buildOfflineShellResponse(url.pathname))
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
event.respondWith(
|
||||
fetch(request)
|
||||
.then((response) => {
|
||||
if (response && response.ok && response.type === 'basic') {
|
||||
const cacheControl = response.headers.get('Cache-Control') || '';
|
||||
const shouldSkipCache =
|
||||
cacheControl.includes('no-store') || cacheControl.includes('private');
|
||||
if (response && response.ok && response.type === 'basic' && !shouldSkipCache) {
|
||||
const clone = response.clone();
|
||||
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone)).catch(() => {});
|
||||
}
|
||||
@@ -62,6 +184,9 @@ self.addEventListener('fetch', (event) => {
|
||||
.catch(async () => {
|
||||
const cached = await caches.match(request);
|
||||
if (cached) return cached;
|
||||
if (request.mode === 'navigate') {
|
||||
return buildOfflineShellResponse(url.pathname);
|
||||
}
|
||||
return new Response('Offline — network unavailable.', {
|
||||
status: 503,
|
||||
statusText: 'Service Unavailable',
|
||||
|
||||
Reference in new issue
Block a user