Fixes the defect the operator spotted in #370 immediately after it shipped: auth.ClientIP ignored X-Forwarded-For whenever RemoteAddr was public, so a proxy on a public address — a separate host, or a CDN, i.e. anyone running this publicly, since public means TLS means a proxy — recorded the PROXY for every session. created_ip and last_ip were then always equal and the "Address changed" signal could never fire. The feature looked like it worked and reported nothing. Replaced with the standard trusted-hop model (Rails, Caddy, Traefik, nginx). XFF grows left-to-right as each proxy appends the peer it received from, so for client -> CDN -> own-proxy -> app the app sees [client, CDN] with RemoteAddr = own-proxy, and the client sits at XFF[len - hops]: 0 RemoteAddr, XFF ignored — no proxy 1 the address your own proxy observed 2 through a CDN in front of your proxy Default 1, per the operator: publicly reachable means a TLS terminator in front. The cost is real and stated rather than hidden. hops >= 1 DECLARES that a proxy exists; set it with no proxy, or deeper than the actual chain, and the index reaches attacker-supplied entries, letting a visitor choose which address their own session shows — defeating exactly the detection #370 is for. That's inherent to the model, which is why 0 is a first-class value and the admin card says "count your proxies, don't guess high" instead of just exposing a number. Both mis-set shapes are pinned by tests so they stay known consequences rather than surprises. Migration 0053 + internal/netsettings, cached under an RWMutex. That's not an optimisation: ClientIP runs in RequireUser for every authenticated request, so a per-request query would put the database on the critical path of the whole API. New() always returns a usable service so a boot-time DB hiccup degrades to the default instead of breaking that path (rule #131), and Hops() is nil-safe because test routers construct middleware without it. RequireUser now takes a func() int rather than an int — the value is operator-editable at runtime while the middleware is built once at boot, and reading it per request is what makes a save take effect with no restart (rule #25). The admin card is verifiable, not just configurable: it reports the address the CURRENT setting resolves THIS request to, the raw forwarded chain, and the socket peer — so you set the number, save, and confirm the address matches the machine you're on. It also counts the arriving chain and says how many proxies that implies. GET/PUT both return that payload, PUT recomputed under the new value, so the effect is visible without a reload. Also fixes styling in the #370 card that CI could not catch: text-destructive and bg-destructive don't exist in this Tailwind config — the palette is colors.action.destructive — so the "Address changed" warning and the sign-out-others button were rendering unstyled. Both now use text-action-destructive / bg-action-destructive / text-action-fg. Not done here: requestlog.go still logs raw RemoteAddr and will disagree with the sessions UI about who connected. Left for its own change.
228 lines
7.8 KiB
Svelte
228 lines
7.8 KiB
Svelte
<script lang="ts">
|
|
import { onMount } from 'svelte';
|
|
import { MonitorSmartphone, TriangleAlert } from 'lucide-svelte';
|
|
import {
|
|
listSessions,
|
|
revokeSession,
|
|
revokeOtherSessions,
|
|
type ActiveSession
|
|
} from '$lib/api/me';
|
|
import { errCode } from '$lib/api/errors';
|
|
import { pushToast } from '$lib/stores/toast.svelte';
|
|
|
|
// Self-contained: nothing else in the app reads this data, so it holds its
|
|
// own state and reloads explicitly rather than joining the query cache.
|
|
|
|
let sessions = $state<ActiveSession[] | null>(null);
|
|
let loadError = $state(false);
|
|
let busy = $state(false);
|
|
let confirmingLogoutOthers = $state(false);
|
|
|
|
const others = $derived((sessions ?? []).filter((s) => !s.current).length);
|
|
|
|
async function load() {
|
|
try {
|
|
sessions = await listSessions();
|
|
loadError = false;
|
|
} catch {
|
|
loadError = true;
|
|
}
|
|
}
|
|
|
|
onMount(load);
|
|
|
|
async function onRevoke(s: ActiveSession) {
|
|
busy = true;
|
|
try {
|
|
await revokeSession(s.id);
|
|
pushToast('Signed that device out.');
|
|
await load();
|
|
} catch (e: unknown) {
|
|
// Already gone — revoked from another device, or expired. Reloading
|
|
// shows the truth, so it isn't worth an error. The code is
|
|
// `session_not_found`: apierror.NotFound("session") prefixes it.
|
|
if (errCode(e) === 'session_not_found') {
|
|
await load();
|
|
} else {
|
|
pushToast("Couldn't sign that device out.", 'error');
|
|
}
|
|
} finally {
|
|
busy = false;
|
|
}
|
|
}
|
|
|
|
async function onLogoutOthers() {
|
|
busy = true;
|
|
try {
|
|
const n = await revokeOtherSessions();
|
|
pushToast(n === 1 ? 'Signed out 1 other device.' : `Signed out ${n} other devices.`);
|
|
confirmingLogoutOthers = false;
|
|
await load();
|
|
} catch {
|
|
pushToast("Couldn't sign the other devices out.", 'error');
|
|
} finally {
|
|
busy = false;
|
|
}
|
|
}
|
|
|
|
// Deliberately coarse. A full UA parser would be a dependency and a
|
|
// maintenance burden for a string whose only job is "do you recognise
|
|
// this?" — the IP columns carry the actual signal.
|
|
function describeAgent(ua: string): string {
|
|
if (!ua) return 'Unknown device';
|
|
if (/Minstrel/i.test(ua)) return 'Minstrel for Android';
|
|
if (/Android/i.test(ua)) return 'Android browser';
|
|
if (/iPhone|iPad|iOS/i.test(ua)) return 'iOS browser';
|
|
const browser = /Edg\//.test(ua)
|
|
? 'Edge'
|
|
: /Firefox\//.test(ua)
|
|
? 'Firefox'
|
|
: /Chrome\//.test(ua)
|
|
? 'Chrome'
|
|
: /Safari\//.test(ua)
|
|
? 'Safari'
|
|
: '';
|
|
const os = /Windows/.test(ua)
|
|
? 'Windows'
|
|
: /Mac OS X/.test(ua)
|
|
? 'macOS'
|
|
: /Linux/.test(ua)
|
|
? 'Linux'
|
|
: '';
|
|
if (browser && os) return `${browser} on ${os}`;
|
|
if (browser) return browser;
|
|
return ua.length > 40 ? `${ua.slice(0, 40)}…` : ua;
|
|
}
|
|
|
|
function when(iso: string): string {
|
|
const then = new Date(iso).getTime();
|
|
if (Number.isNaN(then)) return 'unknown';
|
|
const mins = Math.round((Date.now() - then) / 60000);
|
|
if (mins < 1) return 'just now';
|
|
if (mins < 60) return `${mins} min ago`;
|
|
const hours = Math.round(mins / 60);
|
|
if (hours < 24) return hours === 1 ? '1 hour ago' : `${hours} hours ago`;
|
|
const days = Math.round(hours / 24);
|
|
if (days < 30) return days === 1 ? 'yesterday' : `${days} days ago`;
|
|
return new Date(iso).toLocaleDateString();
|
|
}
|
|
|
|
// The reason IP is stored at all. Rather than making someone eyeball two
|
|
// addresses per row, say plainly when a session is being used from
|
|
// somewhere other than where it was created.
|
|
function hasMoved(s: ActiveSession): boolean {
|
|
return !!s.created_ip && !!s.last_ip && s.created_ip !== s.last_ip;
|
|
}
|
|
|
|
function addr(ip: string): string {
|
|
return ip || 'unknown';
|
|
}
|
|
</script>
|
|
|
|
<section class="space-y-3 rounded border border-border bg-surface p-4">
|
|
<h2 class="text-lg font-semibold">Active sessions</h2>
|
|
<p class="text-sm text-text-secondary">
|
|
Every device signed in to your account. If you see one you don't recognise — especially
|
|
one marked as having moved — sign it out and change your password.
|
|
</p>
|
|
|
|
{#if loadError}
|
|
<p class="text-sm text-action-destructive">
|
|
Couldn't load your sessions.
|
|
<button type="button" class="underline hover:no-underline" onclick={load}>Try again</button>
|
|
</p>
|
|
{:else if sessions === null}
|
|
<p class="text-sm text-text-secondary">Loading…</p>
|
|
{:else if sessions.length === 0}
|
|
<!-- Practically unreachable: listing requires an authenticated request,
|
|
which means at least one session exists. Handled rather than assumed. -->
|
|
<p class="text-sm text-text-secondary">No active sessions.</p>
|
|
{:else}
|
|
<ul class="divide-y divide-border">
|
|
{#each sessions as s (s.id)}
|
|
<li class="flex items-start gap-3 py-3">
|
|
<MonitorSmartphone
|
|
size={18}
|
|
class="mt-0.5 flex-shrink-0 text-text-secondary"
|
|
aria-hidden="true"
|
|
/>
|
|
<div class="min-w-0 flex-1">
|
|
<div class="flex flex-wrap items-center gap-2">
|
|
<span class="font-medium text-text-primary">{describeAgent(s.user_agent)}</span>
|
|
{#if s.current}
|
|
<span class="rounded bg-surface-hover px-1.5 py-0.5 text-xs text-text-secondary">
|
|
This device
|
|
</span>
|
|
{/if}
|
|
{#if hasMoved(s)}
|
|
<span
|
|
class="inline-flex items-center gap-1 rounded px-1.5 py-0.5 text-xs text-action-destructive"
|
|
>
|
|
<TriangleAlert size={12} aria-hidden="true" />
|
|
Address changed
|
|
</span>
|
|
{/if}
|
|
</div>
|
|
<div class="text-xs text-text-secondary">
|
|
Last seen {when(s.last_seen_at)} from <span class="font-mono">{addr(s.last_ip)}</span>
|
|
</div>
|
|
<div class="text-xs text-text-secondary">
|
|
Signed in {when(s.created_at)} from <span class="font-mono">{addr(s.created_ip)}</span>
|
|
</div>
|
|
</div>
|
|
{#if !s.current}
|
|
<button
|
|
type="button"
|
|
class="flex-shrink-0 rounded border border-border px-2 py-1 text-sm
|
|
hover:bg-surface-hover focus-visible:ring-2 focus-visible:ring-accent
|
|
disabled:opacity-50"
|
|
disabled={busy}
|
|
onclick={() => onRevoke(s)}
|
|
>
|
|
Sign out
|
|
</button>
|
|
{/if}
|
|
</li>
|
|
{/each}
|
|
</ul>
|
|
|
|
{#if others > 0}
|
|
{#if confirmingLogoutOthers}
|
|
<div class="flex flex-wrap items-center gap-2">
|
|
<span class="text-sm text-text-secondary">
|
|
Sign out {others === 1 ? '1 other device' : `${others} other devices`}? You'll stay
|
|
signed in here.
|
|
</span>
|
|
<button
|
|
type="button"
|
|
class="rounded bg-action-destructive px-3 py-1 text-sm text-action-fg
|
|
focus-visible:ring-2 focus-visible:ring-accent disabled:opacity-50"
|
|
disabled={busy}
|
|
onclick={onLogoutOthers}
|
|
>
|
|
Sign them out
|
|
</button>
|
|
<button
|
|
type="button"
|
|
class="rounded border border-border px-3 py-1 text-sm hover:bg-surface-hover
|
|
focus-visible:ring-2 focus-visible:ring-accent"
|
|
onclick={() => (confirmingLogoutOthers = false)}
|
|
>
|
|
Cancel
|
|
</button>
|
|
</div>
|
|
{:else}
|
|
<button
|
|
type="button"
|
|
class="rounded border border-border px-3 py-1 text-sm hover:bg-surface-hover
|
|
focus-visible:ring-2 focus-visible:ring-accent disabled:opacity-50"
|
|
disabled={busy}
|
|
onclick={() => (confirmingLogoutOthers = true)}
|
|
>
|
|
Sign out all other devices
|
|
</button>
|
|
{/if}
|
|
{/if}
|
|
{/if}
|
|
</section>
|