mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-06 13:10:27 +08:00
fix: 桌面壳外链仅放行 http/https 并严格按 origin 校验
- 抽取 packaging/desktop/url-policy.js,main.js 复用于窗口打开与导航拦截 - 同源按 URL.origin 比对,防伪造前缀域名;file/smb/javascript 等协议静默拦截 - data: 仅在启动地址确定前放行加载过渡页 - 补充同源与外链协议白名单单元测试
This commit is contained in:
@@ -12,6 +12,7 @@ const fs = require('node:fs');
|
||||
const net = require('node:net');
|
||||
const path = require('node:path');
|
||||
const { spawn } = require('node:child_process');
|
||||
const { isSameOrigin, isSafeExternalUrl } = require('./url-policy');
|
||||
|
||||
const PREFERRED_PORT = 39300;
|
||||
const HOST = '127.0.0.1';
|
||||
@@ -137,16 +138,17 @@ function createWindow() {
|
||||
});
|
||||
win.once('ready-to-show', () => win.show());
|
||||
win.on('closed', () => { win = null; });
|
||||
// 站内链接留在窗口内,其他链接交给系统浏览器
|
||||
// 站内链接留在窗口内,其他 http/https 链接交给系统浏览器;其余协议(file、smb、javascript 等)静默拦截
|
||||
win.webContents.setWindowOpenHandler(({ url }) => {
|
||||
if (baseUrl && url.startsWith(baseUrl)) return { action: 'allow' };
|
||||
shell.openExternal(url);
|
||||
if (isSameOrigin(url, baseUrl)) return { action: 'allow' };
|
||||
if (isSafeExternalUrl(url)) shell.openExternal(url);
|
||||
return { action: 'deny' };
|
||||
});
|
||||
win.webContents.on('will-navigate', (event, url) => {
|
||||
if (url.startsWith('data:') || (baseUrl && url.startsWith(baseUrl))) return;
|
||||
// data: 只用于服务就绪前的加载过渡页;启动地址确定后不再放行
|
||||
if (isSameOrigin(url, baseUrl) || (!baseUrl && url.startsWith('data:'))) return;
|
||||
event.preventDefault();
|
||||
shell.openExternal(url);
|
||||
if (isSafeExternalUrl(url)) shell.openExternal(url);
|
||||
});
|
||||
if (baseUrl) win.loadURL(baseUrl);
|
||||
else win.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(LOADING_HTML)}`);
|
||||
|
||||
3
packaging/desktop/url-policy.d.ts
vendored
Normal file
3
packaging/desktop/url-policy.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export function parseUrl(value: string): URL | null;
|
||||
export function isSameOrigin(url: string, baseUrl: string): boolean;
|
||||
export function isSafeExternalUrl(url: string): boolean;
|
||||
28
packaging/desktop/url-policy.js
Normal file
28
packaging/desktop/url-policy.js
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* 桌面壳外链策略:站内导航按 origin 严格比对,交给系统浏览器前只放行 http/https。
|
||||
* 纯函数,便于单元测试;main.js 通过 require 复用同一套判定。
|
||||
*/
|
||||
|
||||
/** 解析 URL;非法地址返回 null,避免把异常字符串交给 shell.openExternal */
|
||||
function parseUrl(value) {
|
||||
try {
|
||||
return new URL(value);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** 与启动地址同源(忽略端口、协议大小写与路径差异) */
|
||||
function isSameOrigin(url, baseUrl) {
|
||||
const target = parseUrl(url);
|
||||
const base = parseUrl(baseUrl);
|
||||
return Boolean(target && base && target.origin === base.origin);
|
||||
}
|
||||
|
||||
/** 可交给系统浏览器的外链:仅 http/https */
|
||||
function isSafeExternalUrl(url) {
|
||||
const parsed = parseUrl(url);
|
||||
return Boolean(parsed && (parsed.protocol === 'http:' || parsed.protocol === 'https:'));
|
||||
}
|
||||
|
||||
module.exports = { parseUrl, isSameOrigin, isSafeExternalUrl };
|
||||
32
src/__tests__/unit/desktop-url-policy.test.ts
Normal file
32
src/__tests__/unit/desktop-url-policy.test.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
// @vitest-environment node
|
||||
/**
|
||||
* 桌面壳外链策略:同源按 origin 严格比对(防伪造前缀域名),交给系统浏览器仅放行 http/https
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { isSameOrigin, isSafeExternalUrl } from '../../../packaging/desktop/url-policy.js';
|
||||
|
||||
describe('桌面壳外链策略', () => {
|
||||
it('同源按 origin 比对,忽略路径与协议大小写', () => {
|
||||
expect(isSameOrigin('http://127.0.0.1:39300/notebooks/1', 'http://127.0.0.1:39300')).toBe(true);
|
||||
expect(isSameOrigin('HTTP://127.0.0.1:39300/a', 'http://127.0.0.1:39300')).toBe(true);
|
||||
expect(isSameOrigin('http://127.0.0.1:39300.evil.com/a', 'http://127.0.0.1:39300')).toBe(false);
|
||||
expect(isSameOrigin('http://127.0.0.1:39301/a', 'http://127.0.0.1:39300')).toBe(false);
|
||||
expect(isSameOrigin('https://127.0.0.1:39300/a', 'http://127.0.0.1:39300')).toBe(false);
|
||||
expect(isSameOrigin('data:text/html,hi', 'http://127.0.0.1:39300')).toBe(false);
|
||||
});
|
||||
|
||||
it('非法地址不视为同源', () => {
|
||||
expect(isSameOrigin('not a url', 'http://127.0.0.1:39300')).toBe(false);
|
||||
expect(isSameOrigin('http://127.0.0.1:39300/a', 'not a url')).toBe(false);
|
||||
});
|
||||
|
||||
it('仅 http/https 外链可交给系统浏览器', () => {
|
||||
expect(isSafeExternalUrl('https://example.com/a')).toBe(true);
|
||||
expect(isSafeExternalUrl('http://example.com')).toBe(true);
|
||||
expect(isSafeExternalUrl('file:///etc/passwd')).toBe(false);
|
||||
expect(isSafeExternalUrl('javascript:alert(1)')).toBe(false);
|
||||
expect(isSafeExternalUrl('smb://server/share')).toBe(false);
|
||||
expect(isSafeExternalUrl('ms-msdt:/id PCWDiagnostic')).toBe(false);
|
||||
expect(isSafeExternalUrl('not a url')).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user