Handle exceptions, and prevent crashing from unhandled exceptions

This commit is contained in:
Ajay 2023-07-24 21:25:18 -04:00
parent 4e93a007c2
commit f63fa09605
17 changed files with 358 additions and 275 deletions

View file

@ -10,7 +10,11 @@ async function init() {
process.on("unhandledRejection", (error: any) => { process.on("unhandledRejection", (error: any) => {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.dir(error?.stack); console.dir(error?.stack);
process.exit(1); });
process.on("uncaughtExceptions", (error: any) => {
// eslint-disable-next-line no-console
console.dir(error?.stack);
}); });
try { try {

View file

@ -4,6 +4,7 @@ import { config } from "../config";
import { Request, Response } from "express"; import { Request, Response } from "express";
import { isUserVIP } from "../utils/isUserVIP"; import { isUserVIP } from "../utils/isUserVIP";
import { HashedUserID } from "../types/user.model"; import { HashedUserID } from "../types/user.model";
import { Logger } from "../utils/logger";
interface AddUserAsVIPRequest extends Request { interface AddUserAsVIPRequest extends Request {
query: { query: {
@ -34,6 +35,7 @@ export async function addUserAsVIP(req: AddUserAsVIPRequest, res: Response): Pro
// check to see if this user is already a vip // check to see if this user is already a vip
const userIsVIP = await isUserVIP(userID); const userIsVIP = await isUserVIP(userID);
try {
if (enabled && !userIsVIP) { if (enabled && !userIsVIP) {
// add them to the vip list // add them to the vip list
await db.prepare("run", 'INSERT INTO "vipUsers" VALUES(?)', [userID]); await db.prepare("run", 'INSERT INTO "vipUsers" VALUES(?)', [userID]);
@ -45,4 +47,9 @@ export async function addUserAsVIP(req: AddUserAsVIPRequest, res: Response): Pro
} }
return res.sendStatus(200); return res.sendStatus(200);
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
} }

View file

@ -6,6 +6,7 @@ import { ActionType, Category, Service, VideoID } from "../types/segments.model"
import { UserID } from "../types/user.model"; import { UserID } from "../types/user.model";
import { getService } from "../utils/getService"; import { getService } from "../utils/getService";
import { config } from "../config"; import { config } from "../config";
import { Logger } from "../utils/logger";
interface DeleteLockCategoriesRequest extends Request { interface DeleteLockCategoriesRequest extends Request {
body: { body: {
@ -53,7 +54,12 @@ export async function deleteLockCategoriesEndpoint(req: DeleteLockCategoriesRequ
}); });
} }
try {
await deleteLockCategories(videoID, categories, actionTypes, getService(service)); await deleteLockCategories(videoID, categories, actionTypes, getService(service));
} catch (e) {
Logger.error(e as string);
return res.status(500);
}
return res.status(200).json({ message: `Removed lock categories entries for video ${videoID}` }); return res.status(200).json({ message: `Removed lock categories entries for video ${videoID}` });
} }

View file

@ -164,6 +164,7 @@ export default async function dumpDatabase(req: Request, res: Response, showPage
<hr/> <hr/>
${updateQueued ? `Update queued.` : ``} Last updated: ${lastUpdate ? new Date(lastUpdate).toUTCString() : `Unknown`}`); ${updateQueued ? `Update queued.` : ``} Last updated: ${lastUpdate ? new Date(lastUpdate).toUTCString() : `Unknown`}`);
} else { } else {
try {
res.send({ res.send({
dbVersion: await getDbVersion(), dbVersion: await getDbVersion(),
lastUpdated: lastUpdate, lastUpdated: lastUpdate,
@ -176,6 +177,10 @@ export default async function dumpDatabase(req: Request, res: Response, showPage
}; };
}), }),
}); });
} catch (e) {
Logger.error(e as string);
res.sendStatus(500);
}
} }
await queueDump(); await queueDump();

View file

