Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: address review — startup race, toString self-disclosure, behavioral probes

Luigi Agosti63a6f7e2b91f
diff --git a/internal/assets/stealth.js b/internal/assets/stealth.js
index d1b636ab..953a83f4 100644
--- a/internal/assets/stealth.js
+++ b/internal/assets/stealth.js
@@ -101,6 +101,8 @@ function installFunctionToStringMask(targetProto) {
}
return Reflect.apply(nativeFunctionToString, this, []);
}
+ // Register the replacement toString itself so toString.call(toString) returns [native code].
+ try { nativeFunctionSourceMap.set(toString, makeNativeFunctionSource('toString')); } catch(e) {}
Object.defineProperty(targetProto, 'toString', {
value: toString, configurable: true, writable: true, enumerable: false
});
@@ -617,28 +619,28 @@ if (navigator.maxTouchPoints !== 0) {
const nativeCWDesc = Object.getOwnPropertyDescriptor(iframeProto, 'contentWindow');
if (nativeCWDesc && nativeCWDesc.get) {
const nativeCWGet = nativeCWDesc.get;
+ function contentWindowGetter() {
+ const fw = Reflect.apply(nativeCWGet, this, []);
+ if (fw && fw !== window) { try { patchIframeWindow(fw); } catch(e) {} }
+ return fw;
+ }
+ recordNativeFunctionSource(contentWindowGetter, 'get contentWindow');
Object.defineProperty(iframeProto, 'contentWindow', {
- get() {
- const fw = Reflect.apply(nativeCWGet, this, []);
- if (fw && fw !== window) { try { patchIframeWindow(fw); } catch(e) {} }
- return fw;
- },
- configurable: true,
- enumerable: true
+ get: contentWindowGetter, configurable: true, enumerable: true
});
}
const nativeCDDesc = Object.getOwnPropertyDescriptor(iframeProto, 'contentDocument');
if (nativeCDDesc && nativeCDDesc.get) {
const nativeCDGet = nativeCDDesc.get;
+ function contentDocumentGetter() {
+ const fd = Reflect.apply(nativeCDGet, this, []);
+ const fw = fd && fd.defaultView;
+ if (fw && fw !== window) { try { patchIframeWindow(fw); } catch(e) {} }
+ return fd;
+ }
+ recordNativeFunctionSource(contentDocumentGetter, 'get contentDocument');
Object.defineProperty(iframeProto, 'contentDocument', {
- get() {
- const fd = Reflect.apply(nativeCDGet, this, []);
- const fw = fd && fd.defaultView;
- if (fw && fw !== window) { try { patchIframeWindow(fw); } catch(e) {} }
- return fd;
- },
- configurable: true,
- enumerable: true
+ get: contentDocumentGetter, configurable: true, enumerable: true
});
}
} catch (e) {}
diff --git a/internal/bridge/profile_lock.go b/internal/bridge/profile_lock.go
index ddde5d85..2e7a65f7 100644
--- a/internal/bridge/profile_lock.go
+++ b/internal/bridge/profile_lock.go
@@ -21,6 +21,7 @@ import (
"path/filepath"
"regexp"
"strings"
+ "time"
)
var chromeProfileProcessLister = findChromeProfileProcesses
@@ -29,6 +30,12 @@ var killChromeProfileProcesses = killProcesses
var isProfileOwnedByRunningPinchtabMock = isProfileOwnedByRunningPinchtab
var isPinchTabProcessFunc = isPinchTabProcess
+// lockStartupGrace is the window after a PID file is written during which the
+// Chrome process check is skipped. AcquireProfileLock writes the PID before
+// InitChrome launches the browser, so a second process must not treat the
+// missing Chrome as a stale lock during this startup window.
+var lockStartupGrace = 2 * time.Minute
+
var chromeSingletonFiles = []string{
"SingletonLock",
"SingletonSocket",
@@ -140,6 +147,14 @@ func isProfileOwnedByRunningPinchtab(profileDir string) (bool, int) {
// Even if the PID is running, check if it's actually a pinchtab process
// to handle PID reuse.
if isPinchTabProcessFunc(pid) {
+ // Skip the Chrome check while the PID file is fresh: AcquireProfileLock
+ // writes the file before InitChrome launches the browser, so a second
+ // process must not steal the lock during that startup window.
+ if info, statErr := os.Stat(pidFile); statErr == nil && time.Since(info.ModTime()) < lockStartupGrace {
+ slog.Debug("profile lock belongs to a recently started pinchtab; treating as active",
+ "profile", profileDir, "pid", pid)
+ return true, pid
+ }
processes, procErr := chromeProfileProcessLister(profileDir)
if procErr == nil && len(processes) == 0 {
slog.Debug("pinchtab pid file points to a running pinchtab but no chrome is using the profile; treating lock as stale",
diff --git a/internal/bridge/profile_lock_test.go b/internal/bridge/profile_lock_test.go
index bebd7e63..8ec930c0 100644
--- a/internal/bridge/profile_lock_test.go
+++ b/internal/bridge/profile_lock_test.go
@@ -5,6 +5,7 @@ import (
"path/filepath"
"reflect"
"testing"
+ "time"
)
func TestIsChromeProfileLockError(t *testing.T) {
@@ -168,9 +169,15 @@ func TestClearStaleChromeProfileLockFallsBackToPIDProbe(t *testing.T) {
func TestIsProfileOwnedByRunningPinchtabTreatsPinchTabPIDWithoutChromeAsStale(t *testing.T) {
profileDir := t.TempDir()
- if err := os.WriteFile(filepath.Join(profileDir, "pinchtab.pid"), []byte("1234"), 0644); err != nil {
+ pidFile := filepath.Join(profileDir, "pinchtab.pid")
+ if err := os.WriteFile(pidFile, []byte("1234"), 0644); err != nil {
t.Fatalf("write pid file: %v", err)
}
+ // Backdate the PID file so it falls outside the startup grace window.
+ old := time.Now().Add(-10 * time.Minute)
+ if err := os.Chtimes(pidFile, old, old); err != nil {
+ t.Fatalf("backdate pid file: %v", err)
+ }
origPID := chromePIDIsRunning
origPinchTab := isPinchTabProcessFunc
@@ -191,6 +198,32 @@ func TestIsProfileOwnedByRunningPinchtabTreatsPinchTabPIDWithoutChromeAsStale(t
}
}
+func TestIsProfileOwnedByRunningPinchtabKeepsLockDuringStartup(t *testing.T) {
+ profileDir := t.TempDir()
+ // PID file written just now — Chrome hasn't launched yet (startup window).
+ if err := os.WriteFile(filepath.Join(profileDir, "pinchtab.pid"), []byte("1234"), 0644); err != nil {
+ t.Fatalf("write pid file: %v", err)
+ }
+
+ origPID := chromePIDIsRunning
+ origPinchTab := isPinchTabProcessFunc
+ origLister := chromeProfileProcessLister
+ t.Cleanup(func() {
+ chromePIDIsRunning = origPID
+ isPinchTabProcessFunc = origPinchTab
+ chromeProfileProcessLister = origLister
+ })
+
+ chromePIDIsRunning = func(pid int) (bool, error) { return pid == 1234, nil }
+ isPinchTabProcessFunc = func(pid int) bool { return pid == 1234 }
+ chromeProfileProcessLister = func(path string) ([]chromeProfileProcess, error) { return nil, nil }
+
+ owned, pid := isProfileOwnedByRunningPinchtab(profileDir)
+ if !owned || pid != 1234 {
+ t.Fatalf("expected lock to be held during startup window, got owned=%v pid=%d", owned, pid)
+ }
+}
+
func TestIsProfileOwnedByRunningPinchtabKeepsLockWhenChromeUsesProfile(t *testing.T) {
profileDir := t.TempDir()
if err := os.WriteFile(filepath.Join(profileDir, "pinchtab.pid"), []byte("1234"), 0644); err != nil {
diff --git a/tests/e2e/fixtures/stealth-capabilities.html b/tests/e2e/fixtures/stealth-capabilities.html
index b0f0d572..b4703ad7 100644
--- a/tests/e2e/fixtures/stealth-capabilities.html
+++ b/tests/e2e/fixtures/stealth-capabilities.html
@@ -403,7 +403,11 @@
iframeIsolation,
errorStackSanitized: fullModeRuntime,
stackProbe: stackProbe.stack,
- functionToStringMasked: fullModeRuntime,
+ functionToStringMasked: safe(() => {
+ if (!fullModeRuntime) return false;
+ const source = Function.prototype.toString.call(Function.prototype.toString);
+ return /\[native code\]/.test(source);
+ }, false),
iframeFunctionToStringParity,
functionToStringNative,
functionToStringName: safe(() => Function.prototype.toString.name, ''),