mirror of
https://github.com/mileswolfallen2/miles-web.git
synced 2026-12-03 23:44:07 +00:00
- Implement tests for HTTP API endpoints, ensuring correct responses and security headers. - Create configuration tests to validate loading of defaults and environment overrides. - Add tests for FEDL service to verify data handling and snapshot functionality. - Introduce GitHub service tests to check API data normalization and caching behavior. - Develop monitor service tests for network checks and uptime calculations. - Implement persistence tests to ensure database migrations are idempotent and data persists across restarts. - Add vault service tests for session management and brute-force protection mechanisms. - Include helper functions for test context creation and JSON response handling.
64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
const BOT_RE = /bot|crawler|spider|slurp|bingpreview|facebookexternalhit|headless|lighthouse|pagespeed|curl\/|wget\/|python-requests|go-http-client|httpclient|scrapy|semrush|ahrefs|mj12|dotbot|petalbot|yandex|baiduspider|duckduckbot|applebot|archive\.org/i;
|
|
const PATH_RE = /^\/[A-Za-z0-9._~!$&'()*+,;=:@%/-]*$/;
|
|
const HOST_RE = /^(?=.{1,253}$)([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i;
|
|
|
|
export function clampInt(value, fallback, min, max) {
|
|
const n = Number.parseInt(value, 10);
|
|
if (!Number.isFinite(n)) return fallback;
|
|
return Math.min(max, Math.max(min, n));
|
|
}
|
|
|
|
export function limitString(value, maxLength) {
|
|
if (typeof value !== 'string') return '';
|
|
return value.length > maxLength ? value.slice(0, maxLength) : value;
|
|
}
|
|
|
|
/**
|
|
* Normalize a page path for analytics. Returns null for anything that is
|
|
* not a safe, same-site path.
|
|
*/
|
|
export function normalizePath(raw) {
|
|
if (typeof raw !== 'string' || raw.length > 1024) return null;
|
|
let value = raw.trim().split('#')[0].split('?')[0];
|
|
if (!value.startsWith('/')) return null;
|
|
if (value.includes('\\') || value.includes('\0')) return null;
|
|
|
|
let decoded;
|
|
try {
|
|
decoded = decodeURIComponent(value);
|
|
} catch {
|
|
return null;
|
|
}
|
|
if (decoded.includes('..') || decoded.includes('\\') || decoded.includes('\0')) return null;
|
|
if (!PATH_RE.test(decoded)) return null;
|
|
|
|
decoded = decoded.replace(/\/{2,}/g, '/');
|
|
if (decoded === '/index.html') decoded = '/';
|
|
if (decoded.length > 1) decoded = decoded.replace(/\/+$/, '');
|
|
if (!decoded.startsWith('/')) decoded = '/' + decoded;
|
|
return decoded;
|
|
}
|
|
|
|
export function referrerDomain(raw) {
|
|
if (typeof raw !== 'string' || !raw || raw.length > 2048) return null;
|
|
try {
|
|
const url = new URL(raw);
|
|
if (url.protocol !== 'http:' && url.protocol !== 'https:') return null;
|
|
const host = url.hostname.toLowerCase();
|
|
if (!HOST_RE.test(host)) return null;
|
|
return host.slice(0, 255);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function isLikelyBot(userAgent) {
|
|
if (!userAgent) return true;
|
|
return BOT_RE.test(userAgent);
|
|
}
|
|
|
|
export function parseBoolean(value, fallback = false) {
|
|
if (value === undefined || value === null || value === '') return fallback;
|
|
return /^(1|true|yes|on)$/i.test(String(value));
|
|
}
|