Skip to content

feat(csp): CSP 规则管理功能#1348

Open
boommanpro wants to merge 1 commit intoscriptscat:mainfrom
boommanpro:csp_function
Open

feat(csp): CSP 规则管理功能#1348
boommanpro wants to merge 1 commit intoscriptscat:mainfrom
boommanpro:csp_function

Conversation

@boommanpro
Copy link
Copy Markdown

@boommanpro boommanpro commented Apr 11, 2026

新增 CSP (Content-Security-Policy) 规则管理模块,支持移除或修改网页 CSP 头。

功能特性:

  • 全局开关:一键移除所有网页 CSP 头,无需配置规则
  • 规则管理:创建/编辑/删除/启用/禁用 CSP 规则
  • 匹配模式:支持精确匹配、通配符、正则表达式、域名匹配四种模式
  • 模式匹配引擎:参考 wproxy/whistle 规范实现
    • *.domain 单级子域 / **.domain 多级子域 / ***.domain 根域+多级子域
    • ^ 前缀路径通配符:* 单级 / ** 多级 / *** 任意字符
    • 协议通配符 http*:// / 混合通配符 test.abc**.com
  • 模式测试:Drawer 内置测试工具,实时验证匹配结果
  • 帮助文档:右侧 Drawer 展示匹配模式指南
  • 优先级自动递增:创建/更新时自动避免 priority 冲突

技术实现:

  • 基于 chrome.declarativeNetRequest API 拦截并修改响应头
  • Service/Client/Repo 三层架构,直接回调模式确保 DNR 规则实时更新
  • chrome.storage.local 持久化配置和规则数据
  • 63 个单元测试覆盖所有匹配模式

新增文件:

  • src/app/repo/cspRule.ts (数据层)
  • src/app/service/service_worker/cspRule.ts (服务层)
  • src/app/service/service_worker/cspInterceptor.ts (拦截器)
  • src/pages/options/routes/CSPRule/index.tsx (UI 页面)
  • src/pkg/utils/patternMatcher.ts (匹配引擎)
  • tests/pkg/utils/patternMatcher.test.ts (单元测试)

Checklist / 检查清单

  • Fixes mentioned issues / 修复已提及的问题
  • Code reviewed by human / 代码通过人工检查
  • Changes tested / 已完成测试

Description / 描述

Screenshots / 截图

image 原始未开启截图 image

成功截图
image

测试代码

// ==UserScript==
// @name         GitHub 页面加载完成执行脚本
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  GitHub 页面加载完成执行脚本
// @author       助手
// @match        https://github.com/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    console.log('=== CSP unsafe-eval 测试开始 ===');

    // 1. eval
    function testEval() {
        try {
            eval('console.log("eval 执行")');
        } catch (e) {
            console.error('eval 被 CSP 拦截:', e);
        }
    }

    // 2. new Function
    function testNewFunction() {
        try {
            const fn = new Function('console.log("new Function 执行")');
            fn();
        } catch (e) {
            console.error('new Function 被 CSP 拦截:', e);
        }
    }

    // 3. setTimeout 字符串
    function testSetTimeoutString() {
        try {
            setTimeout('console.log("setTimeout 字符串执行")', 100);
        } catch (e) {
            console.error('setTimeout 字符串被 CSP 拦截:', e);
        }
    }

    // 4. setInterval 字符串
    function testSetIntervalString() {
        try {
            setInterval('console.log("setInterval 字符串执行")', 1000);
        } catch (e) {
            console.error('setInterval 字符串被 CSP 拦截:', e);
        }
    }

    // 批量执行
    testEval();
    testNewFunction();
    testSetTimeoutString();
    testSetIntervalString();

    console.log('=== 测试完成 ===');
})();

历史讨论:#1264

新增 CSP (Content-Security-Policy) 规则管理模块,支持移除或修改网页 CSP 头。

功能特性:
- 全局开关:一键移除所有网页 CSP 头,无需配置规则
- 规则管理:创建/编辑/删除/启用/禁用 CSP 规则
- 匹配模式:支持精确匹配、通配符、正则表达式、域名匹配四种模式
- 模式匹配引擎:参考 wproxy/whistle 规范实现
  - *.domain 单级子域 / **.domain 多级子域 / ***.domain 根域+多级子域
  - ^ 前缀路径通配符:* 单级 / ** 多级 / *** 任意字符
  - 协议通配符 http*:// / 混合通配符 test.abc**.com
- 模式测试:Drawer 内置测试工具,实时验证匹配结果
- 帮助文档:右侧 Drawer 展示匹配模式指南
- 优先级自动递增:创建/更新时自动避免 priority 冲突

技术实现:
- 基于 chrome.declarativeNetRequest API 拦截并修改响应头
- Service/Client/Repo 三层架构,直接回调模式确保 DNR 规则实时更新
- chrome.storage.local 持久化配置和规则数据
- 63 个单元测试覆盖所有匹配模式

新增文件:
- src/app/repo/cspRule.ts (数据层)
- src/app/service/service_worker/cspRule.ts (服务层)
- src/app/service/service_worker/cspInterceptor.ts (拦截器)
- src/pages/options/routes/CSPRule/index.tsx (UI 页面)
- src/pkg/utils/patternMatcher.ts (匹配引擎)
- tests/pkg/utils/patternMatcher.test.ts (单元测试)
@CodFrm
Copy link
Copy Markdown
Member

CodFrm commented Apr 11, 2026

级别提这么高吗?我觉得做在设置中就差不多了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants