mirror of
https://github.com/lissy93/networking-toolbox.git
synced 2026-11-27 16:09:04 +03:00
Limited to known DNS servers (#22)
This commit is contained in:
1 parent
8917acd019
commit
0be304b9f1
4 files changed
+304
-29
No files matched your search
@@ -13,6 +13,7 @@ import { browser } from '$app/environment';
|
||||
import type { HomepageLayoutMode } from '$lib/stores/homepageLayout';
|
||||
import type { NavbarDisplayMode } from '$lib/stores/navbarDisplay';
|
||||
import type { ThemeOption } from '$lib/stores/theme';
|
||||
import { DEFAULT_TRUSTED_DNS_SERVERS } from '$lib/utils/ip-security';
|
||||
|
||||
// Get user customization from localStorage (browser only)
|
||||
function getUserCustomization() {
|
||||
@@ -73,3 +74,31 @@ export const PRIMARY_COLOR = env.NTB_PRIMARY_COLOR ?? '';
|
||||
export const DEFAULT_FONT_SCALE = parseInt(env.NTB_FONT_SCALE ?? '2', 10);
|
||||
|
||||
export const SHOW_TIPS_ON_HOMEPAGE = !!env.NTB_SHOW_TIPS_ON_HOMEPAGE;
|
||||
|
||||
/**
|
||||
* DNS Security Settings
|
||||
* These settings protect against SSRF attacks by validating custom DNS server IPs
|
||||
*/
|
||||
|
||||
/**
|
||||
* Allow custom DNS servers
|
||||
* When false, only servers from ALLOWED_DNS_SERVERS can be used
|
||||
* Default: false (for security)
|
||||
*/
|
||||
export const ALLOW_CUSTOM_DNS_SERVERS = env.NTB_ALLOW_CUSTOM_DNS === 'true';
|
||||
|
||||
/**
|
||||
* Block private IP addresses for DNS servers
|
||||
* When true, prevents using private/internal IPs as DNS resolvers
|
||||
* Default: true (for security - prevents SSRF attacks)
|
||||
*/
|
||||
export const BLOCK_PRIVATE_DNS_IPS = env.NTB_BLOCK_PRIVATE_DNS_IPS !== 'false'; // Default true
|
||||
|
||||
/**
|
||||
* Allowed DNS servers (comma-separated list of IPs)
|
||||
* When ALLOW_CUSTOM_DNS_SERVERS is false, only these IPs can be used
|
||||
* Default: Comprehensive list of trusted public DNS providers
|
||||
*/
|
||||
export const ALLOWED_DNS_SERVERS = env.NTB_ALLOWED_DNS_SERVERS
|
||||
? env.NTB_ALLOWED_DNS_SERVERS.split(',').map((ip) => ip.trim())
|
||||
: DEFAULT_TRUSTED_DNS_SERVERS;
|
||||
@@ -0,0 +1,199 @@
|
||||
/**
|
||||
* IP Security Utilities
|
||||
* Provides validation for user-supplied IP addresses to prevent SSRF attacks
|
||||
*/
|
||||
|
||||
import { validateIP } from './ip-validation';
|
||||
|
||||
/**
|
||||
* Check if a string is a valid IPv4 address
|
||||
*/
|
||||
function isIPv4(ip: string): boolean {
|
||||
const result = validateIP(ip);
|
||||
return result !== null && result.isValid && result.type === 'ipv4';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a string is a valid IPv6 address
|
||||
*/
|
||||
function isIPv6(ip: string): boolean {
|
||||
const result = validateIP(ip);
|
||||
return result !== null && result.isValid && result.type === 'ipv6';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an IPv4 address is in a private range
|
||||
* Private ranges: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
|
||||
* Loopback: 127.0.0.0/8
|
||||
* Link-local: 169.254.0.0/16
|
||||
*/
|
||||
function isPrivateIPv4(ip: string): boolean {
|
||||
const parts = ip.split('.').map(Number);
|
||||
if (parts.length !== 4) return false;
|
||||
|
||||
const [a, b] = parts;
|
||||
|
||||
// Loopback: 127.0.0.0/8
|
||||
if (a === 127) return true;
|
||||
|
||||
// Private Class A: 10.0.0.0/8
|
||||
if (a === 10) return true;
|
||||
|
||||
// Private Class B: 172.16.0.0/12
|
||||
if (a === 172 && b >= 16 && b <= 31) return true;
|
||||
|
||||
// Private Class C: 192.168.0.0/16
|
||||
if (a === 192 && b === 168) return true;
|
||||
|
||||
// Link-local: 169.254.0.0/16 (AWS metadata, APIPA)
|
||||
if (a === 169 && b === 254) return true;
|
||||
|
||||
// Broadcast: 255.255.255.255
|
||||
if (a === 255 && b === 255) return true;
|
||||
|
||||
// 0.0.0.0/8
|
||||
if (a === 0) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an IPv6 address is in a private/reserved range
|
||||
*/
|
||||
function isPrivateIPv6(ip: string): boolean {
|
||||
const lower = ip.toLowerCase();
|
||||
|
||||
// Loopback: ::1
|
||||
if (lower === '::1' || lower === '0:0:0:0:0:0:0:1') return true;
|
||||
|
||||
// Link-local: fe80::/10
|
||||
if (lower.startsWith('fe80:')) return true;
|
||||
|
||||
// Unique local: fc00::/7
|
||||
if (lower.startsWith('fc') || lower.startsWith('fd')) return true;
|
||||
|
||||
// Multicast: ff00::/8
|
||||
if (lower.startsWith('ff')) return true;
|
||||
|
||||
// Unspecified: ::
|
||||
if (lower === '::' || lower === '0:0:0:0:0:0:0:0') return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an IP address is in a private or reserved range
|
||||
*/
|
||||
export function isPrivateIP(ip: string): boolean {
|
||||
if (isIPv4(ip)) {
|
||||
return isPrivateIPv4(ip);
|
||||
}
|
||||
if (isIPv6(ip)) {
|
||||
return isPrivateIPv6(ip);
|
||||
}
|
||||
return true; // If not a valid IP, treat as private for safety
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a DNS server IP address for security
|
||||
* @param ip - The IP address to validate
|
||||
* @param allowedServers - List of explicitly allowed server IPs
|
||||
* @param blockPrivate - Whether to block private IP ranges
|
||||
* @returns Object with valid boolean and optional error message
|
||||
*/
|
||||
export function validateDNSServer(
|
||||
ip: string,
|
||||
allowedServers: string[],
|
||||
blockPrivate: boolean = true,
|
||||
): { valid: boolean; error?: string } {
|
||||
// Trim whitespace
|
||||
const cleanIP = ip.trim();
|
||||
|
||||
// Check if it's a valid IP address
|
||||
if (!isIPv4(cleanIP) && !isIPv6(cleanIP)) {
|
||||
return {
|
||||
valid: false,
|
||||
error: 'Invalid IP address format. Must be a valid IPv4 or IPv6 address.',
|
||||
};
|
||||
}
|
||||
|
||||
// Check allowlist first (allowlist takes precedence)
|
||||
if (allowedServers.length > 0 && allowedServers.includes(cleanIP)) {
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
// Check for private IPs if blocking is enabled
|
||||
if (blockPrivate && isPrivateIP(cleanIP)) {
|
||||
return {
|
||||
valid: false,
|
||||
error: 'Private IP addresses are not allowed for security reasons. Use a public DNS server.',
|
||||
};
|
||||
}
|
||||
|
||||
// If we have an allowlist and IP is not in it
|
||||
if (allowedServers.length > 0) {
|
||||
return {
|
||||
valid: false,
|
||||
error: 'DNS server not in allowed list. Please use one of the permitted DNS servers.',
|
||||
};
|
||||
}
|
||||
|
||||
// If no allowlist and not private (or private IPs are allowed), accept it
|
||||
return { valid: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Default list of trusted public DNS servers
|
||||
* These are safe, well-known, public DNS resolvers
|
||||
*/
|
||||
export const DEFAULT_TRUSTED_DNS_SERVERS = [
|
||||
// Cloudflare
|
||||
'1.1.1.1',
|
||||
'1.0.0.1',
|
||||
'2606:4700:4700::1111',
|
||||
'2606:4700:4700::1001',
|
||||
|
||||
// Google
|
||||
'8.8.8.8',
|
||||
'8.8.4.4',
|
||||
'2001:4860:4860::8888',
|
||||
'2001:4860:4860::8844',
|
||||
|
||||
// Quad9
|
||||
'9.9.9.9',
|
||||
'149.112.112.112',
|
||||
'2620:fe::fe',
|
||||
'2620:fe::9',
|
||||
|
||||
// OpenDNS
|
||||
'208.67.222.222',
|
||||
'208.67.220.220',
|
||||
'2620:119:35::35',
|
||||
'2620:119:53::53',
|
||||
|
||||
// Comodo Secure DNS
|
||||
'8.26.56.26',
|
||||
'8.20.247.20',
|
||||
|
||||
// DNS.WATCH
|
||||
'84.200.69.80',
|
||||
'84.200.70.40',
|
||||
'2001:1608:10:25::1c04:b12f',
|
||||
'2001:1608:10:25::9249:d69b',
|
||||
|
||||
// Verisign
|
||||
'64.6.64.6',
|
||||
'64.6.65.6',
|
||||
'2620:74:1b::1:1',
|
||||
'2620:74:1c::2:2',
|
||||
|
||||
// Level3
|
||||
'209.244.0.3',
|
||||
'209.244.0.4',
|
||||
|
||||
// AdGuard DNS
|
||||
'94.140.14.14',
|
||||
'94.140.15.15',
|
||||
'2a10:50c0::ad1:ff',
|
||||
'2a10:50c0::ad2:ff',
|
||||
];
|
||||
@@ -3,6 +3,12 @@ import { promises as dns } from 'node:dns';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { errorManager } from '$lib/utils/error-manager';
|
||||
import { logger } from '$lib/utils/logger';
|
||||
import { validateDNSServer } from '$lib/utils/ip-security';
|
||||
import {
|
||||
ALLOW_CUSTOM_DNS_SERVERS,
|
||||
ALLOWED_DNS_SERVERS,
|
||||
BLOCK_PRIVATE_DNS_IPS,
|
||||
} from '$lib/config/customizable-settings';
|
||||
|
||||
type Action =
|
||||
| 'lookup'
|
||||
@@ -71,6 +77,29 @@ function getDNSServerForResolver(resolver: string): string {
|
||||
return (DNS_SERVERS as Record<string, string>)[resolver] || DNS_SERVERS.cloudflare;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a custom DNS server IP for security
|
||||
* @throws Error if validation fails
|
||||
*/
|
||||
function validateCustomDNSServer(serverIP: string): void {
|
||||
// If custom DNS servers are allowed without restriction, skip validation
|
||||
if (ALLOW_CUSTOM_DNS_SERVERS && !BLOCK_PRIVATE_DNS_IPS) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate the DNS server IP
|
||||
const validation = validateDNSServer(serverIP, ALLOWED_DNS_SERVERS, BLOCK_PRIVATE_DNS_IPS);
|
||||
|
||||
if (!validation.valid) {
|
||||
throw error(
|
||||
403,
|
||||
validation.error ||
|
||||
'Custom DNS server not allowed. For security reasons, only trusted public DNS servers are permitted. ' +
|
||||
'You can edit this with the ALLOW_CUSTOM_DNS_SERVERS and ALLOWED_DNS_SERVERS environmental variables.',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
interface LookupReq extends BaseReq {
|
||||
action: 'lookup';
|
||||
name: string;
|
||||
@@ -233,6 +262,8 @@ async function performNativeDNSLookup(
|
||||
|
||||
try {
|
||||
if (customServer) {
|
||||
// Validate custom DNS server for security (prevent SSRF attacks)
|
||||
validateCustomDNSServer(customServer);
|
||||
dns.setServers([customServer]);
|
||||
}
|
||||
|
||||
@@ -364,6 +395,11 @@ async function performNativeDNSLookup(
|
||||
} catch (err: unknown) {
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
// If this is a SvelteKit HttpError (e.g., from validation), rethrow it without wrapping
|
||||
if (err && typeof err === 'object' && 'status' in err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
// Provide better error messages
|
||||
if ((err as Error).message === 'DNS timeout') {
|
||||
throw new Error(`DNS query timed out after ${timeoutMs}ms`);
|
||||
|
||||
@@ -50,6 +50,8 @@
|
||||
{ domain: 'google.com', type: 'MX', description: 'Mail server records' },
|
||||
{ domain: 'cloudflare.com', type: 'AAAA', description: 'IPv6 addresses' },
|
||||
{ domain: '_dmarc.github.com', type: 'TXT', description: 'DMARC policy record' },
|
||||
{ domain: 'microsoft.com', type: 'TXT', description: 'Multiple TXT records (SPF, verification)' },
|
||||
{ domain: 'netflix.com', type: 'NS', description: 'Name server records' },
|
||||
];
|
||||
|
||||
const examples = useExamples(examplesList);
|
||||
@@ -82,37 +84,46 @@
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
|
||||
// Try to parse JSON response
|
||||
let responseData = null;
|
||||
try {
|
||||
const responseData = JSON.parse(errorText);
|
||||
|
||||
// Handle 404 as "no records found" (warning, not error)
|
||||
if (response.status === 404 && responseData.noRecords) {
|
||||
diagnosticState.setResults({
|
||||
noRecords: true,
|
||||
message: responseData.message,
|
||||
name: responseData.name,
|
||||
type: responseData.type,
|
||||
resolver: useCustomResolver && customResolver ? customResolver.trim() : resolver,
|
||||
});
|
||||
return; // Don't throw error, just set results
|
||||
}
|
||||
|
||||
// Handle other structured errors
|
||||
if (responseData.error) {
|
||||
throw new Error(responseData.error);
|
||||
}
|
||||
|
||||
// Use API message if available
|
||||
if (responseData.message) {
|
||||
throw new Error(responseData.message);
|
||||
}
|
||||
responseData = JSON.parse(errorText);
|
||||
} catch {
|
||||
// If not JSON, use status-based message
|
||||
if (response.status === 400) {
|
||||
throw new Error('Invalid request. Please check your input values.');
|
||||
} else if (response.status === 500) {
|
||||
throw new Error('DNS lookup service temporarily unavailable. Please try again.');
|
||||
}
|
||||
// JSON parsing failed, will use status-based fallback
|
||||
}
|
||||
|
||||
// Handle 404 as "no records found" (warning, not error)
|
||||
if (response.status === 404 && responseData?.noRecords) {
|
||||
diagnosticState.setResults({
|
||||
noRecords: true,
|
||||
message: responseData.message,
|
||||
name: responseData.name,
|
||||
type: responseData.type,
|
||||
resolver: useCustomResolver && customResolver ? customResolver.trim() : resolver,
|
||||
});
|
||||
return; // Don't throw error, just set results
|
||||
}
|
||||
|
||||
// Helper to check if error message is helpful
|
||||
const isUnhelpfulError = (msg: string) => {
|
||||
return !msg || msg.includes('undefined') || msg === 'null' || msg.trim().length === 0;
|
||||
};
|
||||
|
||||
// Use API error message if available and helpful
|
||||
if (responseData?.error && !isUnhelpfulError(responseData.error)) {
|
||||
throw new Error(responseData.error);
|
||||
}
|
||||
|
||||
// Use API message if available and helpful
|
||||
if (responseData?.message && !isUnhelpfulError(responseData.message)) {
|
||||
throw new Error(responseData.message);
|
||||
}
|
||||
|
||||
// Fallback to status-based messages
|
||||
if (response.status === 400) {
|
||||
throw new Error('Invalid request. Please check your input values.');
|
||||
} else if (response.status === 500 || response.status === 403) {
|
||||
throw new Error('DNS lookup service temporarily unavailable. Please try again.');
|
||||
}
|
||||
|
||||
throw new Error(`Lookup failed (${response.status})`);
|
||||
|
||||
Reference in new issue
Block a user