@ -25,6 +25,7 @@ let lastFetch: DBStatsData = {
updateExtensionUsers(); updateExtensionUsers();
export async function getBrandingStats(req: Request, res: Response): Promise<void> { export async function getBrandingStats(req: Request, res: Response): Promise<void> {
try {
const row = await getStats(); const row = await getStats();
lastFetch = row; lastFetch = row;
@ -39,6 +40,10 @@ export async function getBrandingStats(req: Request, res: Response): Promise<voi
titles: row.titles, titles: row.titles,
thumbnails: row.thumbnails, thumbnails: row.thumbnails,
}); });
} catch (e) {
Logger.error(e as string);
res.sendStatus(500);
}
} }
async function getStats(): Promise<DBStatsData> { async function getStats(): Promise<DBStatsData> {

View file

@ -1,7 +1,9 @@
import { db } from "../databases/databases"; import { db } from "../databases/databases";
import { Request, Response } from "express"; import { Request, Response } from "express";
import { Logger } from "../utils/logger";
export async function getDaysSavedFormatted(req: Request, res: Response): Promise<Response> { export async function getDaysSavedFormatted(req: Request, res: Response): Promise<Response> {
try {
const row = await db.prepare("get", 'SELECT SUM(("endTime" - "startTime") / 60 / 60 / 24 * "views") as "daysSaved" from "sponsorTimes" where "shadowHidden" != 1', []); const row = await db.prepare("get", 'SELECT SUM(("endTime" - "startTime") / 60 / 60 / 24 * "views") as "daysSaved" from "sponsorTimes" where "shadowHidden" != 1', []);
if (row !== undefined) { if (row !== undefined) {
@ -14,4 +16,8 @@ export async function getDaysSavedFormatted(req: Request, res: Response): Promis
daysSaved: 0 daysSaved: 0
}); });
} }
} catch (err) {
Logger.error(err as string);
return res.sendStatus(500);
}
} }

View file

@ -1,5 +1,6 @@
import { db } from "../databases/databases"; import { db } from "../databases/databases";
import { Request, Response } from "express"; import { Request, Response } from "express";
import { Logger } from "../utils/logger";
async function generateTopUsersStats(sortBy: string) { async function generateTopUsersStats(sortBy: string) {
const rows = await db.prepare("all", `SELECT COUNT(distinct "titles"."UUID") as "titleCount", COUNT(distinct "thumbnails"."UUID") as "thumbnailCount", COALESCE("userName", "titles"."userID") as "userName" const rows = await db.prepare("all", `SELECT COUNT(distinct "titles"."UUID") as "titleCount", COUNT(distinct "thumbnails"."UUID") as "thumbnailCount", COALESCE("userName", "titles"."userID") as "userName"
@ -36,8 +37,13 @@ export async function getTopBrandingUsers(req: Request, res: Response): Promise<
return res.status(503).send("Disabled for load reasons"); return res.status(503).send("Disabled for load reasons");
} }
try {
const stats = await generateTopUsersStats(sortBy); const stats = await generateTopUsersStats(sortBy);
//send this result //send this result
return res.send(stats); return res.send(stats);
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
} }

View file

@ -3,6 +3,7 @@ import { createMemoryCache } from "../utils/createMemoryCache";
import { config } from "../config"; import { config } from "../config";
import { Request, Response } from "express"; import { Request, Response } from "express";
import { validateCategories } from "../utils/parseParams"; import { validateCategories } from "../utils/parseParams";
import { Logger } from "../utils/logger";
const MILLISECONDS_IN_MINUTE = 60000; const MILLISECONDS_IN_MINUTE = 60000;
// eslint-disable-next-line @typescript-eslint/no-misused-promises // eslint-disable-next-line @typescript-eslint/no-misused-promises
@ -74,8 +75,13 @@ export async function getTopCategoryUsers(req: Request, res: Response): Promise<
return res.sendStatus(400); return res.sendStatus(400);
} }
try {
const stats = await getTopCategoryUsersWithCache(sortBy, category); const stats = await getTopCategoryUsersWithCache(sortBy, category);
//send this result //send this result
return res.send(stats); return res.send(stats);
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
} }

View file

@ -2,6 +2,7 @@ import { db } from "../databases/databases";
import { createMemoryCache } from "../utils/createMemoryCache"; import { createMemoryCache } from "../utils/createMemoryCache";
import { config } from "../config"; import { config } from "../config";
import { Request, Response } from "express"; import { Request, Response } from "express";
import { Logger } from "../utils/logger";
const MILLISECONDS_IN_MINUTE = 60000; const MILLISECONDS_IN_MINUTE = 60000;
// eslint-disable-next-line @typescript-eslint/no-misused-promises // eslint-disable-next-line @typescript-eslint/no-misused-promises
@ -92,8 +93,13 @@ export async function getTopUsers(req: Request, res: Response): Promise<Response
return res.status(503).send("Disabled for load reasons"); return res.status(503).send("Disabled for load reasons");
} }
try {
const stats = await getTopUsersWithCache(sortBy, categoryStatsEnabled); const stats = await getTopUsersWithCache(sortBy, categoryStatsEnabled);
//send this result //send this result
return res.send(stats); return res.send(stats);
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
} }

View file

