Skip to content

fix(cookies): 按每个cookie自己的originAttributes改和删(#2) - #37

Merged
WhiteNightShadow merged 2 commits into
mainfrom
fix/cookie-origin-attributes
Nov 2, 2026
Merged

WhiteNightShadow merged 2 commits into
mainfrom
fix/cookie-origin-attributes

Conversation

@WhiteNightShadow

Copy link
Copy Markdown
Owner

背景

#2要的是在浏览器里删除、修改、新增cookie(含httpOnly)。v0.21.0加的cookies工具有几处问题:

  • originAttributes固定传空:set和remove里的originAttributes固定传{}。容器标签页里的cookie、第三方iframe的分区cookie在list里看得到,但改不了也删不掉。
  • 按域名删时计数不准:这些没删掉的cookie照样算进removed。
  • 单个删除不核对:不管删没删掉都报removed: 1。域cookie的host带前导点(.example.com),传example.com其实一个都没删。
  • 改值会丢属性:没给的属性一律用默认值。改一个httpOnly会话cookie的值,它会变成非httpOnly、持久一年的cookie。

改动(按提交)

  • fix(cookies) 按每个cookie自己的originAttributes操作(be2d20f):
    • 新模块:新增CookieBackend.sys.mjs,已在moz.build登记,替换Backends.sys.mjs里的内联实现,cookie管理器可以注入。
    • list:每个cookie用精简的originAttributes标出所在的罐,分区cookie另标isPartitioned。includePrivate:true时也列隐私窗口的cookie,nsICookieManager.cookies本身不含这些。
    • remove:
      • 匹配规则:host不分前导点;不给path时匹配所有路径;不给originAttributes时匹配所有罐。
      • 删除方式:每个cookie用它自己的host和originAttributes删,删完重新读一遍存储,只把确实没了的算进removed,没删掉的列在failed里。
      • all:true:仍只清普通存储,加includePrivate时连隐私存储一起清。
    • set:写进指定的罐,不指定就写普通罐;读回时按host、名称、路径和罐精确匹配。改已有cookie时,没给的属性(secure、httpOnly、sameSite、过期方式、partitioned)沿用原值。
    • 新增自测:selftest-cookies.mjs共51项,用一个按锁定基线nsICookieManager语义写的假实现。
  • docs(readme)(3b00980):工具表里的cookie一行;版本记录新开“main(尚未发布)”一节。

验证

  • 全量自测:bash scripts/selftest-agent-tools.sh通过,共45步(新增cookie一步,51项)。
  • 对照旧实现:把main上的旧代码接到同一个假cookie管理器上,三个场景都能复现:
    • 按名删sid报removed:1,域、容器、分区三份还在;
    • 删不存在的cookie也报removed:1;
    • 按域名删报removed:5,容器和分区那两份还在。
  • 真实引擎冒烟:在v0.27.0最终arm64包的临时副本里,把这三个模块放进omni.ja并重签。用临时profile、无头模式启动,经marionette调用getBackends().cookies:
    • 引擎里预先写入普通罐的host-only和域cookie、容器2、分区((https,top.test),secure+Partitioned)、隐私窗口、httpOnly会话cookie,全部写入成功;
    • 默认列出4个罐,加includePrivate为5个;
    • 改容器cookie和分区cookie都成功,分区cookie保留secure和Partitioned,普通罐的同名cookie没被动到;
    • 只改httpOnly会话cookie的值,它仍是httpOnly和会话cookie;只改SameSite=None cookie的值,secure被沿用;显式secure:false时被引擎拒绝,原因原样返回;
    • 删除:按罐删1个,按名删3个,删不存在的报0个;隐私窗口的要加includePrivate才删;子域的cookie不受影响;
    • 浏览器正常退出。

没验证的

  • 没有出新包,也没跑多平台原生ci。改动只在侧栏的js模块里,和平台无关,按惯例等下次出包时统一验收。包身份核对会多出一个模块(44个)。
  • 隐私窗口的cookie是直接写进隐私存储测的,测试时没有开着的隐私窗口。

已知问题(本pr未处理)

  • list的domain仍按host子串过滤,国际化域名要写成punycode。

🤖 Generated with Claude Code

WhiteNightShadow and others added 2 commits November 2, 2026 07:08
The cookies tool always passed {} as originAttributes to set and remove,
so cookies in container tabs and partitioned (third-party iframe) cookies
showed up in list but could not be changed or deleted. Single removal also
reported removed: 1 whether or not anything was deleted, and a domain
cookie (.example.com) given as example.com was never removed.

- New CookieBackend module (registered in moz.build) replaces the inline
  implementation in Backends.sys.mjs and takes an injectable cookie manager.
- list marks each cookie's jar with a compact originAttributes and
  isPartitioned; includePrivate adds private-window cookies, which
  nsICookieManager.cookies does not return.
- remove matches the host with or without the leading dot, any path unless
  path is given and every jar unless originAttributes is given, removes each
  cookie with its own host and originAttributes, then re-reads the store and
  counts only cookies that are really gone (failed lists the rest).
  all:true still clears the normal store; includePrivate also clears the
  private one.
- set writes into the given jar (default: the normal one), reads back the
  exact host/name/path/jar, and for an existing cookie keeps the attributes
  that were not given (secure, httpOnly, sameSite, expiry, partitioned), so
  changing a value no longer drops httpOnly or the Secure that SameSite=None
  needs.
- selftest-cookies.mjs: 51 checks against a fake nsICookieManager that
  follows the pinned baseline's semantics.

Refs #2

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@WhiteNightShadow
WhiteNightShadow merged commit da9ae01 into main Nov 2, 2026
2 checks passed
@WhiteNightShadow
WhiteNightShadow deleted the fix/cookie-origin-attributes branch November 2, 2026 12:30
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.

1 participant