Skip to content

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

fix: the stealth persona advertises a hardcoded OS version (PIN-076)

Luigi Agostiaf0fac3c2513
diff --git a/internal/stealth/emulation.go b/internal/stealth/emulation.go
index e315e2f0..bbbde7bf 100644
--- a/internal/stealth/emulation.go
+++ b/internal/stealth/emulation.go
@@ -86,10 +86,9 @@ func ApplyTargetEmulation(ctx context.Context, cfg *config.RuntimeConfig, userAg
// Override the UA Client Hints metadata ONLY when the caller supplied an
// explicit UA (userAgent is the launch --user-agent, set only for a configured
// custom UA). Otherwise defer to Chrome's NATIVE UA-CH: the synthesized
- // metadata is inconsistent with a real Chrome — a stale GREASE brand, a
- // hardcoded platformVersion, and (because cdproto's UserAgentMetadata has no
- // full_version field) an empty uaFullVersion — whereas the native hints are
- // correct and self-consistent.
+ // metadata is inconsistent with a real Chrome — a stale GREASE brand and
+ // (because cdproto's UserAgentMetadata has no full_version field) an empty
+ // uaFullVersion — whereas the native hints are correct and self-consistent.
if strings.TrimSpace(userAgent) != "" {
if uaOverride := BuildUserAgentOverride(userAgent, cfg.BrowserVersion); uaOverride != nil {
if err := uaOverride.Do(ctx); err != nil {
diff --git a/internal/stealth/ua.go b/internal/stealth/ua.go
index 9d58d97c..2ec4c729 100644
--- a/internal/stealth/ua.go
+++ b/internal/stealth/ua.go
@@ -3,6 +3,9 @@ package stealth
import (
goruntime "runtime"
"strings"
+ "sync"
+
+ "github.com/shirou/gopsutil/v4/host"
)
type BrandVersion struct {
@@ -90,17 +93,15 @@ func BuildPersona(userAgent, chromeVersion string) BrowserPersona {
navigatorPlatform := "Linux x86_64"
uaDataPlatform := "Linux"
- platformVersion := "6.5.0"
switch {
case strings.Contains(ua, "Windows"):
navigatorPlatform = "Win32"
uaDataPlatform = "Windows"
- platformVersion = "15.0.0"
case strings.Contains(ua, "Macintosh"), strings.Contains(ua, "Mac OS X"):
navigatorPlatform = "MacIntel"
uaDataPlatform = "macOS"
- platformVersion = "14.0.0"
}
+ platformVersion := PlatformVersionFor(uaDataPlatform)
architecture := "x86"
switch {
@@ -141,6 +142,81 @@ func BuildPersona(userAgent, chromeVersion string) BrowserPersona {
}
}
+var defaultPlatformVersions = map[string]string{
+ "Windows": "15.0.0",
+ "macOS": "14.0.0",
+ "Linux": "6.5.0",
+}
+
+var hostProductVersion = sync.OnceValue(func() string {
+ _, _, version, err := host.PlatformInformation()
+ if err != nil {
+ return ""
+ }
+ return version
+})
+
+var hostKernelVersion = sync.OnceValue(func() string {
+ version, err := host.KernelVersion()
+ if err != nil {
+ return ""
+ }
+ return version
+})
+
+// PlatformVersionFor returns the UA-CH platformVersion to advertise for a
+// userAgentData platform. Real Chrome reports the running host's OS version, so
+// a persona describing this host reads it from the host — a frozen value
+// contradicts the Sec-CH-UA-Platform-Version header Chrome itself sends. A
+// persona describing a different platform keeps a plausible constant.
+func PlatformVersionFor(uaDataPlatform string) string {
+ if version := normalizePlatformVersion(hostPlatformVersion(uaDataPlatform)); version != "" {
+ return version
+ }
+ return defaultPlatformVersions[uaDataPlatform]
+}
+
+func hostPlatformVersion(uaDataPlatform string) string {
+ switch {
+ case uaDataPlatform == "macOS" && goruntime.GOOS == "darwin":
+ return hostProductVersion()
+ case uaDataPlatform == "Linux" && goruntime.GOOS == "linux":
+ return hostKernelVersion()
+ default:
+ return ""
+ }
+}
+
+func normalizePlatformVersion(raw string) string {
+ parts := make([]string, 0, 3)
+ for _, field := range strings.Split(raw, ".") {
+ digits := leadingDigits(field)
+ if digits == "" {
+ break
+ }
+ parts = append(parts, digits)
+ if len(parts) == 3 {
+ break
+ }
+ }
+ if len(parts) == 0 {
+ return ""
+ }
+ for len(parts) < 3 {
+ parts = append(parts, "0")
+ }
+ return strings.Join(parts, ".")
+}
+
+func leadingDigits(field string) string {
+ for i := 0; i < len(field); i++ {
+ if field[i] < '0' || field[i] > '9' {
+ return field[:i]
+ }
+ }
+ return field
+}
+
func chromeVersionOrFallback(chromeVersion string) string {
if chromeVersion != "" {
return chromeVersion
diff --git a/internal/stealth/ua_test.go b/internal/stealth/ua_test.go
new file mode 100644
index 00000000..4f6ce585
--- /dev/null
+++ b/internal/stealth/ua_test.go
@@ -0,0 +1,95 @@
+package stealth
+
+import (
+ "os/exec"
+ goruntime "runtime"
+ "strings"
+ "testing"
+)
+
+func hostVersionOracle(t *testing.T) (uaDataPlatform, version string) {
+ t.Helper()
+ switch goruntime.GOOS {
+ case "darwin":
+ out, err := exec.Command("sw_vers", "-productVersion").Output()
+ if err != nil {
+ t.Skipf("sw_vers unavailable: %v", err)
+ }
+ return "macOS", strings.TrimSpace(string(out))
+ case "linux":
+ out, err := exec.Command("uname", "-r").Output()
+ if err != nil {
+ t.Skipf("uname unavailable: %v", err)
+ }
+ return "Linux", strings.TrimSpace(string(out))
+ default:
+ t.Skipf("no host version oracle for %s", goruntime.GOOS)
+ return "", ""
+ }
+}
+
+func TestPlatformVersionForHostPlatformTracksHost(t *testing.T) {
+ uaDataPlatform, raw := hostVersionOracle(t)
+ want := normalizePlatformVersion(raw)
+ if want == "" {
+ t.Fatalf("oracle produced no version from %q", raw)
+ }
+ if got := PlatformVersionFor(uaDataPlatform); got != want {
+ t.Fatalf("PlatformVersionFor(%q) = %q, want host version %q", uaDataPlatform, got, want)
+ }
+ if got := PlatformVersionFor(uaDataPlatform); got == defaultPlatformVersions[uaDataPlatform] && want != defaultPlatformVersions[uaDataPlatform] {
+ t.Fatalf("PlatformVersionFor(%q) returned the frozen default", uaDataPlatform)
+ }
+}
+
+func TestPlatformVersionForForeignPlatformUsesDefault(t *testing.T) {
+ foreign := "Windows"
+ if goruntime.GOOS == "windows" {
+ foreign = "macOS"
+ }
+ if got := PlatformVersionFor(foreign); got != defaultPlatformVersions[foreign] {
+ t.Fatalf("PlatformVersionFor(%q) = %q, want default %q", foreign, got, defaultPlatformVersions[foreign])
+ }
+ if got := PlatformVersionFor("Android"); got != "" {
+ t.Fatalf("PlatformVersionFor unknown platform = %q, want empty", got)
+ }
+}
+
+func TestNormalizePlatformVersion(t *testing.T) {
+ cases := map[string]string{
+ "26.5.1": "26.5.1",
+ "26.5": "26.5.0",
+ "15": "15.0.0",
+ "6.5.0-27-generic": "6.5.0",
+ "6.11.0-19-lowlatency": "6.11.0",
+ "10.0.22631 Build 22631": "10.0.22631",
+ "": "",
+ "unknown": "",
+ "14.7.1.2": "14.7.1",
+ }
+ for raw, want := range cases {
+ if got := normalizePlatformVersion(raw); got != want {
+ t.Errorf("normalizePlatformVersion(%q) = %q, want %q", raw, got, want)
+ }
+ }
+}
+
+func TestBuildPersonaPlatformVersionMatchesPlatform(t *testing.T) {
+ windows := BuildPersona("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36", "144.0.7559.133")
+ if windows.UserAgentData.Platform != "Windows" {
+ t.Fatalf("platform = %q, want Windows", windows.UserAgentData.Platform)
+ }
+ if windows.UserAgentData.PlatformVersion != PlatformVersionFor("Windows") {
+ t.Fatalf("platformVersion = %q, want %q", windows.UserAgentData.PlatformVersion, PlatformVersionFor("Windows"))
+ }
+
+ uaDataPlatform, _ := hostVersionOracle(t)
+ hostUA := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36"
+ if uaDataPlatform == "Linux" {
+ hostUA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36"
+ }
+ persona := BuildPersona(hostUA, "144.0.7559.133")
+ if persona.UserAgentData.PlatformVersion != PlatformVersionFor(uaDataPlatform) {
+ t.Fatalf("host persona platformVersion = %q, want %q", persona.UserAgentData.PlatformVersion, PlatformVersionFor(uaDataPlatform))
+ }
+}