chore: drop old client support (#14369)

This commit is contained in:
DarkSky
2026-02-05 02:49:33 +08:00
committed by GitHub
parent de29e8300a
commit 403f16b404
103 changed files with 3293 additions and 997 deletions
@@ -0,0 +1,84 @@
import { isAllowedRedirectTarget } from '@toeverything/infra/utils';
import { buildType, isDev } from '../config';
const API_BASE_BY_BUILD_TYPE: Record<typeof buildType, string> = {
stable: 'https://app.affine.pro',
beta: 'https://insider.affine.pro',
internal: 'https://insider.affine.pro',
canary: 'https://affine.fail',
};
function resolveCurrentHostnameForRedirectAllowlist() {
const devServerBase = process.env.DEV_SERVER_URL;
const base =
isDev && devServerBase
? devServerBase
: (API_BASE_BY_BUILD_TYPE[buildType] ?? API_BASE_BY_BUILD_TYPE.stable);
try {
return new URL(base).hostname;
} catch {
return 'app.affine.pro';
}
}
export function extractRedirectTarget(rawUrl: string) {
try {
const parsed = new URL(rawUrl);
const redirectUri = parsed.searchParams.get('redirect_uri');
if (redirectUri) {
return redirectUri;
}
if (parsed.hash) {
const hash = parsed.hash.startsWith('#')
? parsed.hash.slice(1)
: parsed.hash;
const queryIndex = hash.indexOf('?');
if (queryIndex !== -1) {
const hashParams = new URLSearchParams(hash.slice(queryIndex + 1));
const hashRedirect = hashParams.get('redirect_uri');
if (hashRedirect) {
return hashRedirect;
}
}
}
return null;
} catch {
return null;
}
}
export type RedirectProxyValidationResult =
| {
allow: true;
redirectTarget: string;
}
| {
allow: false;
reason: 'missing_redirect_target' | 'untrusted_redirect_target';
redirectTarget?: string;
};
export function validateRedirectProxyUrl(
rawUrl: string
): RedirectProxyValidationResult {
const redirectTarget = extractRedirectTarget(rawUrl);
if (!redirectTarget) {
return { allow: false, reason: 'missing_redirect_target' };
}
const currentHostname = resolveCurrentHostnameForRedirectAllowlist();
if (!isAllowedRedirectTarget(redirectTarget, { currentHostname })) {
return {
allow: false,
reason: 'untrusted_redirect_target',
redirectTarget,
};
}
return { allow: true, redirectTarget };
}
@@ -0,0 +1,117 @@
import * as dns from 'node:dns/promises';
import { BlockList, isIP } from 'node:net';
const ALLOWED_PROTOCOLS = new Set(['http:', 'https:']);
const BLOCKED_IPS = new BlockList();
const ALLOWED_IPV6 = new BlockList();
function stripZoneId(address: string) {
const idx = address.indexOf('%');
return idx === -1 ? address : address.slice(0, idx);
}
// Use Node's built-in BlockList (Electron 39 ships with Node 22.x).
for (const [network, prefix] of [
['0.0.0.0', 8],
['10.0.0.0', 8],
['127.0.0.0', 8],
['169.254.0.0', 16],
['172.16.0.0', 12],
['192.168.0.0', 16],
['100.64.0.0', 10], // CGNAT
['224.0.0.0', 4], // multicast
['240.0.0.0', 4], // reserved (includes broadcast)
] as const) {
BLOCKED_IPS.addSubnet(network, prefix, 'ipv4');
}
BLOCKED_IPS.addAddress('::', 'ipv6');
BLOCKED_IPS.addAddress('::1', 'ipv6');
BLOCKED_IPS.addSubnet('ff00::', 8, 'ipv6'); // multicast
BLOCKED_IPS.addSubnet('fc00::', 7, 'ipv6'); // unique local
BLOCKED_IPS.addSubnet('fe80::', 10, 'ipv6'); // link-local
ALLOWED_IPV6.addSubnet('2000::', 3, 'ipv6'); // global unicast
function extractEmbeddedIPv4FromIPv6(address: string): string | null {
if (!address.includes('.')) {
return null;
}
const idx = address.lastIndexOf(':');
if (idx === -1) {
return null;
}
const tail = address.slice(idx + 1);
return isIP(tail) === 4 ? tail : null;
}
function isBlockedIpAddress(address: string): boolean {
const ip = stripZoneId(address);
const family = isIP(ip);
if (family === 4) {
return BLOCKED_IPS.check(ip, 'ipv4');
}
if (family === 6) {
const embeddedV4 = extractEmbeddedIPv4FromIPv6(ip);
if (embeddedV4) {
return isBlockedIpAddress(embeddedV4);
}
if (!ALLOWED_IPV6.check(ip, 'ipv6')) {
return true;
}
return BLOCKED_IPS.check(ip, 'ipv6');
}
return true;
}
async function resolveHostAddresses(hostname: string): Promise<string[]> {
const lowered = hostname.toLowerCase();
if (lowered === 'localhost' || lowered.endsWith('.localhost')) {
return ['127.0.0.1', '::1'];
}
const results = await dns.lookup(hostname, { all: true, verbatim: true });
return results.map(r => r.address);
}
export async function resolveAndValidateUrlForPreview(
rawUrl: string
): Promise<{ url: URL; address: string }> {
let url: URL;
try {
url = new URL(rawUrl);
} catch {
throw new Error('Invalid URL');
}
if (!ALLOWED_PROTOCOLS.has(url.protocol)) {
throw new Error('Disallowed URL protocol');
}
if (url.username || url.password) {
throw new Error('URL must not include credentials');
}
if (!url.hostname) {
throw new Error('Missing hostname');
}
if (isIP(url.hostname)) {
if (isBlockedIpAddress(url.hostname)) {
throw new Error('Blocked IP address');
}
return { url, address: url.hostname };
}
const addresses = await resolveHostAddresses(url.hostname);
if (!addresses.length) {
throw new Error('Unresolvable hostname');
}
for (const addr of addresses) {
if (isBlockedIpAddress(addr)) {
throw new Error('Blocked IP address');
}
}
return { url, address: addresses[0] };
}