@ -30,6 +30,7 @@ let lastFetch: DBStatsData = {
updateExtensionUsers(); updateExtensionUsers();
export async function getTotalStats(req: Request, res: Response): Promise<void> { export async function getTotalStats(req: Request, res: Response): Promise<void> {
try {
const countContributingUsers = Boolean(req.query?.countContributingUsers == "true"); const countContributingUsers = Boolean(req.query?.countContributingUsers == "true");
const row = await getStats(countContributingUsers); const row = await getStats(countContributingUsers);
lastFetch = row; lastFetch = row;
@ -55,6 +56,10 @@ export async function getTotalStats(req: Request, res: Response): Promise<void>
updateExtensionUsers(); updateExtensionUsers();
} }
} catch (e) {
Logger.error(e as string);
res.sendStatus(500);
}
} }
function getStats(countContributingUsers: boolean): Promise<DBStatsData> { function getStats(countContributingUsers: boolean): Promise<DBStatsData> {

View file

@ -1,6 +1,7 @@
import { db } from "../databases/databases"; import { db } from "../databases/databases";
import { Request, Response } from "express"; import { Request, Response } from "express";
import { UserID } from "../types/user.model"; import { UserID } from "../types/user.model";
import { Logger } from "../utils/logger";
function getFuzzyUserID(userName: string): Promise<{userName: string, userID: UserID }[]> { function getFuzzyUserID(userName: string): Promise<{userName: string, userID: UserID }[]> {
// escape [_ % \] to avoid ReDOS // escape [_ % \] to avoid ReDOS
@ -37,6 +38,8 @@ export async function getUserID(req: Request, res: Response): Promise<Response>
// invalid request // invalid request
return res.sendStatus(400); return res.sendStatus(400);
} }
try {
const results = exactSearch const results = exactSearch
? await getExactUserID(userName) ? await getExactUserID(userName)
: await getFuzzyUserID(userName); : await getFuzzyUserID(userName);
@ -49,4 +52,8 @@ export async function getUserID(req: Request, res: Response): Promise<Response>
} else { } else {
return res.send(results); return res.send(results);
} }
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
} }

View file

@ -205,6 +205,7 @@ async function getUserInfo(req: Request, res: Response): Promise<Response> {
return res.status(400).send("Invalid userID or publicUserID parameter"); return res.status(400).send("Invalid userID or publicUserID parameter");
} }
try {
const responseObj = {} as Record<string, string|SegmentUUID|number>; const responseObj = {} as Record<string, string|SegmentUUID|number>;
for (const property of paramValues) { for (const property of paramValues) {
responseObj[property] = await dbGetValue(hashedUserID, property); responseObj[property] = await dbGetValue(hashedUserID, property);
@ -218,6 +219,10 @@ async function getUserInfo(req: Request, res: Response): Promise<Response> {
} }
return res.send(responseObj); return res.send(responseObj);
} catch (err) {
Logger.error(err as string);
return res.sendStatus(500);
}
} }
export async function endpoint(req: Request, res: Response): Promise<Response> { export async function endpoint(req: Request, res: Response): Promise<Response> {

View file

@ -515,6 +515,7 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
return; return;
} }
try {
const isVIP = (await isUserVIP(userID)); const isVIP = (await isUserVIP(userID));
const isTempVIP = (await isUserTempVIP(userID, videoID)); const isTempVIP = (await isUserTempVIP(userID, videoID));
const rawIP = getIP(req); const rawIP = getIP(req);
@ -545,8 +546,6 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
//hash the ip 5000 times so no one can get it from the database //hash the ip 5000 times so no one can get it from the database
const hashedIP = await getHashCache(rawIP + config.globalSalt); const hashedIP = await getHashCache(rawIP + config.globalSalt);
try {
//get current time
const timeSubmitted = Date.now(); const timeSubmitted = Date.now();
// const rateLimitCheckResult = checkRateLimit(userID, videoID, service, timeSubmitted, hashedIP); // const rateLimitCheckResult = checkRateLimit(userID, videoID, service, timeSubmitted, hashedIP);
@ -620,18 +619,18 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
segment: segmentInfo.segment, segment: segmentInfo.segment,
}); });
} }
} catch (err) {
Logger.error(err as string);
lock.unlock();
return res.sendStatus(500);
}
for (let i = 0; i < segments.length; i++) { for (let i = 0; i < segments.length; i++) {
sendWebhooks(apiVideoDetails, userID, videoID, UUIDs[i], segments[i], service).catch((e) => Logger.error(`call send webhooks ${e}`)); sendWebhooks(apiVideoDetails, userID, videoID, UUIDs[i], segments[i], service).catch((e) => Logger.error(`call send webhooks ${e}`));
} }
lock.unlock();
return res.json(newSegments); return res.json(newSegments);
} catch (err) {
Logger.error(err as string);
return res.sendStatus(500);
} finally {
lock.unlock();
}
} }
// Takes an array of arrays: // Takes an array of arrays:

View file

