~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
/**
* URL Validation Utilities
* Provides secure URL validation to prevent XSS attacks
*/
const URLValidator = {
UNSAFE_SCHEMES: ['javascript', 'data', 'vbscript', 'about', 'blob', 'file'],
SAFE_SCHEMES: ['http', 'https', 'ftp', 'ftps'],
EMAIL_SCHEME: 'mailto',
// Raster MIME types a Markdown image may use as a `data:` source. Read
// only by services/ui.js's isSafeMarkdownImageSrc. safeAssign's data:
// carve-out below does NOT read this list; see the note there.
SAFE_DATA_MIME_TYPES: [
'image/png',
'image/jpeg',
'image/jpg',
'image/gif',
'image/webp',
'image/bmp'
],
/**
* Strip what a browser could ignore in a URL before scheme/MIME
* detection: leading/trailing whitespace, then every C0 control
* character (and space, and DEL) anywhere in the string. Browsers strip
* only leading/trailing C0 controls and spaces plus every tab, LF and
* CR, so this removes a superset of what they ignore, and a check run
* on the result cannot be dodged by a character the browser would
* discard (e.g. a tab between "data:" and the MIME type).
*
* Its only caller is services/ui.js's isSafeMarkdownImageSrc.
* safeAssign does not use it: its data: carve-out matches a raw-string
* regex against `url.trim()`, so a control character inside the
* `data:<mime>;base64,` prefix makes that match fail and the value is
* blocked rather than normalized.
* @param {string} url - The value to normalize.
* @returns {string} - The normalized value, or '' for non-strings.
*/
normalizeUrlForSchemeCheck(url) {
if (typeof url !== 'string') {
return '';
}
// eslint-disable-next-line no-control-regex -- intentional: browsers ignore control characters inside URLs, so they must be stripped before scheme/MIME detection
return url.trim().replace(/[\x00-\x20\x7F]/g, '');
},
isUnsafeScheme(url) {
if (!url) return false;
const normalizedUrl = url.trim().toLowerCase();
for (const scheme of this.UNSAFE_SCHEMES) {
if (normalizedUrl.startsWith(scheme + ':')) {
SafeLogger.warn(`Unsafe URL scheme detected: ${scheme}`);
return true;
}
}
return false;
},
isSafeUrl(url, options = {}) {
const {
requireScheme = true,
allowFragments = true,
allowMailto = false,
trustedDomains = []
} = options;
if (!url || typeof url !== 'string') {
return false;
}
// Check for unsafe schemes first
if (this.isUnsafeScheme(url)) {
return false;
}
// Handle fragment-only URLs
if (url.startsWith('#')) {
return allowFragments;
}
// Parse the URL
try {
const parsed = new URL(url, window.location.href);
const scheme = parsed.protocol.slice(0, -1).toLowerCase(); // Remove trailing ':'
// Check if it's a mailto link
if (scheme === this.EMAIL_SCHEME) {
return allowMailto;
}
// Check if it's a safe scheme
if (!this.SAFE_SCHEMES.includes(scheme)) {
SafeLogger.warn(`Unsafe URL scheme: ${scheme}`);
return false;
}
// Validate domain if trusted domains are specified
if (trustedDomains.length > 0 && parsed.hostname) {
const hostname = parsed.hostname.toLowerCase();
const isTrusted = trustedDomains.some(domain =>
hostname === domain.toLowerCase() ||
hostname.endsWith('.' + domain.toLowerCase())
);
if (!isTrusted) {
SafeLogger.warn(`URL domain not in trusted list: ${parsed.hostname}`);
return false;
}
}
return true;
} catch (e) {
SafeLogger.warn(`Failed to parse URL: ${e.message}`);
return false;
}
},
sanitizeUrl(url, defaultScheme = 'https') {
if (!url) return null;
// Check for unsafe schemes
if (this.isUnsafeScheme(url)) {
return null;
}
// Strip whitespace
url = url.trim();
// Add scheme if missing
if (!url.match(/^[a-z][a-z\d+\-.]*:/i)) {
url = `${defaultScheme}://${url}`;
}
// Validate the final URL
if (this.isSafeUrl(url, { requireScheme: true })) {
return url;
}
return null;
},
/**
* Safe URL assignment with validation
* Use this for any dynamic URL assignments
*/
safeAssign(element, property, url, options = {}) {
if (typeof url !== 'string') {
SafeLogger.warn('Blocked non-string URL assignment');
return false;
}
const normalizedUrl = url.trim();
if (!normalizedUrl) {
SafeLogger.warn('Blocked empty URL assignment');
return false;
}
// Special handling for fragment navigation, while preserving callers'
// explicit fragment policy.
if (normalizedUrl.startsWith('#')) {
if (options.allowFragments === false) {
SafeLogger.warn('Blocked fragment URL assignment');
return false;
}
element[property] = normalizedUrl;
return true;
}
// Anything without a literal scheme is intended to stay on this
// origin. Let the browser parser perform its C0-control and backslash
// normalization before enforcing that boundary. Checking every
// scheme-less value (rather than only strings beginning with '/')
// also catches inputs such as NUL + '//evil.test' and 'h<TAB>ttp:'.
const hasExplicitScheme = /^[a-z][a-z\d+.-]*:/i.test(normalizedUrl);
if (!hasExplicitScheme) {
try {
const parsed = new URL(normalizedUrl, window.location.href);
if (parsed.origin === window.location.origin) {
element[property] = normalizedUrl;
return true;
}
} catch (_error) {
// Fall through to the shared blocked-assignment warning.
}
SafeLogger.warn('Blocked ambiguous cross-origin URL assignment');
return false;
}
// Blob URLs are created by the current page for owned downloads. Keep
// them on anchor hrefs; they must never become a page-navigation sink.
if (normalizedUrl.startsWith('blob:')) {
const tagName = String(element?.tagName || '').toUpperCase();
if (property === 'href' && tagName === 'A') {
element[property] = normalizedUrl;
return true;
}
SafeLogger.warn('Blocked blob URL assignment outside a download link');
return false;
}
// Data URLs are unsafe for navigation (`data:text/html,...` can load
// attacker-controlled active content). The only production use is a
// raster image assigned to an <img> or favicon <link>, so keep that
// narrow target/MIME contract instead of bypassing isUnsafeScheme for
// every data URL.
//
// This is a separate policy from the Markdown image check
// (services/ui.js isSafeMarkdownImageSrc), and the two differ: this
// regex also admits avif and x-icon, which SAFE_DATA_MIME_TYPES
// excludes; it requires `;base64,`, which the Markdown check does
// not; and it runs on `url.trim()` instead of
// normalizeUrlForSchemeCheck(). Each difference only makes one side
// stricter than the other, and neither side admits SVG or any
// non-image type. tests/js/security/markdown-render-xss.test.js pins
// the MIME and base64 differences.
if (/^data:/i.test(normalizedUrl)) {
const tagName = String(element?.tagName || '').toUpperCase();
const isImageTarget = property === 'src' && tagName === 'IMG';
const isFaviconTarget = property === 'href' && tagName === 'LINK' &&
String(element.rel || '').toLowerCase().split(/\s+/).includes('icon');
const isRasterImage = /^data:image\/(?:png|jpe?g|gif|webp|bmp|avif|x-icon);base64,/i
.test(normalizedUrl);
if (isRasterImage && (isImageTarget || isFaviconTarget)) {
element[property] = normalizedUrl;
return true;
}
SafeLogger.warn('Blocked unsafe data URL assignment');
return false;
}
// Validate external URLs
if (this.isSafeUrl(url, options)) {
element[property] = url;
return true;
}
SafeLogger.warn('Blocked unsafe URL assignment:', url);
return false;
}
};
// Export for use in other files
if (typeof module !== 'undefined' && module.exports) {
module.exports = URLValidator;
}
// Make URLValidator available globally for browser usage
if (typeof window !== 'undefined') {
window.URLValidator = URLValidator;
}