Skip to content

Stealth Chromium that passes every bot detection test. Drop-in Playwright replacement with source-level fingerprint patches. 50+ tests passed.

fix(humanize): map iframes by url, not name (#624)

CloakHQ146f46e07d42
diff --git a/cloakbrowser/human/world.py b/cloakbrowser/human/world.py
index 9456741..4a57570 100644
--- a/cloakbrowser/human/world.py
+++ b/cloakbrowser/human/world.py
@@ -631,13 +631,15 @@ def _match_child(frame: Any, parent: Any, kids: List[Dict[str, Any]]) -> Optiona
"""Pick the CDP child frame for a Playwright frame.
Playwright and CDP list child frames in attach order; frames sharing the
- same (name, url) are matched by their position within that group.
+ same url are matched by their position within that group. The name is not
+ part of the key: page JS can set ``window.name``, which CDP reports but
+ Playwright never refreshes.
"""
- def key_pw(f: Any) -> Tuple[str, str]:
- return f.name, f.url
+ def key_pw(f: Any) -> str:
+ return f.url
- def key_cdp(k: Dict[str, Any]) -> Tuple[str, str]:
- return k.get("name", ""), k.get("url", "") + k.get("urlFragment", "")
+ def key_cdp(k: Dict[str, Any]) -> str:
+ return k.get("url", "") + k.get("urlFragment", "")
want = key_pw(frame)
cands = [k for k in kids if key_cdp(k) == want]
diff --git a/dotnet/src/CloakBrowser/Human/HumanWorld.cs b/dotnet/src/CloakBrowser/Human/HumanWorld.cs
index ca3786e..09036f8 100644
--- a/dotnet/src/CloakBrowser/Human/HumanWorld.cs
+++ b/dotnet/src/CloakBrowser/Human/HumanWorld.cs
@@ -155,12 +155,12 @@ internal sealed class HumanWorld
}
/// <summary>Playwright and CDP list child frames in attach order; frames sharing the
- /// same (name, url) are matched by their position within that group.</summary>
+ /// same url are matched by their position within that group. The name is not part of
+ /// the key: page JS can set <c>window.name</c>, which CDP reports but Playwright never refreshes.</summary>
internal static string? MatchChild(IFrame frame, IFrame parent, IReadOnlyList<JsonElement> kids)
{
- static string KeyPw(IFrame f) => f.Name + "\u0000" + f.Url;
+ static string KeyPw(IFrame f) => f.Url;
static string KeyCdp(JsonElement k) =>
- (k.TryGetProperty("name", out var n) ? n.GetString() : "") + "\u0000" +
(k.TryGetProperty("url", out var u) ? u.GetString() : "") +
(k.TryGetProperty("urlFragment", out var uf) ? uf.GetString() : "");
var want = KeyPw(frame);
diff --git a/dotnet/tests/CloakBrowser.Tests/Human/MatchChildTests.cs b/dotnet/tests/CloakBrowser.Tests/Human/MatchChildTests.cs
new file mode 100644
index 0000000..e87b844
--- /dev/null
+++ b/dotnet/tests/CloakBrowser.Tests/Human/MatchChildTests.cs
@@ -0,0 +1,58 @@
+using System.Reflection;
+using System.Text.Json;
+using CloakBrowser.Human;
+using Microsoft.Playwright;
+using Xunit;
+
+namespace CloakBrowser.Tests.Human;
+
+/// <summary>Mapping a Playwright frame to its CDP frame id (HumanWorld.MatchChild).</summary>
+public class MatchChildTests
+{
+ public class FakeFrame : DispatchProxy
+ {
+ public string Name = "", Url = "";
+ public List<IFrame> Kids = new();
+
+ protected override object? Invoke(MethodInfo? m, object?[]? args) => m!.Name switch
+ {
+ "get_Name" => Name,
+ "get_Url" => Url,
+ "get_ChildFrames" => Kids,
+ _ => throw new NotSupportedException(m.Name),
+ };
+ }
+
+ private static IFrame F(string name, string url, params IFrame[] kids)
+ {
+ var f = DispatchProxy.Create<IFrame, FakeFrame>();
+ var p = (FakeFrame)(object)f;
+ p.Name = name; p.Url = url; p.Kids = kids.ToList();
+ return f;
+ }
+
+ private static List<JsonElement> Kids(string json) =>
+ JsonDocument.Parse(json).RootElement.EnumerateArray().ToList();
+
+ [Fact]
+ public void Survives_frame_renamed_by_page_js()
+ {
+ // CDP has the window.name set by page JS, Playwright the old one, and the
+ // cross-site sibling (own process) is absent from the parent's CDP tree.
+ var mid = F("m", "http://site.test/mid.html");
+ var parent = F("", "", F("", "http://other.test/b.html"), mid);
+ var kids = Kids("""[{"id":"MID","name":"renamed-by-js","url":"http://site.test/mid.html"}]""");
+ Assert.Equal("MID", HumanWorld.MatchChild(mid, parent, kids));
+ }
+
+ [Fact]
+ public void Same_url_siblings_keep_attach_order_after_rename()
+ {
+ var a = F("a", "http://site.test/w.html");
+ var b = F("b", "http://site.test/w.html");
+ var parent = F("", "", F("", "http://other.test/x.html"), a, b);
+ var kids = Kids("""[{"id":"A","name":"a","url":"http://site.test/w.html"}, {"id":"B","name":"renamed","url":"http://site.test/w.html"}]""");
+ Assert.Equal("A", HumanWorld.MatchChild(a, parent, kids));
+ Assert.Equal("B", HumanWorld.MatchChild(b, parent, kids));
+ }
+}
diff --git a/js/src/human/world.ts b/js/src/human/world.ts
index e60a929..87cb6ef 100644
--- a/js/src/human/world.ts
+++ b/js/src/human/world.ts
@@ -63,12 +63,13 @@ function findNode(tree: any, cdpId: string): any {
/**
* Pick the CDP child frame for a Playwright frame. Playwright and CDP list
- * child frames in attach order; frames sharing the same (name, url) are
- * matched by their position within that group.
+ * child frames in attach order; frames sharing the same url are matched by
+ * their position within that group. The name is not part of the key: page JS
+ * can set window.name, which CDP reports but Playwright never refreshes.
*/
export function matchChild(frame: Frame, parent: Frame, kids: any[]): string | null {
- const keyPw = (f: Frame) => `${f.name()}\u0000${f.url()}`;
- const keyCdp = (k: any) => `${k.name ?? ''}\u0000${(k.url ?? '') + (k.urlFragment ?? '')}`;
+ const keyPw = (f: Frame) => f.url();
+ const keyCdp = (k: any) => (k.url ?? '') + (k.urlFragment ?? '');
const want = keyPw(frame);
const cands = kids.filter((k) => keyCdp(k) === want);
const siblings = parent.childFrames();
diff --git a/js/tests/humanize.test.ts b/js/tests/humanize.test.ts
index 53f2ffe..517050d 100644
--- a/js/tests/humanize.test.ts
+++ b/js/tests/humanize.test.ts
@@ -588,3 +588,34 @@ describe("mergeConfig", () => {
});
});
+// =========================================================================
+// Frame mapping (Playwright frame -> CDP frame id)
+// =========================================================================
+describe("matchChild", () => {
+ const F = (name: string, url: string, childFrames: any[] = []): any =>
+ ({ name: () => name, url: () => url, childFrames: () => childFrames });
+
+ it("survives a frame renamed by page JS", async () => {
+ // CDP has the window.name set by page JS, Playwright the old one, and the
+ // cross-site sibling (own process) is absent from the parent's CDP tree.
+ const { matchChild } = await import("../src/human/world.js");
+ const mid = F("m", "http://site.test/mid.html");
+ const parent = F("", "", [F("", "http://other.test/b.html"), mid]);
+ const kids = [{ id: "MID", name: "renamed-by-js", url: "http://site.test/mid.html" }];
+ expect(matchChild(mid, parent, kids)).toBe("MID");
+ });
+
+ it("keeps attach order for same-url siblings after a rename", async () => {
+ const { matchChild } = await import("../src/human/world.js");
+ const a = F("a", "http://site.test/w.html");
+ const b = F("b", "http://site.test/w.html");
+ const parent = F("", "", [F("", "http://other.test/x.html"), a, b]);
+ const kids = [
+ { id: "A", name: "a", url: "http://site.test/w.html" },
+ { id: "B", name: "renamed", url: "http://site.test/w.html" },
+ ];
+ expect(matchChild(a, parent, kids)).toBe("A");
+ expect(matchChild(b, parent, kids)).toBe("B");
+ });
+});
+
diff --git a/tests/test_humanize_unit.py b/tests/test_humanize_unit.py
index 090c875..3ea511b 100644
--- a/tests/test_humanize_unit.py
+++ b/tests/test_humanize_unit.py
@@ -1133,3 +1133,29 @@ def test_dispatch_event_raises_on_world_error():
human, target = _human_with({"error": "boom"})
with pytest.raises(Error, match="page.dispatch_event: Error: boom"):
asyncio.run(human.dispatch(target, {}, "page.dispatch_event", "click"))
+
+
+def test_match_child_survives_frame_renamed_by_page_js():
+ # Page JS set window.name: CDP has the new name, Playwright the old one, and
+ # the cross-site sibling (own process) is absent from the parent's CDP tree.
+ from types import SimpleNamespace as F
+ from cloakbrowser.human.world import _match_child
+
+ oopif = F(name="", url="http://other.test/b.html")
+ mid = F(name="m", url="http://site.test/mid.html")
+ parent = F(child_frames=[oopif, mid])
+ kids = [{"id": "MID", "name": "renamed-by-js", "url": "http://site.test/mid.html"}]
+ assert _match_child(mid, parent, kids) == "MID"
+
+
+def test_match_child_same_url_siblings_keep_attach_order_after_rename():
+ from types import SimpleNamespace as F
+ from cloakbrowser.human.world import _match_child
+
+ a = F(name="a", url="http://site.test/w.html")
+ b = F(name="b", url="http://site.test/w.html")
+ parent = F(child_frames=[F(name="", url="http://other.test/x.html"), a, b])
+ kids = [{"id": "A", "name": "a", "url": "http://site.test/w.html"},
+ {"id": "B", "name": "renamed", "url": "http://site.test/w.html"}]
+ assert _match_child(a, parent, kids) == "A"
+ assert _match_child(b, parent, kids) == "B"