mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-07 20:17:02 +03:00
Merge pull request #602 from mini-bomba/request-validator
Create an engine for rule-based request validation
This commit is contained in:
@@ -69,10 +69,7 @@ addDefaults(config, {
|
|||||||
message: "OK",
|
message: "OK",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
validityCheck: {
|
requestValidatorRules: [],
|
||||||
userAgent: null,
|
|
||||||
userAgentR: null,
|
|
||||||
},
|
|
||||||
userCounterURL: null,
|
userCounterURL: null,
|
||||||
userCounterRatio: 10,
|
userCounterRatio: 10,
|
||||||
newLeafURLs: null,
|
newLeafURLs: null,
|
||||||
@@ -271,6 +268,8 @@ function loadFromEnv(config: SBSConfig, prefix = "") {
|
|||||||
config[key] = value === "true";
|
config[key] = value === "true";
|
||||||
} else if (key === "newLeafURLs") {
|
} else if (key === "newLeafURLs") {
|
||||||
config[key] = [value];
|
config[key] = [value];
|
||||||
|
} else if (key === "requestValidatorRules") {
|
||||||
|
config[key] = JSON.parse(value) ?? [];
|
||||||
} else {
|
} else {
|
||||||
config[key] = value;
|
config[key] = value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,9 @@ import { checkBanStatus } from "../utils/checkBan";
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { getMaxResThumbnail } from "../utils/youtubeApi";
|
import { getMaxResThumbnail } from "../utils/youtubeApi";
|
||||||
import { getVideoDetails } from "../utils/getVideoDetails";
|
import { getVideoDetails } from "../utils/getVideoDetails";
|
||||||
import { canSubmitDeArrow, validSubmittedData } from "../utils/permissions";
|
import { canSubmitDeArrow } from "../utils/permissions";
|
||||||
import { parseUserAgent } from "../utils/userAgent";
|
import { parseUserAgent } from "../utils/userAgent";
|
||||||
|
import { isRequestInvalid } from "../utils/requestValidator";
|
||||||
|
|
||||||
enum BrandingType {
|
enum BrandingType {
|
||||||
Title,
|
Title,
|
||||||
@@ -58,8 +59,21 @@ export async function postBranding(req: Request, res: Response) {
|
|||||||
const hashedIP = await getHashCache(getIP(req) + config.globalSalt as IPAddress);
|
const hashedIP = await getHashCache(getIP(req) + config.globalSalt as IPAddress);
|
||||||
const isBanned = await checkBanStatus(hashedUserID, hashedIP);
|
const isBanned = await checkBanStatus(hashedUserID, hashedIP);
|
||||||
|
|
||||||
if (!validSubmittedData(userAgent, req.headers["user-agent"])) {
|
if (isRequestInvalid({
|
||||||
Logger.warn(`Rejecting submission based on invalid data: ${hashedUserID} ${videoID} ${videoDuration} ${userAgent} ${req.headers["user-agent"]}`);
|
userAgent,
|
||||||
|
userAgentHeader: req.headers["user-agent"],
|
||||||
|
videoDuration,
|
||||||
|
videoID,
|
||||||
|
userID,
|
||||||
|
service,
|
||||||
|
dearrow: {
|
||||||
|
title,
|
||||||
|
thumbnail,
|
||||||
|
downvote,
|
||||||
|
},
|
||||||
|
endpoint: "dearrow-postBranding",
|
||||||
|
})) {
|
||||||
|
Logger.warn(`Dearrow submission rejected by request validator: ${hashedUserID} ${videoID} ${videoDuration} ${userAgent} ${req.headers["user-agent"]} ${title.title} ${thumbnail.timestamp}`);
|
||||||
res.status(200).send("OK");
|
res.status(200).send("OK");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import { QueryCacher } from "../utils/queryCacher";
|
|||||||
import { acquireLock } from "../utils/redisLock";
|
import { acquireLock } from "../utils/redisLock";
|
||||||
import { checkBanStatus } from "../utils/checkBan";
|
import { checkBanStatus } from "../utils/checkBan";
|
||||||
import { canSubmitDeArrow } from "../utils/permissions";
|
import { canSubmitDeArrow } from "../utils/permissions";
|
||||||
|
import { isRequestInvalid } from "../utils/requestValidator";
|
||||||
|
import { parseUserAgent } from "../utils/userAgent";
|
||||||
|
|
||||||
interface ExistingVote {
|
interface ExistingVote {
|
||||||
UUID: BrandingUUID;
|
UUID: BrandingUUID;
|
||||||
@@ -22,6 +24,7 @@ interface ExistingVote {
|
|||||||
|
|
||||||
export async function postCasual(req: Request, res: Response) {
|
export async function postCasual(req: Request, res: Response) {
|
||||||
const { videoID, userID, downvote } = req.body as CasualVoteSubmission;
|
const { videoID, userID, downvote } = req.body as CasualVoteSubmission;
|
||||||
|
const userAgent = req.body.userAgent ?? parseUserAgent(req.get("user-agent")) ?? "";
|
||||||
let categories = req.body.categories as CasualCategory[];
|
let categories = req.body.categories as CasualCategory[];
|
||||||
const title = (req.body.title as string)?.toLowerCase();
|
const title = (req.body.title as string)?.toLowerCase();
|
||||||
const service = getService(req.body.service);
|
const service = getService(req.body.service);
|
||||||
@@ -36,6 +39,19 @@ export async function postCasual(req: Request, res: Response) {
|
|||||||
return res.status(400).send("Bad Request");
|
return res.status(400).send("Bad Request");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isRequestInvalid({
|
||||||
|
userID,
|
||||||
|
videoID,
|
||||||
|
userAgent,
|
||||||
|
userAgentHeader: req.headers["user-agent"],
|
||||||
|
casualCategories: categories,
|
||||||
|
service,
|
||||||
|
endpoint: "dearrow-postCasual",
|
||||||
|
})) {
|
||||||
|
Logger.warn(`Casual vote rejected by request validator: ${userAgent} ${req.headers["user-agent"]} ${categories} ${service} ${videoID}`);
|
||||||
|
return res.status(200).send("OK");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const hashedUserID = await getHashCache(userID);
|
const hashedUserID = await getHashCache(userID);
|
||||||
const hashedVideoID = await getHashCache(videoID, 1);
|
const hashedVideoID = await getHashCache(videoID, 1);
|
||||||
|
|||||||
@@ -20,11 +20,12 @@ import { parseUserAgent } from "../utils/userAgent";
|
|||||||
import { getService } from "../utils/getService";
|
import { getService } from "../utils/getService";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { vote } from "./voteOnSponsorTime";
|
import { vote } from "./voteOnSponsorTime";
|
||||||
import { canSubmit, canSubmitGlobal, validSubmittedData } from "../utils/permissions";
|
import { canSubmit, canSubmitGlobal } from "../utils/permissions";
|
||||||
import { getVideoDetails, videoDetails } from "../utils/getVideoDetails";
|
import { getVideoDetails, videoDetails } from "../utils/getVideoDetails";
|
||||||
import * as youtubeID from "../utils/youtubeID";
|
import * as youtubeID from "../utils/youtubeID";
|
||||||
import { acquireLock } from "../utils/redisLock";
|
import { acquireLock } from "../utils/redisLock";
|
||||||
import { checkBanStatus } from "../utils/checkBan";
|
import { checkBanStatus } from "../utils/checkBan";
|
||||||
|
import { isRequestInvalid } from "../utils/requestValidator";
|
||||||
|
|
||||||
type CheckResult = {
|
type CheckResult = {
|
||||||
pass: boolean,
|
pass: boolean,
|
||||||
@@ -509,8 +510,17 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
|
|||||||
}
|
}
|
||||||
const userID: HashedUserID = await getHashCache(paramUserID);
|
const userID: HashedUserID = await getHashCache(paramUserID);
|
||||||
|
|
||||||
if (!validSubmittedData(userAgent, req.headers["user-agent"])) {
|
if (isRequestInvalid({
|
||||||
Logger.warn(`Rejecting submission based on invalid data: ${userID} ${videoID} ${videoDurationParam} ${userAgent} ${req.headers["user-agent"]}`);
|
userAgent,
|
||||||
|
userAgentHeader: req.headers["user-agent"],
|
||||||
|
videoDuration,
|
||||||
|
videoID,
|
||||||
|
userID: paramUserID,
|
||||||
|
service,
|
||||||
|
segments,
|
||||||
|
endpoint: "sponsorblock-postSkipSegments"
|
||||||
|
})) {
|
||||||
|
Logger.warn(`Sponsorblock submission rejected by request validator: ${userID} ${videoID} ${videoDurationParam} ${userAgent} ${req.headers["user-agent"]}`);
|
||||||
return res.status(200).send("OK");
|
return res.status(200).send("OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { getHashCache } from "../utils/getHashCache";
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import { isUserBanned } from "../utils/checkBan";
|
import { isUserBanned } from "../utils/checkBan";
|
||||||
import { HashedUserID } from "../types/user.model";
|
import { HashedUserID } from "../types/user.model";
|
||||||
|
import { isRequestInvalid } from "../utils/requestValidator";
|
||||||
|
|
||||||
function logUserNameChange(userID: string, newUserName: string, oldUserName: string, updatedByAdmin: boolean): Promise<Response> {
|
function logUserNameChange(userID: string, newUserName: string, oldUserName: string, updatedByAdmin: boolean): Promise<Response> {
|
||||||
return privateDB.prepare("run",
|
return privateDB.prepare("run",
|
||||||
@@ -15,7 +16,7 @@ function logUserNameChange(userID: string, newUserName: string, oldUserName: str
|
|||||||
|
|
||||||
export async function setUsername(req: Request, res: Response): Promise<Response> {
|
export async function setUsername(req: Request, res: Response): Promise<Response> {
|
||||||
const userIDInput = req.query.userID as string;
|
const userIDInput = req.query.userID as string;
|
||||||
const adminUserIDInput = req.query.adminUserID as string;
|
const adminUserIDInput = req.query.adminUserID as string | undefined;
|
||||||
let userName = req.query.username as string;
|
let userName = req.query.username as string;
|
||||||
let hashedUserID: HashedUserID;
|
let hashedUserID: HashedUserID;
|
||||||
|
|
||||||
@@ -29,16 +30,22 @@ export async function setUsername(req: Request, res: Response): Promise<Response
|
|||||||
return res.sendStatus(200);
|
return res.sendStatus(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
const timings = [Date.now()];
|
|
||||||
|
|
||||||
// remove unicode control characters from username (example: \n, \r, \t etc.)
|
// remove unicode control characters from username (example: \n, \r, \t etc.)
|
||||||
// source: https://en.wikipedia.org/wiki/Control_character#In_Unicode
|
// source: https://en.wikipedia.org/wiki/Control_character#In_Unicode
|
||||||
// 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 {
|
if (isRequestInvalid({
|
||||||
timings.push(Date.now());
|
userAgentHeader: req.headers["user-agent"],
|
||||||
|
userID: adminUserIDInput ?? userIDInput,
|
||||||
|
newUsername: userName,
|
||||||
|
endpoint: "setUsername",
|
||||||
|
})) {
|
||||||
|
Logger.warn(`Username change rejected by request validator: ${userName} ${req.headers["user-agent"]}`);
|
||||||
|
return res.sendStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
if (adminUserIDInput != undefined) {
|
if (adminUserIDInput != undefined) {
|
||||||
//this is the admin controlling the other users account, don't hash the controling account's ID
|
//this is the admin controlling the other users account, don't hash the controling account's ID
|
||||||
hashedUserID = userIDInput as HashedUserID;
|
hashedUserID = userIDInput as HashedUserID;
|
||||||
@@ -55,15 +62,11 @@ export async function setUsername(req: Request, res: Response): Promise<Response
|
|||||||
//hash the userID
|
//hash the userID
|
||||||
hashedUserID = await getHashCache(userIDInput) as HashedUserID;
|
hashedUserID = await getHashCache(userIDInput) as HashedUserID;
|
||||||
|
|
||||||
timings.push(Date.now());
|
|
||||||
|
|
||||||
const row = await db.prepare("get", `SELECT count(*) as "userCount" FROM "userNames" WHERE "userID" = ? AND "locked" = 1`, [hashedUserID]);
|
const row = await db.prepare("get", `SELECT count(*) as "userCount" FROM "userNames" WHERE "userID" = ? AND "locked" = 1`, [hashedUserID]);
|
||||||
if (row.userCount > 0) {
|
if (row.userCount > 0) {
|
||||||
return res.sendStatus(200);
|
return res.sendStatus(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
timings.push(Date.now());
|
|
||||||
|
|
||||||
if (await isUserBanned(hashedUserID)) {
|
if (await isUserBanned(hashedUserID)) {
|
||||||
return res.sendStatus(200);
|
return res.sendStatus(200);
|
||||||
}
|
}
|
||||||
@@ -80,8 +83,6 @@ export async function setUsername(req: Request, res: Response): Promise<Response
|
|||||||
const locked = adminUserIDInput === undefined ? 0 : 1;
|
const locked = adminUserIDInput === undefined ? 0 : 1;
|
||||||
let oldUserName = "";
|
let oldUserName = "";
|
||||||
|
|
||||||
timings.push(Date.now());
|
|
||||||
|
|
||||||
if (row?.userName !== undefined) {
|
if (row?.userName !== undefined) {
|
||||||
//already exists, update this row
|
//already exists, update this row
|
||||||
oldUserName = row.userName;
|
oldUserName = row.userName;
|
||||||
@@ -95,14 +96,9 @@ export async function setUsername(req: Request, res: Response): Promise<Response
|
|||||||
await db.prepare("run", `INSERT INTO "userNames"("userID", "userName", "locked") VALUES(?, ?, ?)`, [hashedUserID, userName, locked]);
|
await db.prepare("run", `INSERT INTO "userNames"("userID", "userName", "locked") VALUES(?, ?, ?)`, [hashedUserID, userName, locked]);
|
||||||
}
|
}
|
||||||
|
|
||||||
timings.push(Date.now());
|
|
||||||
|
|
||||||
await logUserNameChange(hashedUserID, userName, oldUserName, adminUserIDInput !== undefined);
|
await logUserNameChange(hashedUserID, userName, oldUserName, adminUserIDInput !== undefined);
|
||||||
|
|
||||||
timings.push(Date.now());
|
return res.sendStatus(200);
|
||||||
|
|
||||||
|
|
||||||
return res.status(200).send(timings.join(", "));
|
|
||||||
} catch (err) /* istanbul ignore next */ {
|
} catch (err) /* istanbul ignore next */ {
|
||||||
Logger.error(err as string);
|
Logger.error(err as string);
|
||||||
return res.sendStatus(500);
|
return res.sendStatus(500);
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ export interface BrandingHashDBResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface OriginalThumbnailSubmission {
|
export interface OriginalThumbnailSubmission {
|
||||||
|
timestamp?: undefined | null;
|
||||||
original: true;
|
original: true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,34 @@ export interface CustomPostgresReadOnlyConfig extends CustomPostgresConfig {
|
|||||||
stopRetryThreshold: number;
|
stopRetryThreshold: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ValidatorPattern = string | [string, string];
|
||||||
|
export interface RequestValidatorRule {
|
||||||
|
// mostly universal
|
||||||
|
userAgent?: ValidatorPattern;
|
||||||
|
userAgentHeader?: ValidatorPattern;
|
||||||
|
videoDuration?: ValidatorPattern;
|
||||||
|
videoID?: ValidatorPattern;
|
||||||
|
userID?: ValidatorPattern;
|
||||||
|
service?: ValidatorPattern;
|
||||||
|
endpoint?: ValidatorPattern;
|
||||||
|
// sb postSkipSegments
|
||||||
|
startTime?: ValidatorPattern;
|
||||||
|
endTime?: ValidatorPattern;
|
||||||
|
category?: ValidatorPattern;
|
||||||
|
actionType?: ValidatorPattern;
|
||||||
|
description?: ValidatorPattern;
|
||||||
|
// dearrow postBranding
|
||||||
|
title?: ValidatorPattern;
|
||||||
|
titleOriginal?: boolean;
|
||||||
|
thumbnailTimestamp?: ValidatorPattern;
|
||||||
|
thumbnailOriginal?: boolean;
|
||||||
|
dearrowDownvote?: boolean;
|
||||||
|
// postCasual
|
||||||
|
casualCategory?: ValidatorPattern;
|
||||||
|
// setUsername
|
||||||
|
newUsername?: ValidatorPattern;
|
||||||
|
}
|
||||||
|
|
||||||
export interface SBSConfig {
|
export interface SBSConfig {
|
||||||
[index: string]: any
|
[index: string]: any
|
||||||
port: number;
|
port: number;
|
||||||
@@ -85,10 +113,7 @@ export interface SBSConfig {
|
|||||||
vote: RateLimitConfig;
|
vote: RateLimitConfig;
|
||||||
view: RateLimitConfig;
|
view: RateLimitConfig;
|
||||||
};
|
};
|
||||||
validityCheck: {
|
requestValidatorRules: RequestValidatorRule[];
|
||||||
userAgent: string | null;
|
|
||||||
userAgentR: string | null;
|
|
||||||
}
|
|
||||||
minimumPrefix?: string;
|
minimumPrefix?: string;
|
||||||
maximumPrefix?: string;
|
maximumPrefix?: string;
|
||||||
redis?: RedisConfig;
|
redis?: RedisConfig;
|
||||||
|
|||||||
@@ -108,34 +108,6 @@ export async function canSubmit(userID: HashedUserID, category: Category): Promi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validSubmittedData(userAgent: string, userAgentR: string): boolean {
|
|
||||||
if (!config.validityCheck.userAgent) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const key of Object.keys(config.validityCheck)) {
|
|
||||||
const check = (config.validityCheck as Record<string, string | null>)[key];
|
|
||||||
if (check === null) {
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
switch (key) {
|
|
||||||
case "userAgent":
|
|
||||||
if (!userAgent.match(check)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "userAgentR":
|
|
||||||
if (!userAgentR.match(new RegExp(check))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function canSubmitGlobal(userID: HashedUserID): Promise<CanSubmitGlobalResult> {
|
export async function canSubmitGlobal(userID: HashedUserID): Promise<CanSubmitGlobalResult> {
|
||||||
const oldSubmitterOrAllowedPromise = oldSubmitterOrAllowed(userID);
|
const oldSubmitterOrAllowedPromise = oldSubmitterOrAllowed(userID);
|
||||||
|
|
||||||
|
|||||||
260
src/utils/requestValidator.ts
Normal file
260
src/utils/requestValidator.ts
Normal file
@@ -0,0 +1,260 @@
|
|||||||
|
import { config } from "../config";
|
||||||
|
import {
|
||||||
|
CasualCategory,
|
||||||
|
ThumbnailSubmission,
|
||||||
|
TitleSubmission,
|
||||||
|
} from "../types/branding.model";
|
||||||
|
import { ValidatorPattern, RequestValidatorRule } from "../types/config.model";
|
||||||
|
import { IncomingSegment } from "../types/segments.model";
|
||||||
|
|
||||||
|
export interface RequestValidatorInput {
|
||||||
|
userAgent?: string;
|
||||||
|
userAgentHeader?: string;
|
||||||
|
videoDuration?: string | number;
|
||||||
|
videoID?: string;
|
||||||
|
userID?: string;
|
||||||
|
service?: string;
|
||||||
|
segments?: IncomingSegment[];
|
||||||
|
dearrow?: {
|
||||||
|
title?: TitleSubmission;
|
||||||
|
thumbnail?: ThumbnailSubmission;
|
||||||
|
downvote: boolean;
|
||||||
|
};
|
||||||
|
casualCategories?: CasualCategory[];
|
||||||
|
newUsername?: string;
|
||||||
|
endpoint?: string;
|
||||||
|
}
|
||||||
|
export type CompiledValidityCheck = (input: RequestValidatorInput) => boolean;
|
||||||
|
type CompiledSegmentCheck = (input: IncomingSegment) => boolean;
|
||||||
|
type InputExtractor = (
|
||||||
|
input: RequestValidatorInput,
|
||||||
|
) => string | number | undefined | null;
|
||||||
|
type SegmentExtractor = (input: IncomingSegment) => string | undefined | null;
|
||||||
|
type BooleanRules = "titleOriginal" | "thumbnailOriginal" | "dearrowDownvote";
|
||||||
|
type RuleEntry =
|
||||||
|
| [Exclude<keyof RequestValidatorRule, BooleanRules>, ValidatorPattern]
|
||||||
|
| [BooleanRules, boolean];
|
||||||
|
|
||||||
|
let compiledRules: CompiledValidityCheck;
|
||||||
|
|
||||||
|
function patternToRegex(pattern: ValidatorPattern): RegExp {
|
||||||
|
return typeof pattern === "string"
|
||||||
|
? new RegExp(pattern, "i")
|
||||||
|
: new RegExp(...pattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
function compilePattern(
|
||||||
|
pattern: ValidatorPattern,
|
||||||
|
extractor: InputExtractor,
|
||||||
|
): CompiledValidityCheck {
|
||||||
|
const regex = patternToRegex(pattern);
|
||||||
|
|
||||||
|
return (input: RequestValidatorInput) => {
|
||||||
|
const field = extractor(input);
|
||||||
|
if (field == undefined) return false;
|
||||||
|
return regex.test(String(field));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function compileSegmentPattern(
|
||||||
|
pattern: ValidatorPattern,
|
||||||
|
extractor: SegmentExtractor,
|
||||||
|
): CompiledSegmentCheck {
|
||||||
|
const regex = patternToRegex(pattern);
|
||||||
|
|
||||||
|
return (input: IncomingSegment) => {
|
||||||
|
const field = extractor(input);
|
||||||
|
if (field == undefined) return false;
|
||||||
|
return regex.test(field);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function compileRules(
|
||||||
|
ruleDefinitions: RequestValidatorRule[],
|
||||||
|
): CompiledValidityCheck {
|
||||||
|
if (ruleDefinitions.length === 0) return () => false;
|
||||||
|
|
||||||
|
const rules: CompiledValidityCheck[] = [];
|
||||||
|
for (const ruleDefinition of ruleDefinitions) {
|
||||||
|
const ruleComponents: CompiledValidityCheck[] = [];
|
||||||
|
const segmentRuleComponents: CompiledSegmentCheck[] = [];
|
||||||
|
for (const [ruleKey, rulePattern] of Object.entries(
|
||||||
|
ruleDefinition,
|
||||||
|
) as RuleEntry[]) {
|
||||||
|
switch (ruleKey) {
|
||||||
|
case "userAgent":
|
||||||
|
ruleComponents.push(
|
||||||
|
compilePattern(rulePattern, (input) => input.userAgent),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "userAgentHeader":
|
||||||
|
ruleComponents.push(
|
||||||
|
compilePattern(
|
||||||
|
rulePattern,
|
||||||
|
(input) => input.userAgentHeader,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "videoDuration":
|
||||||
|
ruleComponents.push(
|
||||||
|
compilePattern(
|
||||||
|
rulePattern,
|
||||||
|
(input) => input.videoDuration,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "videoID":
|
||||||
|
ruleComponents.push(
|
||||||
|
compilePattern(rulePattern, (input) => input.videoID),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "userID":
|
||||||
|
ruleComponents.push(
|
||||||
|
compilePattern(rulePattern, (input) => input.userID),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "service":
|
||||||
|
ruleComponents.push(
|
||||||
|
compilePattern(rulePattern, (input) => input.service),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "startTime":
|
||||||
|
segmentRuleComponents.push(
|
||||||
|
compileSegmentPattern(
|
||||||
|
rulePattern,
|
||||||
|
(input) => input.segment[0],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "endTime":
|
||||||
|
segmentRuleComponents.push(
|
||||||
|
compileSegmentPattern(
|
||||||
|
rulePattern,
|
||||||
|
(input) => input.segment[1],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "category":
|
||||||
|
segmentRuleComponents.push(
|
||||||
|
compileSegmentPattern(
|
||||||
|
rulePattern,
|
||||||
|
(input) => input.category,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "actionType":
|
||||||
|
segmentRuleComponents.push(
|
||||||
|
compileSegmentPattern(
|
||||||
|
rulePattern,
|
||||||
|
(input) => input.actionType,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "description":
|
||||||
|
segmentRuleComponents.push(
|
||||||
|
compileSegmentPattern(
|
||||||
|
rulePattern,
|
||||||
|
(input) => input.description,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "title":
|
||||||
|
ruleComponents.push(
|
||||||
|
compilePattern(
|
||||||
|
rulePattern,
|
||||||
|
(input) => input.dearrow?.title?.title,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "titleOriginal":
|
||||||
|
ruleComponents.push(
|
||||||
|
(input) =>
|
||||||
|
input.dearrow?.title?.original === rulePattern,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "thumbnailTimestamp":
|
||||||
|
ruleComponents.push(
|
||||||
|
compilePattern(
|
||||||
|
rulePattern,
|
||||||
|
(input) => input.dearrow?.thumbnail?.timestamp,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "thumbnailOriginal":
|
||||||
|
ruleComponents.push(
|
||||||
|
(input) =>
|
||||||
|
input.dearrow?.thumbnail?.original === rulePattern,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "dearrowDownvote":
|
||||||
|
ruleComponents.push(
|
||||||
|
(input) => input.dearrow?.downvote === rulePattern,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "newUsername":
|
||||||
|
ruleComponents.push(
|
||||||
|
compilePattern(
|
||||||
|
rulePattern,
|
||||||
|
(input) => input.newUsername,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "endpoint":
|
||||||
|
ruleComponents.push(
|
||||||
|
compilePattern(rulePattern, (input) => input.endpoint),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case "casualCategory": {
|
||||||
|
const regex = patternToRegex(rulePattern);
|
||||||
|
ruleComponents.push((input) => {
|
||||||
|
if (input.casualCategories === undefined) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (const category of input.casualCategories) {
|
||||||
|
if (regex.test(category)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
const _exhaustive: never = ruleKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (segmentRuleComponents.length > 0) {
|
||||||
|
ruleComponents.push((input) => {
|
||||||
|
if (input.segments === undefined) return false;
|
||||||
|
for (const segment of input.segments) {
|
||||||
|
let result = true;
|
||||||
|
for (const rule of segmentRuleComponents) {
|
||||||
|
if (!rule(segment)) {
|
||||||
|
result = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (result) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
rules.push((input) => {
|
||||||
|
for (const rule of ruleComponents) {
|
||||||
|
if (!rule(input)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return (input) => {
|
||||||
|
for (const rule of rules) {
|
||||||
|
if (rule(input)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isRequestInvalid(input: RequestValidatorInput) {
|
||||||
|
compiledRules ??= compileRules(config.requestValidatorRules);
|
||||||
|
return compiledRules(input);
|
||||||
|
}
|
||||||
735
test/cases/requestValidator.ts
Normal file
735
test/cases/requestValidator.ts
Normal file
@@ -0,0 +1,735 @@
|
|||||||
|
import assert from "assert";
|
||||||
|
import { RequestValidatorRule } from "../../src/types/config.model";
|
||||||
|
import { ActionType, Category } from "../../src/types/segments.model";
|
||||||
|
import {
|
||||||
|
CompiledValidityCheck,
|
||||||
|
compileRules,
|
||||||
|
} from "../../src/utils/requestValidator";
|
||||||
|
|
||||||
|
describe("Request validator", () => {
|
||||||
|
describe("single simple rule", () => {
|
||||||
|
const ruleset: RequestValidatorRule[] = [
|
||||||
|
{
|
||||||
|
// rules are case insensitive by default
|
||||||
|
userID: "^[a-z]+$",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let compiledRuleset: CompiledValidityCheck;
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
compiledRuleset = compileRules(ruleset);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("simple expected match", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
userID: "asdfg",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("case insensitive match", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
userID: "asDfg",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("simple expected no match", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userID: "125aaa",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing field - no match", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userAgent: "Mozilla/5.0",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("single case sensitive rule", () => {
|
||||||
|
const ruleset: RequestValidatorRule[] = [
|
||||||
|
{
|
||||||
|
// tuple patterns allow setting regex flags
|
||||||
|
userID: ["^[a-z]+$", ""],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let compiledRuleset: CompiledValidityCheck;
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
compiledRuleset = compileRules(ruleset);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("simple expected match", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
userID: "asdfg",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("different casing", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userID: "asDfg",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("extra field match", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
userID: "asdfg",
|
||||||
|
userAgent: "Mozilla/5.0",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("simple expected no match", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userID: "125aaa",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing field - no match", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userAgent: "Mozilla/5.0",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("2-pattern rule", () => {
|
||||||
|
const ruleset: RequestValidatorRule[] = [
|
||||||
|
{
|
||||||
|
userID: ["^[a-z]+$", ""],
|
||||||
|
userAgent: "^Mozilla/5\\.0",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let compiledRuleset: CompiledValidityCheck;
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
compiledRuleset = compileRules(ruleset);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("simple expected match", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
userID: "asdfg",
|
||||||
|
userAgent: "Mozilla/5.0 Chromeium/213.7",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("only matching one pattern - fail #1", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userID: "asDfg",
|
||||||
|
userAgent: "Mozilla/5.0 Chromeium/213.7",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("only matching one pattern - fail #2", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userID: "asdfg",
|
||||||
|
userAgent: "ReVanced/20.07.39",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing one of the fields - fail #1", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userID: "asdfg",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing one of the fields - fail #2", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userAgent: "Mozilla/5.0 Chromeium/213.7",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing all fields - fail", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
videoDuration: 21.37,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("1-pattern segment rule", () => {
|
||||||
|
const ruleset: RequestValidatorRule[] = [
|
||||||
|
{
|
||||||
|
description: "mini_bomba",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let compiledRuleset: CompiledValidityCheck;
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
compiledRuleset = compileRules(ruleset);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("simple expected match", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "mini_bomba",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("match on one of multiple segments", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "mini_bomba",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "aaaaa",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("match on one of multiple segments with other missing field", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "mini_bomba",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "sponsor" as Category,
|
||||||
|
actionType: "skip" as ActionType,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("no match with one segment", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "aaaa",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("no match with multiple segments", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "aaaa",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "bbbbb",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("one segment missing field", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "sponsor" as Category,
|
||||||
|
actionType: "skip" as ActionType,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("multiple segments missing field", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "sponsor" as Category,
|
||||||
|
actionType: "skip" as ActionType,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "filler" as Category,
|
||||||
|
actionType: "skip" as ActionType,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("zero segments", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing segments", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userAgent: "Mozilla/5.0",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("2-pattern segment rule", () => {
|
||||||
|
const ruleset: RequestValidatorRule[] = [
|
||||||
|
{
|
||||||
|
description: "mini_bomba",
|
||||||
|
startTime: "\\.\\d",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let compiledRuleset: CompiledValidityCheck;
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
compiledRuleset = compileRules(ruleset);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("simple expected match", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1.1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "mini_bomba",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("match on one of multiple segments", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1.1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "mini_bomba",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "aaaaa",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("match on one of multiple segments with other missing field", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1.1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "mini_bomba",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "sponsor" as Category,
|
||||||
|
actionType: "skip" as ActionType,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("no match with one segment #1", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "aaaa",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("no match with one segment #2", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1.1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "aaaa",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("no match with one segment #2", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "mini_bomba",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("no match with multiple segments", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "aaaa",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "bbbbb",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("no match with multiple segments with partial matches", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1.1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "aaaa",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "chapter" as Category,
|
||||||
|
actionType: "chapter" as ActionType,
|
||||||
|
description: "mini_bomba",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("one segment missing field", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "sponsor" as Category,
|
||||||
|
actionType: "skip" as ActionType,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("multiple segments missing field", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "sponsor" as Category,
|
||||||
|
actionType: "skip" as ActionType,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
segment: ["1", "2"],
|
||||||
|
category: "filler" as Category,
|
||||||
|
actionType: "skip" as ActionType,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("zero segments", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
segments: [],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing segments", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userAgent: "Mozilla/5.0",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("boolean rule", () => {
|
||||||
|
const ruleset: RequestValidatorRule[] = [
|
||||||
|
{
|
||||||
|
dearrowDownvote: true,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let compiledRuleset: CompiledValidityCheck;
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
compiledRuleset = compileRules(ruleset);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("simple expected match", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: true,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("simple expected no match", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing field - no match", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userAgent: "Mozilla/5.0",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("mixed type rules", () => {
|
||||||
|
const ruleset: RequestValidatorRule[] = [
|
||||||
|
{
|
||||||
|
titleOriginal: true,
|
||||||
|
title: "mini_bomba",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let compiledRuleset: CompiledValidityCheck;
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
compiledRuleset = compileRules(ruleset);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("simple expected match", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
title: {
|
||||||
|
title: "mini_bomba gaming",
|
||||||
|
original: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("simple expected no match", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
title: {
|
||||||
|
title: "mschae restaurant",
|
||||||
|
original: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("partial match #1", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
title: {
|
||||||
|
title: "mini_bomba gaming",
|
||||||
|
original: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("partial match #2", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
title: {
|
||||||
|
title: "mschae restaurant",
|
||||||
|
original: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing field - no match #1", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userAgent: "Mozilla/5.0",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing field - no match #2", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing field - no match #3", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
thumbnail: {
|
||||||
|
original: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("two-rule ruleset", () => {
|
||||||
|
const ruleset: RequestValidatorRule[] = [
|
||||||
|
{
|
||||||
|
titleOriginal: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "mini_bomba",
|
||||||
|
}
|
||||||
|
];
|
||||||
|
let compiledRuleset: CompiledValidityCheck;
|
||||||
|
|
||||||
|
before(() => {
|
||||||
|
compiledRuleset = compileRules(ruleset);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("matches both", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
title: {
|
||||||
|
title: "mini_bomba gaming",
|
||||||
|
original: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("matches 1", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
title: {
|
||||||
|
title: "mini_bomba gaming",
|
||||||
|
original: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("matches 2", () => {
|
||||||
|
assert.ok(
|
||||||
|
compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
title: {
|
||||||
|
title: "mschae restaurant",
|
||||||
|
original: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("no match", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
title: {
|
||||||
|
title: "mschae restaurant",
|
||||||
|
original: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing both fields #1", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
userAgent: "Mozilla/5.0",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing both fields #2", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("missing both fields #3", () => {
|
||||||
|
assert.ok(
|
||||||
|
!compiledRuleset({
|
||||||
|
dearrow: {
|
||||||
|
downvote: false,
|
||||||
|
thumbnail: {
|
||||||
|
original: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user