Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(security): generate row ids with crypto.randomUUID

spacesdrive919e9860db24
diff --git a/backend/lib/ids.ts b/backend/lib/ids.ts
new file mode 100644
index 0000000..1418948
--- /dev/null
+++ b/backend/lib/ids.ts
@@ -0,0 +1,4 @@
+// row ids come from the platform's cryptographic generator, so no one can work out another row's id from their own
+export function newRowId(): string {
+ return crypto.randomUUID();
+}
diff --git a/backend/services/accountFactory.ts b/backend/services/accountFactory.ts
index 22cfdbc..091b04f 100644
--- a/backend/services/accountFactory.ts
+++ b/backend/services/accountFactory.ts
@@ -1,12 +1,9 @@
+import { newRowId } from '../lib/ids.ts';
import type { Account, NewAccount } from '../../shared/types/domain.ts';
import type { YouTubeChannelData } from '../../shared/types/youtube.ts';
import type { RedditProfile } from '../../shared/types/reddit.ts';
import type { InstagramProfile } from '../../shared/types/instagram.ts';
-export function newAccountId(): string {
- return Date.now().toString() + Math.random().toString(36).substring(2, 6);
-}
-
// a token without a known lifetime is stored with no expiry
export function tokenExpiryIso(expiresIn: number): string | null {
return expiresIn > 0
@@ -16,7 +13,7 @@ export function tokenExpiryIso(expiresIn: number): string | null {
export function buildYouTubeAccount(channelData: YouTubeChannelData): NewAccount {
return {
- id: newAccountId(),
+ id: newRowId(),
channelId: channelData.channelId,
title: channelData.title,
description: channelData.description,
@@ -39,7 +36,7 @@ export function buildYouTubeAccount(channelData: YouTubeChannelData): NewAccount
export function buildRedditAccount(profile: RedditProfile, cookie: string): Account {
return {
- id: newAccountId(),
+ id: newRowId(),
platform: 'reddit',
username: profile.username,
title: `u/${profile.username}`,
@@ -56,7 +53,7 @@ export function buildRedditAccount(profile: RedditProfile, cookie: string): Acco
export function buildInstagramAccount(profile: InstagramProfile, accessToken: string, expiresIn: number): Account {
return {
- id: newAccountId(),
+ id: newRowId(),
platform: 'instagram',
igUserId: profile.igUserId,
username: profile.username,
diff --git a/backend/tests/fixtures/rowIds.ts b/backend/tests/fixtures/rowIds.ts
new file mode 100644
index 0000000..179a1df
--- /dev/null
+++ b/backend/tests/fixtures/rowIds.ts
@@ -0,0 +1,9 @@
+import { mock } from 'node:test';
+
+type Uuid = ReturnType<typeof crypto.randomUUID>;
+
+// new rows get ids from crypto.randomUUID(); numbering them keeps responses and golden snapshots stable
+export function mockRowIds(): void {
+ let next = 0;
+ mock.method(crypto, 'randomUUID', (): Uuid => `00000000-0000-4000-8000-${String(++next).padStart(12, '0')}`);
+}
diff --git a/backend/tests/routes/accountRoutes.test.ts b/backend/tests/routes/accountRoutes.test.ts
index 38c0a87..d10e903 100644
--- a/backend/tests/routes/accountRoutes.test.ts
+++ b/backend/tests/routes/accountRoutes.test.ts
@@ -10,6 +10,7 @@ import type { FetchMock, RecordedRequest } from '../fixtures/fakeFetch.ts';
import { fakeSupabase, accountRow } from '../fixtures/fakeSupabase.ts';
import type { SupabaseCall } from '../fixtures/fakeSupabase.ts';
import { mountWithContext, callRoute, fakeRedis } from '../fixtures/routeHarness.ts';
+import { mockRowIds } from '../fixtures/rowIds.ts';
import type { RouteStep } from '../fixtures/routeHarness.ts';
// a byte order mark that pasted secrets sometimes start with
@@ -87,6 +88,7 @@ before(() => mock.timers.enable({ apis: ['Date'], now: FIXED_NOW }));
after(() => mock.timers.reset());
beforeEach(() => {
mock.method(Math, 'random', () => 0.123456789);
+ mockRowIds();
logLines = [];
mock.method(console, 'log', (...args: unknown[]) => logLines.push(['log', ...args]));
mock.method(console, 'error', (...args: unknown[]) => logLines.push(['error', ...args]));
diff --git a/backend/tests/routes/instagramAuthRoutes.test.ts b/backend/tests/routes/instagramAuthRoutes.test.ts
index 2bdee3f..b9e099b 100644
--- a/backend/tests/routes/instagramAuthRoutes.test.ts
+++ b/backend/tests/routes/instagramAuthRoutes.test.ts
@@ -10,6 +10,7 @@ import type { FetchMock, RecordedRequest } from '../fixtures/fakeFetch.ts';
import { fakeSupabase, accountRow } from '../fixtures/fakeSupabase.ts';
import type { FakeSupabase } from '../fixtures/fakeSupabase.ts';
import { mountWithContext, callRoute } from '../fixtures/routeHarness.ts';
+import { mockRowIds } from '../fixtures/rowIds.ts';
// a byte order mark that pasted secrets sometimes start with
const BOM = String.fromCharCode(0xfeff);
@@ -55,6 +56,7 @@ before(() => mock.timers.enable({ apis: ['Date'], now: FIXED_NOW }));
after(() => mock.timers.reset());
beforeEach(() => {
mock.method(Math, 'random', () => 0.987654321);
+ mockRowIds();
logLines = [];
mock.method(console, 'error', (...args: unknown[]) => logLines.push(['error', ...args]));
fetchMock = installFetch(instagramServer);
diff --git a/backend/tests/services/accountFactory.test.ts b/backend/tests/services/accountFactory.test.ts
index 44b987f..8730154 100644
--- a/backend/tests/services/accountFactory.test.ts
+++ b/backend/tests/services/accountFactory.test.ts
@@ -1,25 +1,21 @@
import { test, describe, before, after, beforeEach, afterEach, mock } from 'node:test';
import assert from 'node:assert/strict';
import {
- newAccountId, tokenExpiryIso, buildYouTubeAccount, buildRedditAccount, buildInstagramAccount,
+ tokenExpiryIso, buildYouTubeAccount, buildRedditAccount, buildInstagramAccount,
} from '../../services/accountFactory.ts';
import type { YouTubeChannelData } from '../../../shared/types/youtube.ts';
import type { RedditProfile } from '../../../shared/types/reddit.ts';
const FIXED_NOW = Date.parse('2026-09-01T12:00:00Z');
const NOW_ISO = '2026-09-01T12:00:00.000Z';
-const ACCOUNT_ID = '17882640000004fzz';
+const ACCOUNT_ID = '00000000-0000-4000-8000-00000000a001';
describe('accountFactory', () => {
before(() => mock.timers.enable({ apis: ['Date'], now: FIXED_NOW }));
after(() => mock.timers.reset());
- beforeEach(() => mock.method(Math, 'random', () => 0.123456789));
+ beforeEach(() => mock.method(crypto, 'randomUUID', () => ACCOUNT_ID));
afterEach(() => mock.restoreAll());
- test('newAccountId joins the time and four random base 36 characters', () => {
- assert.equal(newAccountId(), ACCOUNT_ID);
- });
-
test('tokenExpiryIso adds seconds to now, or returns null without a lifetime', () => {
assert.equal(tokenExpiryIso(3600), '2026-09-01T13:00:00.000Z');
assert.equal(tokenExpiryIso(0), null);