mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-11-22 04:46:07 -08:00
fix(stats): reject malformed resource IDs before mutations (#259)
This commit is contained in:
1 parent
7959e1a4e5
commit
f9c4e892dc
8 files changed
+300
-105
No files matched your search
@@ -6,6 +6,7 @@ import {
|
||||
loadKnownWordsSet,
|
||||
parseEventTypesQuery,
|
||||
parseIntQuery,
|
||||
parsePositiveId,
|
||||
parseTrendFillEmpty,
|
||||
parseTrendGroupBy,
|
||||
parseTrendRange,
|
||||
@@ -83,8 +84,8 @@ export function registerStatsAnalyticsRoutes(
|
||||
});
|
||||
|
||||
app.get('/api/stats/sessions/:id/timeline', async (c) => {
|
||||
const id = parseIntQuery(c.req.param('id'), 0);
|
||||
if (id <= 0) return c.json(statsJson('sessionTimeline', []), 400);
|
||||
const id = parsePositiveId(c.req.param('id'));
|
||||
if (id === null) return c.json(statsJson('sessionTimeline', []), 400);
|
||||
const rawLimit = c.req.query('limit');
|
||||
const limit = rawLimit === undefined ? undefined : parseIntQuery(rawLimit, 200, 1000);
|
||||
const timeline = await tracker.getSessionTimeline(id, limit);
|
||||
@@ -92,8 +93,8 @@ export function registerStatsAnalyticsRoutes(
|
||||
});
|
||||
|
||||
app.get('/api/stats/sessions/:id/events', async (c) => {
|
||||
const id = parseIntQuery(c.req.param('id'), 0);
|
||||
if (id <= 0) return c.json(statsJson('sessionEvents', []), 400);
|
||||
const id = parsePositiveId(c.req.param('id'));
|
||||
if (id === null) return c.json(statsJson('sessionEvents', []), 400);
|
||||
const limit = parseIntQuery(c.req.query('limit'), 500, 1000);
|
||||
const eventTypes = parseEventTypesQuery(c.req.query('types'));
|
||||
const events = await tracker.getSessionEvents(id, limit, eventTypes);
|
||||
@@ -101,8 +102,8 @@ export function registerStatsAnalyticsRoutes(
|
||||
});
|
||||
|
||||
app.get('/api/stats/sessions/:id/known-words-timeline', async (c) => {
|
||||
const id = parseIntQuery(c.req.param('id'), 0);
|
||||
if (id <= 0) return c.json(statsJson('sessionKnownWordsTimeline', []), 400);
|
||||
const id = parsePositiveId(c.req.param('id'));
|
||||
if (id === null) return c.json(statsJson('sessionKnownWordsTimeline', []), 400);
|
||||
|
||||
const knownWordsSet = loadKnownWordsSet(options?.knownWordCachePath) ?? new Set<string>();
|
||||
|
||||
|
||||
@@ -12,8 +12,10 @@ import {
|
||||
buildAnkiNotePreview,
|
||||
countKnownWords,
|
||||
enrichSessionsWithKnownWordMetrics,
|
||||
isPositiveSafeInteger,
|
||||
loadKnownWordsSet,
|
||||
parseIntQuery,
|
||||
parsePositiveId,
|
||||
parsePositiveIdList,
|
||||
} from './route-support.js';
|
||||
|
||||
const ANKI_CONNECT_FETCH_TIMEOUT_MS = 3_000;
|
||||
@@ -87,8 +89,8 @@ export function registerStatsIntegrationRoutes(
|
||||
});
|
||||
|
||||
app.get('/api/stats/anime/:animeId/known-words-summary', async (c) => {
|
||||
const animeId = parseIntQuery(c.req.param('animeId'), 0);
|
||||
if (animeId <= 0) {
|
||||
const animeId = parsePositiveId(c.req.param('animeId'));
|
||||
if (animeId === null) {
|
||||
return c.json(
|
||||
statsJson('animeKnownWordsSummary', { totalUniqueWords: 0, knownWordCount: 0 }),
|
||||
400,
|
||||
@@ -105,8 +107,8 @@ export function registerStatsIntegrationRoutes(
|
||||
});
|
||||
|
||||
app.get('/api/stats/media/:videoId/known-words-summary', async (c) => {
|
||||
const videoId = parseIntQuery(c.req.param('videoId'), 0);
|
||||
if (videoId <= 0) {
|
||||
const videoId = parsePositiveId(c.req.param('videoId'));
|
||||
if (videoId === null) {
|
||||
return c.json(
|
||||
statsJson('mediaKnownWordsSummary', { totalUniqueWords: 0, knownWordCount: 0 }),
|
||||
400,
|
||||
@@ -123,14 +125,10 @@ export function registerStatsIntegrationRoutes(
|
||||
});
|
||||
|
||||
app.patch('/api/stats/anime/:animeId/anilist', async (c) => {
|
||||
const animeId = parseIntQuery(c.req.param('animeId'), 0);
|
||||
if (animeId <= 0) return c.body(null, 400);
|
||||
const animeId = parsePositiveId(c.req.param('animeId'));
|
||||
if (animeId === null) return c.body(null, 400);
|
||||
const body = await c.req.json().catch(() => null);
|
||||
if (
|
||||
typeof body?.anilistId !== 'number' ||
|
||||
!Number.isInteger(body.anilistId) ||
|
||||
body.anilistId <= 0
|
||||
) {
|
||||
if (!isPositiveSafeInteger(body?.anilistId)) {
|
||||
return c.body(null, 400);
|
||||
}
|
||||
await tracker.reassignAnimeAnilist(animeId, body);
|
||||
@@ -140,8 +138,8 @@ export function registerStatsIntegrationRoutes(
|
||||
registerStatsCoverRoutes(app, tracker);
|
||||
|
||||
app.get('/api/stats/episode/:videoId/detail', async (c) => {
|
||||
const videoId = parseIntQuery(c.req.param('videoId'), 0);
|
||||
if (videoId <= 0) return c.body(null, 400);
|
||||
const videoId = parsePositiveId(c.req.param('videoId'));
|
||||
if (videoId === null) return c.body(null, 400);
|
||||
const rawSessions = await tracker.getEpisodeSessions(videoId);
|
||||
const words = await tracker.getEpisodeWords(videoId);
|
||||
const cardEvents = await tracker.getEpisodeCardEvents(videoId);
|
||||
@@ -154,8 +152,8 @@ export function registerStatsIntegrationRoutes(
|
||||
});
|
||||
|
||||
app.post('/api/stats/anki/browse', async (c) => {
|
||||
const noteId = parseIntQuery(c.req.query('noteId'), 0);
|
||||
if (noteId <= 0) return c.body(null, 400);
|
||||
const noteId = parsePositiveId(c.req.query('noteId'));
|
||||
if (noteId === null) return c.body(null, 400);
|
||||
const ankiConfig = getAnkiConnectConfig();
|
||||
try {
|
||||
const response = await fetch(ankiConfig?.url ?? 'http://127.0.0.1:8765', {
|
||||
@@ -177,19 +175,14 @@ export function registerStatsIntegrationRoutes(
|
||||
|
||||
app.post('/api/stats/anki/notesInfo', async (c) => {
|
||||
const body = await c.req.json().catch(() => null);
|
||||
const noteIds: number[] = Array.isArray(body?.noteIds)
|
||||
? body.noteIds.filter(
|
||||
(id: unknown): id is number => typeof id === 'number' && Number.isInteger(id) && id > 0,
|
||||
)
|
||||
: [];
|
||||
const noteIds = parsePositiveIdList(body?.noteIds);
|
||||
if (!noteIds) return c.body(null, 400);
|
||||
if (noteIds.length === 0) return c.json(statsJson('ankiNotesInfo', []));
|
||||
const resolvedNoteIds = Array.from(
|
||||
new Set(
|
||||
noteIds.map((noteId) => {
|
||||
const resolvedNoteId = options?.resolveAnkiNoteId?.(noteId);
|
||||
return Number.isInteger(resolvedNoteId) && (resolvedNoteId as number) > 0
|
||||
? (resolvedNoteId as number)
|
||||
: noteId;
|
||||
return isPositiveSafeInteger(resolvedNoteId) ? resolvedNoteId : noteId;
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -8,12 +8,14 @@ import type { ImmersionTrackerService } from '../immersion-tracker-service.js';
|
||||
import {
|
||||
buildSentenceSearchOptions,
|
||||
enrichSessionsWithKnownWordMetrics,
|
||||
isPositiveSafeInteger,
|
||||
loadKnownWordsSet,
|
||||
parseBooleanQuery,
|
||||
parseDuplicateLineCleanupBody,
|
||||
parseExcludedWordsBody,
|
||||
parseIntQuery,
|
||||
parsePositiveId,
|
||||
parsePositiveIdList,
|
||||
loadKnownWordsSet,
|
||||
} from './route-support.js';
|
||||
|
||||
export function registerStatsLibraryRoutes(
|
||||
@@ -116,8 +118,8 @@ export function registerStatsLibraryRoutes(
|
||||
});
|
||||
|
||||
app.get('/api/stats/vocabulary/:wordId/detail', async (c) => {
|
||||
const wordId = parseIntQuery(c.req.param('wordId'), 0);
|
||||
if (wordId <= 0) return c.body(null, 400);
|
||||
const wordId = parsePositiveId(c.req.param('wordId'));
|
||||
if (wordId === null) return c.body(null, 400);
|
||||
const detail = await tracker.getWordDetail(wordId);
|
||||
if (!detail) return c.body(null, 404);
|
||||
const animeAppearances = await tracker.getWordAnimeAppearances(wordId);
|
||||
@@ -126,8 +128,8 @@ export function registerStatsLibraryRoutes(
|
||||
});
|
||||
|
||||
app.get('/api/stats/kanji/:kanjiId/detail', async (c) => {
|
||||
const kanjiId = parseIntQuery(c.req.param('kanjiId'), 0);
|
||||
if (kanjiId <= 0) return c.body(null, 400);
|
||||
const kanjiId = parsePositiveId(c.req.param('kanjiId'));
|
||||
if (kanjiId === null) return c.body(null, 400);
|
||||
const detail = await tracker.getKanjiDetail(kanjiId);
|
||||
if (!detail) return c.body(null, 404);
|
||||
const animeAppearances = await tracker.getKanjiAnimeAppearances(kanjiId);
|
||||
@@ -141,8 +143,8 @@ export function registerStatsLibraryRoutes(
|
||||
});
|
||||
|
||||
app.get('/api/stats/media/:videoId', async (c) => {
|
||||
const videoId = parseIntQuery(c.req.param('videoId'), 0);
|
||||
if (videoId <= 0) return c.json(statsJson('error', null), 400);
|
||||
const videoId = parsePositiveId(c.req.param('videoId'));
|
||||
if (videoId === null) return c.json(statsJson('error', null), 400);
|
||||
const [detail, rawSessions, rollups] = await Promise.all([
|
||||
tracker.getMediaDetail(videoId),
|
||||
tracker.getMediaSessions(videoId, 100),
|
||||
@@ -167,16 +169,16 @@ export function registerStatsLibraryRoutes(
|
||||
});
|
||||
|
||||
app.delete('/api/stats/anime/merge-recommendations/:recommendationId', async (c) => {
|
||||
const recommendationId = parseIntQuery(c.req.param('recommendationId'), 0);
|
||||
if (recommendationId <= 0) return c.body(null, 400);
|
||||
const recommendationId = parsePositiveId(c.req.param('recommendationId'));
|
||||
if (recommendationId === null) return c.body(null, 400);
|
||||
const dismissed = await tracker.dismissAnimeMergeRecommendation(recommendationId);
|
||||
if (!dismissed) return c.body(null, 404);
|
||||
return c.json(statsJson('dismissAnimeMergeRecommendation', { ok: true }));
|
||||
});
|
||||
|
||||
app.get('/api/stats/anime/:animeId', async (c) => {
|
||||
const animeId = parseIntQuery(c.req.param('animeId'), 0);
|
||||
if (animeId <= 0) return c.body(null, 400);
|
||||
const animeId = parsePositiveId(c.req.param('animeId'));
|
||||
if (animeId === null) return c.body(null, 400);
|
||||
const detail = await tracker.getAnimeDetail(animeId);
|
||||
if (!detail) return c.body(null, 404);
|
||||
const [episodes, anilistEntries] = await Promise.all([
|
||||
@@ -187,22 +189,22 @@ export function registerStatsLibraryRoutes(
|
||||
});
|
||||
|
||||
app.get('/api/stats/anime/:animeId/words', async (c) => {
|
||||
const animeId = parseIntQuery(c.req.param('animeId'), 0);
|
||||
const animeId = parsePositiveId(c.req.param('animeId'));
|
||||
const limit = parseIntQuery(c.req.query('limit'), 50, 200);
|
||||
if (animeId <= 0) return c.body(null, 400);
|
||||
if (animeId === null) return c.body(null, 400);
|
||||
return c.json(statsJson('animeWords', await tracker.getAnimeWords(animeId, limit)));
|
||||
});
|
||||
|
||||
app.get('/api/stats/anime/:animeId/rollups', async (c) => {
|
||||
const animeId = parseIntQuery(c.req.param('animeId'), 0);
|
||||
const animeId = parsePositiveId(c.req.param('animeId'));
|
||||
const limit = parseIntQuery(c.req.query('limit'), 90, 365);
|
||||
if (animeId <= 0) return c.body(null, 400);
|
||||
if (animeId === null) return c.body(null, 400);
|
||||
return c.json(statsJson('animeRollups', await tracker.getAnimeDailyRollups(animeId, limit)));
|
||||
});
|
||||
|
||||
app.patch('/api/stats/media/:videoId/watched', async (c) => {
|
||||
const videoId = parseIntQuery(c.req.param('videoId'), 0);
|
||||
if (videoId <= 0) return c.body(null, 400);
|
||||
const videoId = parsePositiveId(c.req.param('videoId'));
|
||||
if (videoId === null) return c.body(null, 400);
|
||||
const body = await c.req.json().catch(() => null);
|
||||
const watched = typeof body?.watched === 'boolean' ? body.watched : true;
|
||||
await tracker.setVideoWatched(videoId, watched);
|
||||
@@ -211,42 +213,40 @@ export function registerStatsLibraryRoutes(
|
||||
|
||||
app.delete('/api/stats/sessions', async (c) => {
|
||||
const body = await c.req.json().catch(() => null);
|
||||
const ids = Array.isArray(body?.sessionIds)
|
||||
? body.sessionIds.filter(
|
||||
(id: unknown): id is number => Number.isSafeInteger(id) && (id as number) > 0,
|
||||
)
|
||||
: [];
|
||||
if (ids.length === 0) return c.body(null, 400);
|
||||
const ids = parsePositiveIdList(body?.sessionIds);
|
||||
if (!ids || ids.length === 0) return c.body(null, 400);
|
||||
await tracker.deleteSessions(ids);
|
||||
return c.json(statsJson('deleteSessions', { ok: true }));
|
||||
});
|
||||
|
||||
app.delete('/api/stats/sessions/:sessionId', async (c) => {
|
||||
const sessionId = parseIntQuery(c.req.param('sessionId'), 0);
|
||||
if (sessionId <= 0) return c.body(null, 400);
|
||||
const sessionId = parsePositiveId(c.req.param('sessionId'));
|
||||
if (sessionId === null) return c.body(null, 400);
|
||||
await tracker.deleteSession(sessionId);
|
||||
return c.json(statsJson('deleteSession', { ok: true }));
|
||||
});
|
||||
|
||||
app.delete('/api/stats/media/:videoId', async (c) => {
|
||||
const videoId = parseIntQuery(c.req.param('videoId'), 0);
|
||||
if (videoId <= 0) return c.body(null, 400);
|
||||
const videoId = parsePositiveId(c.req.param('videoId'));
|
||||
if (videoId === null) return c.body(null, 400);
|
||||
await tracker.deleteVideo(videoId);
|
||||
return c.json(statsJson('deleteVideo', { ok: true }));
|
||||
});
|
||||
|
||||
app.delete('/api/stats/anime/:animeId', async (c) => {
|
||||
const animeId = parseIntQuery(c.req.param('animeId'), 0);
|
||||
if (animeId <= 0) return c.body(null, 400);
|
||||
const animeId = parsePositiveId(c.req.param('animeId'));
|
||||
if (animeId === null) return c.body(null, 400);
|
||||
await tracker.deleteAnime(animeId);
|
||||
return c.json(statsJson('deleteAnime', { ok: true }));
|
||||
});
|
||||
|
||||
app.post('/api/stats/anime/:animeId/merge', async (c) => {
|
||||
const animeId = parseIntQuery(c.req.param('animeId'), 0);
|
||||
if (animeId <= 0) return c.body(null, 400);
|
||||
const animeId = parsePositiveId(c.req.param('animeId'));
|
||||
if (animeId === null) return c.body(null, 400);
|
||||
const body = await c.req.json().catch(() => null);
|
||||
const sourceAnimeIds = parsePositiveIdList(body?.sourceAnimeIds).filter((id) => id !== animeId);
|
||||
const parsedSourceAnimeIds = parsePositiveIdList(body?.sourceAnimeIds);
|
||||
if (!parsedSourceAnimeIds) return c.body(null, 400);
|
||||
const sourceAnimeIds = parsedSourceAnimeIds.filter((id) => id !== animeId);
|
||||
if (sourceAnimeIds.length === 0) return c.body(null, 400);
|
||||
let summary: Awaited<ReturnType<typeof tracker.mergeAnime>>;
|
||||
try {
|
||||
@@ -271,11 +271,11 @@ export function registerStatsLibraryRoutes(
|
||||
});
|
||||
|
||||
app.patch('/api/stats/media/:videoId/anime', async (c) => {
|
||||
const videoId = parseIntQuery(c.req.param('videoId'), 0);
|
||||
if (videoId <= 0) return c.body(null, 400);
|
||||
const videoId = parsePositiveId(c.req.param('videoId'));
|
||||
if (videoId === null) return c.body(null, 400);
|
||||
const body = await c.req.json().catch(() => null);
|
||||
const animeId = Number.isSafeInteger(body?.animeId) ? (body.animeId as number) : 0;
|
||||
if (animeId <= 0) return c.body(null, 400);
|
||||
const animeId = body?.animeId;
|
||||
if (!isPositiveSafeInteger(animeId)) return c.body(null, 400);
|
||||
try {
|
||||
const summary = await tracker.moveVideoToAnime(videoId, animeId);
|
||||
return c.json(
|
||||
|
||||
@@ -48,6 +48,16 @@ export function parseIntQuery(
|
||||
return maxLimit === undefined ? parsed : Math.min(parsed, maxLimit);
|
||||
}
|
||||
|
||||
export function isPositiveSafeInteger(value: unknown): value is number {
|
||||
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0;
|
||||
}
|
||||
|
||||
export function parsePositiveId(raw: string | undefined): number | null {
|
||||
if (raw === undefined) return null;
|
||||
const value = Number(raw);
|
||||
return isPositiveSafeInteger(value) && String(value) === raw ? value : null;
|
||||
}
|
||||
|
||||
export function parseTrendRange(raw: string | undefined): '7d' | '30d' | '90d' | '365d' | 'all' {
|
||||
return raw === '7d' || raw === '30d' || raw === '90d' || raw === '365d' || raw === 'all'
|
||||
? raw
|
||||
@@ -199,16 +209,16 @@ export async function enrichSessionsWithKnownWordMetrics<
|
||||
);
|
||||
}
|
||||
|
||||
/** Deduplicated positive integer ids from an untrusted JSON body field. */
|
||||
export function parsePositiveIdList(raw: unknown): number[] {
|
||||
if (!Array.isArray(raw)) return [];
|
||||
/** Deduplicated positive safe integer ids from an untrusted JSON body field. */
|
||||
export function parsePositiveIdList(raw: unknown, maxItems?: number): number[] | null {
|
||||
if (!Array.isArray(raw)) return null;
|
||||
const ids = new Set<number>();
|
||||
for (const value of raw) {
|
||||
if (Number.isSafeInteger(value) && (value as number) > 0) {
|
||||
ids.add(value as number);
|
||||
}
|
||||
if (!isPositiveSafeInteger(value)) return null;
|
||||
ids.add(value);
|
||||
}
|
||||
return [...ids];
|
||||
const parsed = [...ids];
|
||||
return maxItems === undefined ? parsed : parsed.slice(0, maxItems);
|
||||
}
|
||||
|
||||
export function parseBooleanQuery(raw: string | undefined, fallback: boolean): boolean {
|
||||
|
||||
Reference in new issue
Block a user