api/cookie: validate service names for cookies

This commit is contained in:
jj 2024-11-26 13:44:51 +00:00
parent d7ae13213e
commit 31d65c9fb7
No known key found for this signature in database

View file

@ -7,13 +7,23 @@ import * as cluster from '../../misc/cluster.js';
import { isCluster } from '../../config.js'; import { isCluster } from '../../config.js';
const WRITE_INTERVAL = 60000; const WRITE_INTERVAL = 60000;
const VALID_SERVICES = new Set([
'instagram',
'instagram_bearer',
'reddit',
'twitter',
'youtube_oauth'
]);
const invalidCookies = {};
let cookies = {}, dirty = false, intervalId; let cookies = {}, dirty = false, intervalId;
function writeChanges(cookiePath) { function writeChanges(cookiePath) {
if (!dirty) return; if (!dirty) return;
dirty = false; dirty = false;
writeFile(cookiePath, JSON.stringify(cookies, null, 4)).catch(() => { const cookieData = JSON.stringify({ ...cookies, ...invalidCookies }, null, 4);
writeFile(cookiePath, cookieData).catch(() => {
clearInterval(intervalId) clearInterval(intervalId)
}) })
} }
@ -22,6 +32,14 @@ const setupMain = async (cookiePath) => {
try { try {
cookies = await readFile(cookiePath, 'utf8'); cookies = await readFile(cookiePath, 'utf8');
cookies = JSON.parse(cookies); cookies = JSON.parse(cookies);
for (const serviceName in cookies) {
if (!VALID_SERVICES.has(serviceName)) {
console.warn(`${Yellow('[!]')} ignoring unknown service in cookie file: ${serviceName}`);
invalidCookies[serviceName] = cookies[serviceName];
delete cookies[serviceName];
}
}
intervalId = setInterval(() => writeChanges(cookiePath), WRITE_INTERVAL); intervalId = setInterval(() => writeChanges(cookiePath), WRITE_INTERVAL);
cluster.broadcast({ cookies }); cluster.broadcast({ cookies });
@ -68,6 +86,11 @@ export const setup = async (cookiePath) => {
} }
export function getCookie(service) { export function getCookie(service) {
if (!VALID_SERVICES.has(service)) {
throw `${service} not in allowed services list for cookies.`
+ ' if adding a new cookie type, include it there.';
}
if (!cookies[service] || !cookies[service].length) return; if (!cookies[service] || !cookies[service].length) return;
const idx = Math.floor(Math.random() * cookies[service].length); const idx = Math.floor(Math.random() * cookies[service].length);