commit
Make read-only dashboard pages public
Keep pending reviews and moderation actions behind the password gate, and preserve the GitHub quota reserve for public lookups. Co-Authored-By: Sandrone <[email protected]>
diff --git a/README.md b/README.md
index 0721f01..e441dc1 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,13 @@ The dashboard combines every repository. Use the **All / is-a.dev / is-a.bot** f
on Overview, Pending and History to narrow that page to one repository. Lookup has its own
repository picker.
+Overview (`/`), History, and Lookup are public. Overview shows moderation totals,
+the 30-day chart, pending finding counts, and scanner health. Pending reviews and
+moderation actions require the existing password login. Public lookups respect the
+GitHub quota reserve held back for moderation. Raw scanner errors are only included for signed-in moderators.
+Signing in opens Pending; signing out returns to the public overview.
+Without `DASHBOARD_PASSWORD`, moderation stays unavailable in production.
+
It detects the violations below, pings you on Discord, and acts **only** when you press a button.
| Violation | Button | What the button does |
diff --git a/package.json b/package.json
index d4c1887..59489eb 100644
--- a/package.json
+++ b/package.json
@@ -18,7 +18,8 @@
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"lint": "prettier --check . && eslint .",
- "format": "prettier --write ."
+ "format": "prettier --write .",
+ "test:access": "npm run build && node --test tests/access.test.mjs && ACCESS_TEST_PASSWORD= node --test tests/access.test.mjs"
},
"dependencies": {
"discord.js": "^14.28.1",
diff --git a/src/app.d.ts b/src/app.d.ts
index da08e6d..3f2166e 100644
--- a/src/app.d.ts
+++ b/src/app.d.ts
@@ -3,7 +3,9 @@
declare global {
namespace App {
// interface Error {}
- // interface Locals {}
+ interface Locals {
+ authenticated: boolean;
+ }
// interface PageData {}
// interface PageState {}
// interface Platform {}
diff --git a/src/hooks.server.ts b/src/hooks.server.ts
index bde195f..c20d7e6 100644
--- a/src/hooks.server.ts
+++ b/src/hooks.server.ts
@@ -17,12 +17,15 @@ export const init: ServerInit = async () => {
};
export const handle: Handle = async ({ event, resolve }) => {
- if (event.url.pathname === '/login') {
+ event.locals.authenticated =
+ isSessionValid(event.cookies.get(sessionCookieName)) || (dev && !isPasswordConfigured());
+
+ if (['/', '/history', '/lookup', '/login'].includes(event.route.id ?? '')) {
return resolve(event);
}
if (!isPasswordConfigured()) {
- // Without a password the dashboard only opens on the local dev server
+ // Without a password, moderation only opens on the local dev server
if (dev) {
return resolve(event);
}
@@ -30,7 +33,7 @@ export const handle: Handle = async ({ event, resolve }) => {
return new Response('Set DASHBOARD_PASSWORD to use the dashboard.', { status: 503 });
}
- if (!isSessionValid(event.cookies.get(sessionCookieName))) {
+ if (!event.locals.authenticated) {
redirect(303, '/login');
}
diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts
index 3f55741..6752758 100644
--- a/src/routes/+layout.server.ts
+++ b/src/routes/+layout.server.ts
@@ -8,7 +8,7 @@ import { isPasswordConfigured } from '#lib/server/auth.ts';
import type { LayoutServerLoad } from './$types';
-export const load: LayoutServerLoad = ({ url }) => {
+export const load: LayoutServerLoad = ({ url, locals }) => {
const onLoginPage = url.pathname === '/login';
const repoFilter = readRepoFilter(url);
@@ -18,6 +18,7 @@ export const load: LayoutServerLoad = ({ url }) => {
repoFilter,
pendingTotal: onLoginPage ? 0 : listOpenFindings().length,
showNavigation: !onLoginPage,
- canSignOut: isPasswordConfigured()
+ authenticated: locals.authenticated,
+ canSignOut: locals.authenticated && isPasswordConfigured()
};
};
diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte
index f48e37e..a2c4250 100644
--- a/src/routes/+layout.svelte
+++ b/src/routes/+layout.svelte
@@ -42,7 +42,7 @@
<nav class="page">
<strong>is-a-tracker</strong>
- {#each links as link (link.href)}
+ {#each links.filter((link) => link.href !== '/pending' || data.authenticated) as link (link.href)}
<a href={link.href} aria-current={page.url.pathname === link.href ? 'page' : undefined}>
{link.label}
{#if link.href === '/pending' && data.pendingTotal > 0}
@@ -61,6 +61,8 @@
<form method="POST" action="/logout">
<button>Sign out</button>
</form>
+ {:else if !data.authenticated}
+ <a href="/login">Moderator sign in</a>
{/if}
</nav>
</header>
diff --git a/src/routes/+page.server.ts b/src/routes/+page.server.ts
index 7487b73..a242d30 100644
--- a/src/routes/+page.server.ts
+++ b/src/routes/+page.server.ts
@@ -8,12 +8,21 @@ import { getScannerStatus } from '#lib/server/moderation/scanner.ts';
import type { PageServerLoad } from './$types';
-export const load: PageServerLoad = ({ url }) => {
+export const load: PageServerLoad = ({ url, locals }) => {
const repoFilter = readRepoFilter(url);
const repos = repoFilter ? [repoFilter] : getConfig().repos;
return {
stats: getModerationStats(30, repoFilter),
- scanners: repos.map(getScannerStatus)
+ scanners: repos.map((repo) => {
+ const scanner = getScannerStatus(repo);
+
+ return {
+ repo: scanner.repo,
+ lastScanAt: scanner.lastScanAt,
+ lastError: locals.authenticated ? scanner.lastError : null,
+ hasError: scanner.lastError !== null
+ };
+ })
};
};
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte
index 0f77c8d..d0b235b 100644
--- a/src/routes/+page.svelte
+++ b/src/routes/+page.svelte
@@ -35,7 +35,13 @@
<title>Overview · is-a-tracker</title>
</svelte:head>
-<h1>Overview</h1>
+<h1>Moderation overview</h1>
+
+<p class="muted">
+ Public statistics for pull request moderation across the tracked repositories. Action totals cover
+ all time; the chart shows the last 30 days. Pending items are individual findings, so one pull
+ request can have several.
+</p>
<RepoFilter repos={data.repos} current={data.repoFilter} />
@@ -58,6 +64,8 @@
{repoLabel(scanner.repo)}: last scan {formatTimestamp(scanner.lastScanAt)}.
{#if scanner.lastError}
Last scan problem: {scanner.lastError}
+ {:else if scanner.hasError}
+ The scanner is experiencing a problem.
{/if}
</p>
{/each}
diff --git a/src/routes/login/+page.server.ts b/src/routes/login/+page.server.ts
index 1e1a156..b3b0a59 100644
--- a/src/routes/login/+page.server.ts
+++ b/src/routes/login/+page.server.ts
@@ -18,9 +18,13 @@ import type { Actions, PageServerLoad } from './$types';
const sessionLengthSeconds = 60 * 60 * 24 * 30;
export const load: PageServerLoad = ({ cookies }) => {
- if (!isPasswordConfigured() || isSessionValid(cookies.get(sessionCookieName))) {
+ if (!isPasswordConfigured()) {
redirect(303, '/');
}
+
+ if (isSessionValid(cookies.get(sessionCookieName))) {
+ redirect(303, '/pending');
+ }
};
export const actions: Actions = {
@@ -43,6 +47,6 @@ export const actions: Actions = {
maxAge: sessionLengthSeconds
});
- redirect(303, '/');
+ redirect(303, '/pending');
}
};
diff --git a/src/routes/login/+page.svelte b/src/routes/login/+page.svelte
index bdb5a58..5b591ba 100644
--- a/src/routes/login/+page.svelte
+++ b/src/routes/login/+page.svelte
@@ -12,7 +12,10 @@
<title>Sign in · is-a-tracker</title>
</svelte:head>
-<h1>is-a-tracker</h1>
+<h1>Moderator sign in</h1>
+
+<p class="muted">Sign in to review pending findings and access moderation tools.</p>
+<p><a href="/">View public statistics</a></p>
{#if form?.problem}
<p class="notice failure" role="alert">{form.problem}</p>
diff --git a/src/routes/logout/+server.ts b/src/routes/logout/+server.ts
index 8ff2abe..75ef461 100644
--- a/src/routes/logout/+server.ts
+++ b/src/routes/logout/+server.ts
@@ -9,5 +9,5 @@ import { sessionCookieName } from '#lib/server/auth.ts';
export const POST: RequestHandler = ({ cookies }) => {
cookies.delete(sessionCookieName, { path: '/' });
- redirect(303, '/login');
+ redirect(303, '/');
};
diff --git a/src/routes/lookup/+page.server.ts b/src/routes/lookup/+page.server.ts
index 7605b08..70d629c 100644
--- a/src/routes/lookup/+page.server.ts
+++ b/src/routes/lookup/+page.server.ts
@@ -34,7 +34,7 @@ function parsePullRequest(
}
// Read-only: fetches and validates live, and stores nothing
-export const load: PageServerLoad = async ({ url }) => {
+export const load: PageServerLoad = async ({ url, locals }) => {
const query = url.searchParams.get('pr') ?? '';
if (query.trim() === '') {
@@ -68,11 +68,11 @@ export const load: PageServerLoad = async ({ url }) => {
}
try {
- const pullRequest = await getPullRequest(repo, number, true);
+ const pullRequest = await getPullRequest(repo, number, locals.authenticated);
const evaluation = await evaluatePullRequest(pullRequest, {
fresh: true,
- priority: true
+ priority: locals.authenticated
});
return {
diff --git a/tests/access.test.mjs b/tests/access.test.mjs
new file mode 100644
index 0000000..977e74c
--- /dev/null
+++ b/tests/access.test.mjs
@@ -0,0 +1,135 @@
+/* tests/access.test.mjs */
+
+import assert from 'node:assert/strict';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { DatabaseSync } from 'node:sqlite';
+import { after, test } from 'node:test';
+
+// Run against the production build without starting the scanner or Discord.
+const directory = mkdtempSync(join(tmpdir(), 'tracker-access-'));
+const password = process.env.ACCESS_TEST_PASSWORD ?? 'access-test-password';
+
+process.env.DATABASE_PATH = join(directory, 'tracker.sqlite');
+process.env.DASHBOARD_PASSWORD = password;
+process.env.GITHUB_REPOS = 'is-a-dev/register,free-domains/is-a.bot';
+
+const database = new DatabaseSync(process.env.DATABASE_PATH);
+const privateError = 'Internal scanner error fixture';
+
+database.exec('CREATE TABLE keyValue (key TEXT PRIMARY KEY, value TEXT NOT NULL)');
+database
+ .prepare('INSERT INTO keyValue VALUES (?, ?)')
+ .run('scanner.lastError:is-a-dev/register', privateError);
+database.close();
+
+after(() => rmSync(directory, { recursive: true }));
+
+const { handle } = await import('../.svelte-kit/output/server/entries/hooks.server.js');
+const { load: overview } =
+ await import('../.svelte-kit/output/server/entries/pages/_page.server.ts.js');
+const { actions: login } =
+ await import('../.svelte-kit/output/server/entries/pages/login/_page.server.ts.js');
+
+function eventFor(route, path = route, method = 'GET', token) {
+ const url = new URL(path, 'https://tracker.example');
+
+ return {
+ url,
+ request: new Request(url, { method }),
+ route: { id: route },
+ locals: {},
+ cookies: { get: () => token }
+ };
+}
+
+function resolve() {
+ return new Response('resolved');
+}
+
+for (const route of ['/', '/history', '/lookup', '/login']) {
+ test(`${route} is public, including page-data requests`, async () => {
+ for (const path of [route, `${route === '/' ? '' : route}/__data.json`]) {
+ const event = eventFor(route, path);
+ const response = await handle({ event, resolve });
+
+ assert.equal(response.status, 200);
+ assert.equal(event.locals.authenticated, false);
+ }
+ });
+}
+
+test('Pending GET, page data, and direct action POSTs require login', async () => {
+ for (const [path, method] of [
+ ['/pending', 'GET'],
+ ['/pending/__data.json', 'GET'],
+ ['/pending?/approve', 'POST'],
+ ['/pending?/ignore', 'POST']
+ ]) {
+ const event = eventFor('/pending', path, method, 'invalid-session');
+
+ if (password) {
+ await assert.rejects(handle({ event, resolve }), (error) => {
+ assert.equal(error.status, 303);
+ assert.equal(error.location, '/login');
+ return true;
+ });
+ } else {
+ const response = await handle({ event, resolve });
+
+ assert.equal(response.status, 503);
+ }
+ }
+});
+
+test('public overview data excludes raw scanner errors', () => {
+ const data = overview(eventFor('/'));
+
+ assert.equal(data.scanners[0].hasError, true);
+ assert.equal(data.scanners[0].lastError, null);
+ assert.equal(JSON.stringify(data).includes(privateError), false);
+});
+
+test(
+ 'login grants Pending access and logout returns to the public page',
+ {
+ skip: !password
+ },
+ async () => {
+ let token;
+ const event = eventFor('/login', '/login', 'POST');
+
+ event.request = new Request(event.url, {
+ method: 'POST',
+ body: new URLSearchParams({ password })
+ });
+ event.cookies.set = (name, value) => {
+ assert.equal(name, 'trackerSession');
+ token = value;
+ };
+
+ await assert.rejects(login.default(event), (error) => error.location === '/pending');
+ assert.ok(token);
+
+ const pending = eventFor('/pending', '/pending', 'POST', token);
+ const response = await handle({ event: pending, resolve });
+
+ assert.equal(response.status, 200);
+ assert.equal(pending.locals.authenticated, true);
+ assert.equal(overview(pending).scanners[0].lastError, privateError);
+
+ const { POST } =
+ await import('../.svelte-kit/output/server/entries/endpoints/logout/_server.ts.js');
+
+ assert.throws(
+ () =>
+ POST({
+ cookies: {
+ delete: (name) => assert.equal(name, 'trackerSession')
+ }
+ }),
+ (error) => error.location === '/'
+ );
+ }
+);