@ -42,6 +42,7 @@ export async function postWarning(req: Request, res: Response): Promise<Response
let resultStatus = ""; let resultStatus = "";
try {
if (enabled) { if (enabled) {
const previousWarning = await db.prepare("get", 'SELECT * FROM "warnings" WHERE "userID" = ? AND "issuerUserID" = ? AND "type" = ?', [userID, issuerUserID, type]) as warningEntry; const previousWarning = await db.prepare("get", 'SELECT * FROM "warnings" WHERE "userID" = ? AND "issuerUserID" = ? AND "type" = ?', [userID, issuerUserID, type]) as warningEntry;
@ -91,4 +92,8 @@ export async function postWarning(req: Request, res: Response): Promise<Response
return res.status(200).json({ return res.status(200).json({
message: `Warning ${resultStatus} user '${userID}'.`, message: `Warning ${resultStatus} user '${userID}'.`,
}); });
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
} }

View file

@ -34,6 +34,7 @@ export async function setUsername(req: Request, res: Response): Promise<Response
// eslint-disable-next-line no-control-regex // eslint-disable-next-line no-control-regex
userName = userName.replace(/[\u0000-\u001F\u007F-\u009F]/g, ""); userName = userName.replace(/[\u0000-\u001F\u007F-\u009F]/g, "");
try {
// check privateID against publicID // check privateID against publicID
if (!await checkPrivateUsername(userName, userID)) { if (!await checkPrivateUsername(userName, userID)) {
return res.sendStatus(400); return res.sendStatus(400);
@ -56,7 +57,6 @@ export async function setUsername(req: Request, res: Response): Promise<Response
timings.push(Date.now()); timings.push(Date.now());
try {
const row = await db.prepare("get", `SELECT count(*) as "userCount" FROM "userNames" WHERE "userID" = ? AND "locked" = 1`, [userID]); const row = await db.prepare("get", `SELECT count(*) as "userCount" FROM "userNames" WHERE "userID" = ? AND "locked" = 1`, [userID]);
if (adminUserIDInput === undefined && row.userCount > 0) { if (adminUserIDInput === undefined && row.userCount > 0) {
return res.sendStatus(200); return res.sendStatus(200);

View file

@ -7,6 +7,7 @@ import { UserID } from "../types/user.model";
import { QueryCacher } from "../utils/queryCacher"; import { QueryCacher } from "../utils/queryCacher";
import { isUserVIP } from "../utils/isUserVIP"; import { isUserVIP } from "../utils/isUserVIP";
import { parseCategories, parseDeArrowTypes } from "../utils/parseParams"; import { parseCategories, parseDeArrowTypes } from "../utils/parseParams";
import { Logger } from "../utils/logger";
export async function shadowBanUser(req: Request, res: Response): Promise<Response> { export async function shadowBanUser(req: Request, res: Response): Promise<Response> {
const userID = req.query.userID as UserID; const userID = req.query.userID as UserID;
@ -36,6 +37,7 @@ export async function shadowBanUser(req: Request, res: Response): Promise<Respon
return res.sendStatus(400); return res.sendStatus(400);
} }
try {
//hash the userID //hash the userID
const adminUserID = await getHashCache(adminUserIDInput); const adminUserID = await getHashCache(adminUserIDInput);
@ -72,6 +74,10 @@ export async function shadowBanUser(req: Request, res: Response): Promise<Respon
} }
} }
return res.sendStatus(200); return res.sendStatus(200);
} catch (e) {
Logger.error(e as string);
return res.sendStatus(500);
}
} }
export async function banUser(userID: UserID, enabled: boolean, unHideOldSubmissions: boolean, export async function banUser(userID: UserID, enabled: boolean, unHideOldSubmissions: boolean,

View file

@ -17,6 +17,7 @@ export const validateLicenseKeyRegex = (token: string) =>
export async function verifyTokenRequest(req: VerifyTokenRequest, res: Response): Promise<Response> { export async function verifyTokenRequest(req: VerifyTokenRequest, res: Response): Promise<Response> {
const { query: { licenseKey } } = req; const { query: { licenseKey } } = req;
try {
if (!licenseKey) { if (!licenseKey) {
return res.status(400).send("Invalid request"); return res.status(400).send("Invalid request");
} else if (!validateLicenseKeyRegex(licenseKey)) { } else if (!validateLicenseKeyRegex(licenseKey)) {
@ -62,6 +63,10 @@ export async function verifyTokenRequest(req: VerifyTokenRequest, res: Response)
} }
} }
} catch (e) {
Logger.error(e as string);
return res.status(500);
}
} }
async function checkAllGumroadProducts(licenseKey: string): Promise<boolean> { async function checkAllGumroadProducts(licenseKey: string): Promise<boolean> {