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): stop the comparison endpoint returning account secrets

spacesdriveffd9ac00a0b2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b2a8e79..9c2a1a6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -54,6 +54,8 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed
+- `GET /api/comparison` returned stored account secrets (Instagram tokens, Reddit cookies) and ranked Instagram and Reddit accounts with YouTube channels; it now ranks YouTube channels only, passed through `safeAccount()`
+
- The frontend test for the Google sign-in nonce failed about one run in 23, because it required at least 40 characters while the nonce length varies around 36 to 43
- The Worker cron and the GitHub Actions workflow could both publish the same scheduled post; posts are now claimed atomically
- Channels with no public videos failed to load with "playlist cannot be found"; they now open with a "No public videos yet" message
diff --git a/backend/routes/analytics.ts b/backend/routes/analytics.ts
index d503b15..0d0b1b7 100644
--- a/backend/routes/analytics.ts
+++ b/backend/routes/analytics.ts
@@ -1,4 +1,5 @@
import { Hono } from 'hono';
+import { safeAccount } from '../lib/safeAccount.ts';
import { getAccounts, getAccountById, updateAccount } from '../lib/db.ts';
import { getVideosCache, setVideosCache, getIGCache, setIGCache } from '../lib/cache.ts';
import { fetchAllVideos, computeVideoAnalytics } from '../services/youtube.ts';
@@ -104,8 +105,10 @@ app.get('/accounts/:id/videos', async (c) => {
app.get('/comparison', async (c) => {
try {
const accounts = await getAccounts(c.get('supabase'), c.get('userId'));
- if (accounts.length === 0) return c.json({ success: true, comparison: null } satisfies YouTubeComparisonResponse);
- return c.json({ success: true, comparison: accountComparison(accounts) } satisfies YouTubeComparisonResponse);
+ // only channels have the counts being ranked, and stored secrets never leave the Worker
+ const channels = accounts.filter(account => account.platform === 'youtube').map(safeAccount);
+ if (channels.length === 0) return c.json({ success: true, comparison: null } satisfies YouTubeComparisonResponse);
+ return c.json({ success: true, comparison: accountComparison(channels) } satisfies YouTubeComparisonResponse);
} catch (err) {
return c.json({ success: false, message: (err as Error).message }, 500);
}
diff --git a/backend/tests/fixtures/analyticsRoutes.youtube.golden.json b/backend/tests/fixtures/analyticsRoutes.youtube.golden.json
index 2dbd219..bf61cb0 100644
--- a/backend/tests/fixtures/analyticsRoutes.youtube.golden.json
+++ b/backend/tests/fixtures/analyticsRoutes.youtube.golden.json
@@ -4,7 +4,7 @@
{
"call": "GET /comparison",
"status": 200,
- "body": "{\"success\":true,\"comparison\":{\"rankings\":{\"bySubscribers\":[{\"id\":\"yt2\",\"platform\":\"youtube\",\"channelId\":\"UC2\",\"uploadsPlaylistId\":\"UU2\",\"title\":\"Beta\",\"subscriberCount\":300,\"viewCount\":1000,\"videoCount\":0},{\"id\":\"yt1\",\"platform\":\"youtube\",\"channelId\":\"UC1\",\"uploadsPlaylistId\":\"UU1\",\"title\":\"Alpha\",\"subscriberCount\":100,\"viewCount\":5000,\"videoCount\":10,\"publishedAt\":\"2024-09-01T00:00:00Z\",\"thumbnails\":{\"default\":\"a.jpg\"}},{\"id\":\"yt3\",\"platform\":\"youtube\",\"channelId\":\"UC3\",\"title\":\"Gamma\"},{\"id\":\"ig1\",\"platform\":\"instagram\",\"igUserId\":\"IG1\",\"accessToken\":\"T1\",\"username\":\"ribbon\"},{\"id\":\"rd1\",\"platform\":\"reddit\",\"username\":\"ribbon\",\"cookie\":\"COOKIE\"},{\"id\":\"rd2\",\"platform\":\"reddit\",\"username\":\"nocookie\"}],\"byViews\":[{\"id\":\"yt1\",\"platform\":\"youtube\",\"channelId\":\"UC1\",\"uploadsPlaylistId\":\"UU1\",\"title\":\"Alpha\",\"subscriberCount\":100,\"viewCount\":5000,\"videoCount\":10,\"publishedAt\":\"2024-09-01T00:00:00Z\",\"thumbnails\":{\"default\":\"a.jpg\"}},{\"id\":\"yt2\",\"platform\":\"youtube\",\"channelId\":\"UC2\",\"uploadsPlaylistId\":\"UU2\",\"title\":\"Beta\",\"subscriberCount\":300,\"viewCount\":1000,\"videoCount\":0},{\"id\":\"yt3\",\"platform\":\"youtube\",\"channelId\":\"UC3\",\"title\":\"Gamma\"},{\"id\":\"ig1\",\"platform\":\"instagram\",\"igUserId\":\"IG1\",\"accessToken\":\"T1\",\"username\":\"ribbon\"},{\"id\":\"rd1\",\"platform\":\"reddit\",\"username\":\"ribbon\",\"cookie\":\"COOKIE\"},{\"id\":\"rd2\",\"platform\":\"reddit\",\"username\":\"nocookie\"}],\"byVideos\":[{\"id\":\"yt1\",\"platform\":\"youtube\",\"channelId\":\"UC1\",\"uploadsPlaylistId\":\"UU1\",\"title\":\"Alpha\",\"subscriberCount\":100,\"viewCount\":5000,\"videoCount\":10,\"publishedAt\":\"2024-09-01T00:00:00Z\",\"thumbnails\":{\"default\":\"a.jpg\"}},{\"id\":\"yt2\",\"platform\":\"youtube\",\"channelId\":\"UC2\",\"uploadsPlaylistId\":\"UU2\",\"title\":\"Beta\",\"subscriberCount\":300,\"viewCount\":1000,\"videoCount\":0},{\"id\":\"yt3\",\"platform\":\"youtube\",\"channelId\":\"UC3\",\"title\":\"Gamma\"},{\"id\":\"ig1\",\"platform\":\"instagram\",\"igUserId\":\"IG1\",\"accessToken\":\"T1\",\"username\":\"ribbon\"},{\"id\":\"rd1\",\"platform\":\"reddit\",\"username\":\"ribbon\",\"cookie\":\"COOKIE\"},{\"id\":\"rd2\",\"platform\":\"reddit\",\"username\":\"nocookie\"}]},\"totals\":{\"subscribers\":400,\"views\":6000,\"videos\":10},\"accountCount\":6,\"bestBySubscribers\":{\"id\":\"yt2\",\"platform\":\"youtube\",\"channelId\":\"UC2\",\"uploadsPlaylistId\":\"UU2\",\"title\":\"Beta\",\"subscriberCount\":300,\"viewCount\":1000,\"videoCount\":0},\"bestByViews\":{\"id\":\"yt1\",\"platform\":\"youtube\",\"channelId\":\"UC1\",\"uploadsPlaylistId\":\"UU1\",\"title\":\"Alpha\",\"subscriberCount\":100,\"viewCount\":5000,\"videoCount\":10,\"publishedAt\":\"2024-09-01T00:00:00Z\",\"thumbnails\":{\"default\":\"a.jpg\"}},\"bestByVideos\":{\"id\":\"yt1\",\"platform\":\"youtube\",\"channelId\":\"UC1\",\"uploadsPlaylistId\":\"UU1\",\"title\":\"Alpha\",\"subscriberCount\":100,\"viewCount\":5000,\"videoCount\":10,\"publishedAt\":\"2024-09-01T00:00:00Z\",\"thumbnails\":{\"default\":\"a.jpg\"}}}}"
+ "body": "{\"success\":true,\"comparison\":{\"rankings\":{\"bySubscribers\":[{\"id\":\"yt2\",\"platform\":\"youtube\",\"channelId\":\"UC2\",\"uploadsPlaylistId\":\"UU2\",\"title\":\"Beta\",\"subscriberCount\":300,\"viewCount\":1000,\"videoCount\":0,\"hasCookie\":false},{\"id\":\"yt1\",\"platform\":\"youtube\",\"channelId\":\"UC1\",\"uploadsPlaylistId\":\"UU1\",\"title\":\"Alpha\",\"subscriberCount\":100,\"viewCount\":5000,\"videoCount\":10,\"publishedAt\":\"2024-09-01T00:00:00Z\",\"thumbnails\":{\"default\":\"a.jpg\"},\"hasCookie\":false},{\"id\":\"yt3\",\"platform\":\"youtube\",\"channelId\":\"UC3\",\"title\":\"Gamma\",\"hasCookie\":false}],\"byViews\":[{\"id\":\"yt1\",\"platform\":\"youtube\",\"channelId\":\"UC1\",\"uploadsPlaylistId\":\"UU1\",\"title\":\"Alpha\",\"subscriberCount\":100,\"viewCount\":5000,\"videoCount\":10,\"publishedAt\":\"2024-09-01T00:00:00Z\",\"thumbnails\":{\"default\":\"a.jpg\"},\"hasCookie\":false},{\"id\":\"yt2\",\"platform\":\"youtube\",\"channelId\":\"UC2\",\"uploadsPlaylistId\":\"UU2\",\"title\":\"Beta\",\"subscriberCount\":300,\"viewCount\":1000,\"videoCount\":0,\"hasCookie\":false},{\"id\":\"yt3\",\"platform\":\"youtube\",\"channelId\":\"UC3\",\"title\":\"Gamma\",\"hasCookie\":false}],\"byVideos\":[{\"id\":\"yt1\",\"platform\":\"youtube\",\"channelId\":\"UC1\",\"uploadsPlaylistId\":\"UU1\",\"title\":\"Alpha\",\"subscriberCount\":100,\"viewCount\":5000,\"videoCount\":10,\"publishedAt\":\"2024-09-01T00:00:00Z\",\"thumbnails\":{\"default\":\"a.jpg\"},\"hasCookie\":false},{\"id\":\"yt2\",\"platform\":\"youtube\",\"channelId\":\"UC2\",\"uploadsPlaylistId\":\"UU2\",\"title\":\"Beta\",\"subscriberCount\":300,\"viewCount\":1000,\"videoCount\":0,\"hasCookie\":false},{\"id\":\"yt3\",\"platform\":\"youtube\",\"channelId\":\"UC3\",\"title\":\"Gamma\",\"hasCookie\":false}]},\"totals\":{\"subscribers\":400,\"views\":6000,\"videos\":10},\"accountCount\":3,\"bestBySubscribers\":{\"id\":\"yt2\",\"platform\":\"youtube\",\"channelId\":\"UC2\",\"uploadsPlaylistId\":\"UU2\",\"title\":\"Beta\",\"subscriberCount\":300,\"viewCount\":1000,\"videoCount\":0,\"hasCookie\":false},\"bestByViews\":{\"id\":\"yt1\",\"platform\":\"youtube\",\"channelId\":\"UC1\",\"uploadsPlaylistId\":\"UU1\",\"title\":\"Alpha\",\"subscriberCount\":100,\"viewCount\":5000,\"videoCount\":10,\"publishedAt\":\"2024-09-01T00:00:00Z\",\"thumbnails\":{\"default\":\"a.jpg\"},\"hasCookie\":false},\"bestByVideos\":{\"id\":\"yt1\",\"platform\":\"youtube\",\"channelId\":\"UC1\",\"uploadsPlaylistId\":\"UU1\",\"title\":\"Alpha\",\"subscriberCount\":100,\"viewCount\":5000,\"videoCount\":10,\"publishedAt\":\"2024-09-01T00:00:00Z\",\"thumbnails\":{\"default\":\"a.jpg\"},\"hasCookie\":false}}}"
},
{
"call": "GET /videos/all",
diff --git a/backend/tests/fixtures/golden.ts b/backend/tests/fixtures/golden.ts
index fe5558b..c091c51 100644
--- a/backend/tests/fixtures/golden.ts
+++ b/backend/tests/fixtures/golden.ts
@@ -1,5 +1,5 @@
import assert from 'node:assert/strict';
-import { readFileSync } from 'node:fs';
+import { readFileSync, writeFileSync } from 'node:fs';
// json drops undefined and non-finite numbers, so mark them to keep them visible in the snapshot
function encodeValue(_key: string, value: unknown): unknown {
@@ -18,8 +18,12 @@ export function readGolden(name: string): unknown {
// the string comparison also pins key order, which deepStrictEqual ignores
export function assertGolden(actual: unknown, name: string): void {
- const expected = readGolden(name);
const actualSnapshot = snapshot(actual);
+ // UPDATE_GOLDEN=<name> rewrites one snapshot, only for an intended behaviour change
+ if (process.env.UPDATE_GOLDEN === name) {
+ writeFileSync(new URL(`./${name}.golden.json`, import.meta.url), `${JSON.stringify(actualSnapshot, null, 4)}\n`);
+ }
+ const expected = readGolden(name);
assert.deepStrictEqual(actualSnapshot, expected);
assert.equal(JSON.stringify(actualSnapshot), JSON.stringify(expected));
}
diff --git a/backend/tests/routes/analytics.test.ts b/backend/tests/routes/analytics.test.ts
index d125e07..4565d30 100644
--- a/backend/tests/routes/analytics.test.ts
+++ b/backend/tests/routes/analytics.test.ts
@@ -14,6 +14,8 @@ import { mountWithContext, callRoute } from '../fixtures/routeHarness.ts';
import type { RouteStep } from '../fixtures/routeHarness.ts';
import type { AppEnv } from '../../types/app.ts';
import type { Env } from '../../types/env.ts';
+import type { YouTubeComparisonResponse } from '../../../shared/types/youtube.ts';
+import assert from 'node:assert/strict';
interface StoreWrite {
key: string;
@@ -122,6 +124,18 @@ describe('analytics and overview routes golden output', () => {
assertGolden({ result, withoutKey }, 'analyticsRoutes.youtube');
});
+ test('the comparison ranks YouTube channels only and never returns stored secrets', async () => {
+ const supabase = fakeSupabase(ACCOUNT_ROWS);
+ const app = mountWithContext(server => server.route('/', analyticsRouter), { supabase, redis: null, userId: 'user-1' });
+ const response = await callRoute(app, 'GET', '/comparison');
+ const body = JSON.parse(response.body) as YouTubeComparisonResponse;
+ const ranked = body.comparison!.rankings.bySubscribers;
+ assert.deepEqual(ranked.map(account => account.id), ['yt2', 'yt1', 'yt3']);
+ for (const secret of ['accessToken', 'cookie', 'encryptedPassword', 'encryptedTotpSecret', 'authToken', 'ct0']) {
+ assert.equal(response.body.includes(`"${secret}"`), false, `${secret} leaked`);
+ }
+ });
+
test('an empty account list has no comparison', async () => {
const supabase = fakeSupabase([]);
const app = mountWithContext(server => server.route('/', analyticsRouter), { supabase, redis: null, userId: 'user-1' });
diff --git a/shared/types/youtube.ts b/shared/types/youtube.ts
index 1c30ab5..072f5c4 100644
--- a/shared/types/youtube.ts
+++ b/shared/types/youtube.ts
@@ -1,4 +1,4 @@
-import type { Account, ChannelThumbnails } from './domain.ts';
+import type { Account, ChannelThumbnails, SafeAccount } from './domain.ts';
// a video as normalizeYouTubeVideo() stores it in the videos: cache
export interface YouTubeVideo {
@@ -241,9 +241,10 @@ export interface YouTubeVideosResponse {
}
// GET /api/comparison; comparison is null when the user has no accounts
+// GET /api/comparison ranks YouTube channels only, with secrets removed by safeAccount()
export interface YouTubeComparisonResponse {
success: true;
- comparison: YouTubeAccountComparison | null;
+ comparison: YouTubeAccountComparison<SafeAccount> | null;
}
export interface YouTubeLibraryChannel {