fix(editor): salt the session record's fingerprint with its tag
This commit is contained in:
1 parent
2ad720b34e
commit
7ecaf1998e
3 files changed
+64
-26
No files matched your search
@@ -0,0 +1,26 @@
|
|||||||
|
const FNV_OFFSET = 0xcbf29ce484222325n
|
||||||
|
const FNV_PRIME = 0x100000001b3n
|
||||||
|
const MASK_64 = 0xffffffffffffffffn
|
||||||
|
|
||||||
|
/** A fingerprint as `fingerprintOf` writes it. */
|
||||||
|
export const FINGERPRINT_PATTERN = /^[0-9a-f]{16}$/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FNV-1a 64 of `text`, hex. An equality check binding a record to one account, not a
|
||||||
|
* secret: it keeps the raw account id out of storage, and needs no secure context
|
||||||
|
* (`crypto.subtle` is absent on an http origin such as the LAN trial). Callers salt the
|
||||||
|
* text with a random value, so one account leaves no common value across records.
|
||||||
|
*/
|
||||||
|
export function fingerprintOf(text: string): string {
|
||||||
|
let hash = FNV_OFFSET
|
||||||
|
for (const byte of new TextEncoder().encode(text)) {
|
||||||
|
hash = ((hash ^ BigInt(byte)) * FNV_PRIME) & MASK_64
|
||||||
|
}
|
||||||
|
return hash.toString(16).padStart(16, '0')
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A random hex value of `bytes` bytes (tags, salts). */
|
||||||
|
export function randomHex(bytes: number): string {
|
||||||
|
const values = crypto.getRandomValues(new Uint8Array(bytes))
|
||||||
|
return Array.from(values, (b) => b.toString(16).padStart(2, '0')).join('')
|
||||||
|
}
|
||||||
@@ -2,22 +2,22 @@ import { useMemo } from 'react'
|
|||||||
import { create } from 'zustand'
|
import { create } from 'zustand'
|
||||||
import { useAuthStore } from '@/state/authStore'
|
import { useAuthStore } from '@/state/authStore'
|
||||||
import { clearLeftDrafts, sweepAccountFlags } from '../leftDraft'
|
import { clearLeftDrafts, sweepAccountFlags } from '../leftDraft'
|
||||||
|
import { FINGERPRINT_PATTERN, fingerprintOf, randomHex } from './fingerprint'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The session tag (criterion 22, owner 2026-11-14): a random 128-bit label the history
|
* The session tag (criterion 22, owner 2026-11-14): a random 128-bit label the history
|
||||||
* entries and the left-draft flag carry instead of the account id. It is kept in this
|
* entries and the left-draft flag carry instead of the account id. It is kept in this
|
||||||
* tab's sessionStorage with a fingerprint of the account id, reused only for the account
|
* tab's sessionStorage with a fingerprint of the account id salted with the tag, reused
|
||||||
* whose fingerprint it holds, and rotated on sign-out and on any other account (the flags
|
* only for the account whose fingerprint it holds, and rotated on sign-out and on any
|
||||||
* of the old tag cleared). Resolved at once, on any origin (http included). A storage
|
* other account (the flags of the old tag cleared). Resolved at once, on any origin (http included). A storage
|
||||||
* that refuses keeps it in memory only: a reload then restores nothing.
|
* that refuses keeps it in memory only: a reload then restores nothing.
|
||||||
*/
|
*/
|
||||||
const STORAGE_KEY = 'faunosteo:editor:session-tag'
|
const STORAGE_KEY = 'faunosteo:editor:session-tag'
|
||||||
const TAG_PATTERN = /^[0-9a-f]{32}$/
|
const TAG_PATTERN = /^[0-9a-f]{32}$/
|
||||||
const FINGERPRINT_PATTERN = /^[0-9a-f]{16}$/
|
|
||||||
|
|
||||||
interface TagRecord {
|
interface TagRecord {
|
||||||
tag: string
|
tag: string
|
||||||
/** The fingerprint of the account id (`fingerprintOf`). */
|
/** The fingerprint of the account id salted with `tag` (`accountOf`). */
|
||||||
account: string
|
account: string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,27 +31,13 @@ export const useSessionTagStore = create<{ held: HeldTag | null }>(() => ({
|
|||||||
held: null,
|
held: null,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
const hex = (bytes: Uint8Array) =>
|
const randomTag = () => randomHex(16)
|
||||||
Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('')
|
|
||||||
|
|
||||||
const randomTag = () => hex(crypto.getRandomValues(new Uint8Array(16)))
|
|
||||||
|
|
||||||
const FNV_OFFSET = 0xcbf29ce484222325n
|
|
||||||
const FNV_PRIME = 0x100000001b3n
|
|
||||||
const MASK_64 = 0xffffffffffffffffn
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FNV-1a 64 of the account id, hex. An equality check binding the tag to one account,
|
* The record's fingerprint of `user`, salted with the record's own tag: two records of
|
||||||
* not a secret: it keeps the raw id out of storage, and needs no secure context
|
* one account share no value. A record of the older, unsalted shape reads as none.
|
||||||
* (`crypto.subtle` is absent on an http origin such as the LAN trial).
|
|
||||||
*/
|
*/
|
||||||
function fingerprintOf(user: string): string {
|
const accountOf = (tag: string, user: string) => fingerprintOf(`${tag}:${user}`)
|
||||||
let hash = FNV_OFFSET
|
|
||||||
for (const byte of new TextEncoder().encode(user)) {
|
|
||||||
hash = ((hash ^ BigInt(byte)) * FNV_PRIME) & MASK_64
|
|
||||||
}
|
|
||||||
return hash.toString(16).padStart(16, '0')
|
|
||||||
}
|
|
||||||
|
|
||||||
function isTagRecord(raw: unknown): raw is TagRecord {
|
function isTagRecord(raw: unknown): raw is TagRecord {
|
||||||
if (typeof raw !== 'object' || raw === null) return false
|
if (typeof raw !== 'object' || raw === null) return false
|
||||||
@@ -97,11 +83,13 @@ function hold(next: HeldTag): string {
|
|||||||
export function resolveSessionTag(user: string): string {
|
export function resolveSessionTag(user: string): string {
|
||||||
const held = useSessionTagStore.getState().held
|
const held = useSessionTagStore.getState().held
|
||||||
if (held?.user === user) return held.tag
|
if (held?.user === user) return held.tag
|
||||||
const account = fingerprintOf(user)
|
|
||||||
const kept = readStored()
|
const kept = readStored()
|
||||||
if (kept && kept.account === account) return hold({ ...kept, user })
|
if (kept && kept.account === accountOf(kept.tag, user)) {
|
||||||
|
return hold({ ...kept, user })
|
||||||
|
}
|
||||||
retire(kept, held)
|
retire(kept, held)
|
||||||
return hold({ tag: randomTag(), account, user })
|
const tag = randomTag()
|
||||||
|
return hold({ tag, account: accountOf(tag, user), user })
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sign-out: the tag goes, with its flags; the next sign-in draws a new one. */
|
/** Sign-out: the tag goes, with its flags; the next sign-in draws a new one. */
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
startSessionTagSync,
|
startSessionTagSync,
|
||||||
useSessionTagStore,
|
useSessionTagStore,
|
||||||
} from '@/editor/history/sessionTag'
|
} from '@/editor/history/sessionTag'
|
||||||
|
import { fingerprintOf } from '@/editor/history/fingerprint'
|
||||||
import { leftDraftKey } from '@/editor/leftDraft'
|
import { leftDraftKey } from '@/editor/leftDraft'
|
||||||
import { useAuthStore } from '@/state/authStore'
|
import { useAuthStore } from '@/state/authStore'
|
||||||
|
|
||||||
@@ -100,6 +101,29 @@ describe('resolveSessionTag', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('the fingerprint of the record (security LOW, rev 4)', () => {
|
||||||
|
it('is salted with its own tag: two tags of one account differ', () => {
|
||||||
|
resolveSessionTag('account-a')
|
||||||
|
const first = stored()!
|
||||||
|
forgetSessionTag()
|
||||||
|
reload()
|
||||||
|
resolveSessionTag('account-a')
|
||||||
|
const second = stored()!
|
||||||
|
expect(second.tag).not.toBe(first.tag)
|
||||||
|
expect(second.account).not.toBe(first.account)
|
||||||
|
expect(first.account).toBe(fingerprintOf(`${first.tag}:account-a`))
|
||||||
|
})
|
||||||
|
|
||||||
|
it('reads an unsalted record of the older shape as none, and rotates', () => {
|
||||||
|
const tag = 'a'.repeat(32)
|
||||||
|
const account = fingerprintOf('account-a')
|
||||||
|
sessionStorage.setItem(STORAGE_KEY, JSON.stringify({ tag, account }))
|
||||||
|
expect(resolveSessionTag('account-a')).not.toBe(tag)
|
||||||
|
reload()
|
||||||
|
expect(resolveSessionTag('account-a')).toBe(stored()!.tag)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe('the sweep of flags keyed by an account id (older shape)', () => {
|
describe('the sweep of flags keyed by an account id (older shape)', () => {
|
||||||
it('removes them on start and keeps the flags of a tag', () => {
|
it('removes them on start and keeps the flags of a tag', () => {
|
||||||
const older = 'faunosteo:editor:left-draft:S:account-a'
|
const older = 'faunosteo:editor:left-draft:S:account-a'
|
||||||
|
|||||||
Reference in new issue
Block a user