Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | 1x 1x 57x 14x 2x 2x 2x 2x 2x 2x 2x 12x 12x 12x 12x 14x 7x 7x 7x 7x 7x 5x 5x 5x 5x 5x 5x 14x 57x 1x 6x 2x 2x 2x 2x 2x 2x 2x 4x 4x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 3x 1x 1x 1x 1x 1x 1x 2x 3x 2x 2x 2x 2x 3x 4x 3x 4x 1x 1x 4x 3x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 1x 5x 5x 5x 5x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 1x 7x 7x 7x 3x 3x 3x 3x 3x 7x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 21x 21x 9x 9x 9x 4x 4x 2x 2x 2x 2x 2x 2x 2x 4x 9x 1x 1x 1x 1x 1x 1x 1x 9x 1x 1x 1x 1x 1x 1x 1x 9x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 9x 21x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 3x 5x 2x 2x 2x 2x 2x 2x 5x 2x 2x 1x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 2x 2x 2x 6x 6x 6x 5x 5x 6x 2x 2x | /**
* URL Parameter Validation Utilities
*
* Provides robust validation for URL parameters to prevent invalid data
* from entering the application state and causing unexpected behavior.
*/
import type { InboxFilterType } from '@common/hooks/useInboxFilters';
import type { TimeRange } from '@web/components/TimeFilter';
import sanitizeHtml from 'sanitize-html';
// Validation result interface
export interface ValidationResult<T = unknown> {
isValid: boolean;
value?: T;
error?: string;
defaultValue?: T;
}
// Base validator type
export type Validator<T> = (value: string | null, defaultValue?: T) => ValidationResult<T>;
/**
* Validates that a value is one of the allowed enum values
*/
export function createEnumValidator<T extends string>(allowedValues: T[]): Validator<T> {
return (value: string | null, defaultValue?: T): ValidationResult<T> => {
if (value === null || value === undefined || value === '') {
return {
isValid: defaultValue !== undefined,
value: defaultValue,
defaultValue,
error: defaultValue === undefined ? 'Value is required but no default provided' : undefined,
};
}
const normalizedValue = value.trim().toLowerCase();
const validValue = allowedValues.find(
allowed => allowed.toLowerCase() === normalizedValue
) as T | undefined;
if (validValue !== undefined) {
return {
isValid: true,
value: validValue,
};
}
return {
isValid: false,
value: defaultValue,
defaultValue,
error: `Invalid value "${value}". Must be one of: ${allowedValues.join(', ')}`,
};
};
}
/**
* Validates UUID strings
*/
export const uuidValidator: Validator<string> = (value: string | null, defaultValue?: string): ValidationResult<string> => {
if (value === null || value === undefined || value === '') {
return {
isValid: defaultValue !== undefined,
value: defaultValue,
defaultValue,
error: defaultValue === undefined ? 'UUID is required but no default provided' : undefined,
};
}
const trimmedValue = value.trim();
// Basic UUID regex pattern (supports both v4 and other UUID formats)
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
if (uuidRegex.test(trimmedValue)) {
return {
isValid: true,
value: trimmedValue,
};
}
return {
isValid: false,
value: defaultValue,
defaultValue,
error: `Invalid UUID format: "${value}"`,
};
};
/**
* Validates arrays of UUID strings
*/
export const uuidArrayValidator: Validator<string[]> = (value: string | null, defaultValue: string[] = []): ValidationResult<string[]> => {
if (value === null || value === undefined || value === '') {
return {
isValid: true,
value: defaultValue,
defaultValue,
};
}
const trimmedValue = value.trim();
if (trimmedValue === '') {
return {
isValid: true,
value: defaultValue,
defaultValue,
};
}
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
const parts = trimmedValue.split(',').map(part => part.trim()).filter(Boolean);
const validUuids: string[] = [];
const invalidUuids: string[] = [];
for (const part of parts) {
if (uuidRegex.test(part)) {
validUuids.push(part);
} else {
invalidUuids.push(part);
}
}
if (invalidUuids.length === 0) {
return {
isValid: true,
value: validUuids,
};
}
return {
isValid: false,
value: validUuids.length > 0 ? validUuids : defaultValue,
defaultValue,
error: `Invalid UUIDs found: ${invalidUuids.join(', ')}`,
};
};
/**
* Validates positive integers
*/
export const positiveIntegerValidator: Validator<number> = (value: string | null, defaultValue: number = 1): ValidationResult<number> => {
if (value === null || value === undefined || value === '') {
return {
isValid: true,
value: defaultValue,
defaultValue,
};
}
const trimmedValue = value.trim();
const num = parseInt(trimmedValue, 10);
if (!isNaN(num) && num > 0 && Number.isInteger(num)) {
return {
isValid: true,
value: num,
};
}
return {
isValid: false,
value: defaultValue,
defaultValue,
error: `Invalid positive integer: "${value}"`,
};
};
/**
* Validates boolean values
*/
export const booleanValidator: Validator<boolean> = (value: string | null, defaultValue: boolean = false): ValidationResult<boolean> => {
if (value === null || value === undefined || value === '') {
return {
isValid: true,
value: defaultValue,
defaultValue,
};
}
const trimmedValue = value.trim().toLowerCase();
if (trimmedValue === 'true' || trimmedValue === '1' || trimmedValue === 'yes') {
return {
isValid: true,
value: true,
};
}
if (trimmedValue === 'false' || trimmedValue === '0' || trimmedValue === 'no') {
return {
isValid: true,
value: false,
};
}
return {
isValid: false,
value: defaultValue,
defaultValue,
error: `Invalid boolean value: "${value}". Use true/false, 1/0, or yes/no`,
};
};
/**
* Validates string values with optional length constraints
*/
export function createStringValidator(options: {
minLength?: number;
maxLength?: number;
pattern?: RegExp;
allowedValues?: string[];
} = {}): Validator<string> {
return (value: string | null, defaultValue?: string): ValidationResult<string> => {
if (value === null || value === undefined || value === '') {
return {
isValid: defaultValue !== undefined,
value: defaultValue,
defaultValue,
error: defaultValue === undefined ? 'Value is required but no default provided' : undefined,
};
}
const trimmedValue = value.trim();
// Check allowed values if specified
if (options.allowedValues) {
const isValid = options.allowedValues.some(allowed => allowed === trimmedValue);
if (!isValid) {
return {
isValid: false,
value: defaultValue,
defaultValue,
error: `Invalid value "${trimmedValue}". Must be one of: ${options.allowedValues.join(', ')}`,
};
}
}
// Check length constraints
if (options.minLength !== undefined && trimmedValue.length < options.minLength) {
return {
isValid: false,
value: defaultValue,
defaultValue,
error: `Value too short. Minimum length: ${options.minLength}`,
};
}
if (options.maxLength !== undefined && trimmedValue.length > options.maxLength) {
return {
isValid: false,
value: defaultValue,
defaultValue,
error: `Value too long. Maximum length: ${options.maxLength}`,
};
}
// Check pattern if specified
if (options.pattern && !options.pattern.test(trimmedValue)) {
return {
isValid: false,
value: defaultValue,
defaultValue,
error: `Value does not match required pattern`,
};
}
return {
isValid: true,
value: trimmedValue,
};
};
}
// Predefined validators for common use cases
export const inboxFilterValidator = createEnumValidator<InboxFilterType>(['unread', 'read', 'liked', 'archived']);
export const timeRangeValidator = createEnumValidator<TimeRange>(['day', 'last24h', '2days', 'week', 'month', 'all']);
export const orderDirectionValidator = createEnumValidator(['asc', 'desc']);
// Common sort fields for newsletters
export const sortFieldValidator = createStringValidator({
allowedValues: ['created_at', 'updated_at', 'title', 'published_at', 'read_at', 'name', 'received_at', 'estimated_read_time'],
});
/**
* Validates an object of parameters against a validation schema
*/
export interface ValidationSchema {
[key: string]: {
validator: Validator<unknown>;
required?: boolean;
defaultValue?: unknown;
};
}
export function validateParams(
params: Record<string, string | null>,
schema: ValidationSchema
): { isValid: boolean; values: Record<string, unknown>; errors: Record<string, string> } {
const values: Record<string, unknown> = {};
const errors: Record<string, string> = {};
let isValid = true;
for (const [key, config] of Object.entries(schema)) {
const paramValue = params[key];
const result = config.validator(paramValue, config.defaultValue as any);
if (result.isValid && result.value !== undefined) {
values[key] = result.value;
} else {
if (config.required || (paramValue !== null && paramValue !== undefined && paramValue !== '')) {
errors[key] = result.error || `Invalid value for ${key}`;
isValid = false;
}
if (result.defaultValue !== undefined) {
values[key] = result.defaultValue;
}
}
}
return { isValid, values, errors };
}
/**
* Sanitizes URL parameters by removing potentially dangerous characters
*/
export function sanitizeUrlParam(value: string): string {
// Normalize whitespace first
value = value.trim();
// Use a robust HTML sanitizer to strip any HTML/script content.
// We don't allow any HTML; treat everything as plain text.
let sanitized = sanitizeHtml(value, {
allowedTags: [],
allowedAttributes: {},
});
// As a defensive fallback, strip any remaining angle brackets and
// obvious dangerous protocol / script patterns that could survive
// unexpected encodings or malformed input.
sanitized = sanitized
.replace(/</g, '')
.replace(/>/g, '')
.replace(/javascript:/gi, '')
.replace(/vbscript:/gi, '')
.replace(/data:/gi, '')
.replace(/script/gi, '');
// Limit length to prevent abuse
return sanitized.slice(0, 1000);
}
/**
* Validates and sanitizes a complete URL search query
*/
export function sanitizeUrlQuery(query: string): string {
const params = new URLSearchParams(query);
const sanitizedParams = new URLSearchParams();
for (const [key, value] of params.entries()) {
const sanitizedKey = sanitizeUrlParam(key);
const sanitizedValue = sanitizeUrlParam(value);
if (sanitizedKey && sanitizedValue) {
sanitizedParams.set(sanitizedKey, sanitizedValue);
}
}
return sanitizedParams.toString();
} |