🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Update SDK version to 0.1.1 and enhance request header handling
Bill Yang0253f2e4ab02
diff --git a/react-native/index.js b/react-native/index.jsindex 84b44871a..985420ed3 100644--- a/react-native/index.js+++ b/react-native/index.js@@ -1,6 +1,6 @@"use strict";-const SDK_VERSION = "0.1.0";+const SDK_VERSION = "0.1.1";const DEFAULT_CONFIG_TIMEOUT_MS = 3000;const DEFAULT_MAX_QUEUE_SIZE = 100;@@ -99,6 +99,29 @@ function getUserAgent(appVersion) {}}+function normalizeAcceptLanguage(language) {+ const fallback = "en-US,en;q=0.9";+ const value = String(language || "").trim();+ if (!value) return fallback;++ const primaryLanguage = value.replace(/_/g, "-").split(/[;,]/)[0].trim();+ return primaryLanguage || fallback;+}++function createRequestHeaders(payload) {+ const headers = {+ "Content-Type": "application/json",+ Accept: "application/json",+ "Accept-Language": normalizeAcceptLanguage(payload?.language),+ };++ if (payload?.user_agent) {+ headers["User-Agent"] = payload.user_agent;+ }++ return headers;+}+class RybbitReactNative {constructor() {this.config = null;@@ -231,9 +254,7 @@ class RybbitReactNative {try {const response = await this.config.fetch(`${this.config.analyticsHost}/track`, {method: "POST",- headers: {- "Content-Type": "application/json",- },+ headers: createRequestHeaders(payload),body: JSON.stringify(payload),});@@ -339,19 +360,21 @@ class RybbitReactNative {}async sendIdentify(userId, traits, isNewIdentify) {+ const userAgent = getUserAgent(this.config.appVersion);try {const response = await this.config.fetch(`${this.config.analyticsHost}/identify`, {method: "POST",- headers: {- "Content-Type": "application/json",- },+ headers: createRequestHeaders({+ language: getLanguage(),+ user_agent: userAgent,+ }),body: JSON.stringify({site_id: this.config.siteId,anonymous_id: this.anonymousId,user_id: userId,traits,is_new_identify: isNewIdentify,- user_agent: getUserAgent(this.config.appVersion),+ user_agent: userAgent,}),});diff --git a/react-native/package.json b/react-native/package.jsonindex 4de59b1dc..97af05da5 100644--- a/react-native/package.json+++ b/react-native/package.json@@ -1,6 +1,6 @@{"name": "@rybbit/react-native",- "version": "0.1.0",+ "version": "0.1.1","description": "React Native analytics SDK for Rybbit","main": "index.js","types": "index.d.ts",diff --git a/server/src/services/tracker/botBlocking/headerHeuristics.test.ts b/server/src/services/tracker/botBlocking/headerHeuristics.test.tsindex 8012c2006..0e73f3e27 100644--- a/server/src/services/tracker/botBlocking/headerHeuristics.test.ts+++ b/server/src/services/tracker/botBlocking/headerHeuristics.test.ts@@ -20,6 +20,32 @@ describe("header heuristic bot detection", () => {});});+ it("detects default OkHttp React Native requests as scripting framework traffic", () => {+ const result = detectBot(requestWithHeaders({}), "okhttp/4.12.0");++ expect(result).toEqual({+ isBot: true,+ score: 5,+ reason: "bot_framework:okhttp",+ });+ });++ it("does not block React Native SDK requests that send SDK HTTP headers", () => {+ const result = detectBot(+ requestWithHeaders({+ accept: "application/json",+ "accept-language": "en-US,en;q=0.9",+ }),+ "Mozilla/5.0 (Linux; Android 36) AppleWebKit/537.36 (KHTML, like Gecko) RybbitReactNative/0.1.1"+ );++ expect(result).toEqual({+ isBot: false,+ score: 2,+ reason: "missing_accept_encoding",+ });+ });+it("scores missing browser headers", () => {const result = detectBot(requestWithHeaders({}), browserUserAgent);diff --git a/server/src/services/tracker/botBlocking/index.test.ts b/server/src/services/tracker/botBlocking/index.test.tsindex eecb1a87b..6d71fe8ea 100644--- a/server/src/services/tracker/botBlocking/index.test.ts+++ b/server/src/services/tracker/botBlocking/index.test.ts@@ -141,6 +141,42 @@ describe("checkBotBlocking", () => {});});+ it("skips browser-shaped layers for mobile sites so native SDK clients are not flagged", async () => {+ // Android React Native ships requests through okhttp with no browser headers.+ // On a web site this trips both the UA-pattern and header-heuristic layers...+ const asWeb = await checkBotBlocking({+ request: requestWithHeaders({ "user-agent": "okhttp/4.12.0" }),+ blockBots: true,+ payload: { ...basePayload, userAgent: "okhttp/4.12.0", clientBotScore: 0, clientBotSignalMask: 0 },+ });+ expect(asWeb).toMatchObject({ isBot: true });++ // ...but a mobile/app site treats it as legitimate first-party traffic.+ const asMobile = await checkBotBlocking({+ request: requestWithHeaders({ "user-agent": "okhttp/4.12.0" }),+ blockBots: true,+ isMobileSite: true,+ payload: { ...basePayload, userAgent: "okhttp/4.12.0", clientBotScore: 0, clientBotSignalMask: 0 },+ });+ expect(asMobile).toBeNull();+ });++ it("still flags mobile traffic through client signals, ASN, and rate anomaly", async () => {+ const result = await checkBotBlocking({+ request: requestWithHeaders({ "user-agent": "okhttp/4.12.0" }),+ blockBots: true,+ isMobileSite: true,+ payload: { ...basePayload, userAgent: "okhttp/4.12.0", clientBotScore: 5 },+ });++ expect(result).toMatchObject({+ isBot: true,+ message: "Bot detected using client signals",+ });+ // The browser-shaped layers stay silent; only the native-applicable layer fires.+ expect(result?.detections.map(detection => detection.layer)).toEqual(["client_signals"]);+ });+it("records client bot score and signal aggregates for inspected requests", async () => {const request = requestWithHeaders(browserHeaders);diff --git a/server/src/services/tracker/botBlocking/index.ts b/server/src/services/tracker/botBlocking/index.tsindex 7ff6789c8..f409d898f 100644--- a/server/src/services/tracker/botBlocking/index.ts+++ b/server/src/services/tracker/botBlocking/index.ts@@ -30,6 +30,12 @@ interface BotBlockingInput {request: FastifyRequest;blockBots: boolean;trustedServerSideIngestion?: boolean;+ /**+ * App/mobile site. The UA-pattern and header-heuristic layers are+ * browser-shaped and produce false positives for native SDK traffic, so they+ * are skipped for mobile sites (see checkBotBlocking).+ */+ isMobileSite?: boolean;payload: BotBlockingPayload;}@@ -181,6 +187,7 @@ export async function checkBotBlocking({request,blockBots,trustedServerSideIngestion = false,+ isMobileSite = false,payload,}: BotBlockingInput): Promise<BotDetectionResult | null> {const userAgent = payload.userAgent || (request.headers["user-agent"] as string) || "";@@ -199,24 +206,32 @@ export async function checkBotBlocking({detections.push(detection);}- // Layer 1: User-agent classification (vendored from isbot patterns, with categories)- const uaClassification = classifyUA(userAgent);- if (uaClassification.isBot) {- addDetection("Bot detected using ua-pattern", {- layer: "ua_pattern",- botCategory: uaClassification.category,- matchedPattern: uaClassification.matchedPattern,- });- }+ // Layers 1 and 2 are browser-shaped: they flag native HTTP clients (okhttp,+ // CFNetwork, Cronet) as scripting frameworks and treat the absence of+ // browser-only headers (Accept-Language, sec-fetch-*) as suspicious. A+ // first-party mobile SDK legitimately looks exactly like that, so skip these+ // layers for app/mobile sites and rely on the client-signal, ASN, and+ // rate-anomaly layers below, which apply equally to native traffic.+ if (!isMobileSite) {+ // Layer 1: User-agent classification (vendored from isbot patterns, with categories)+ const uaClassification = classifyUA(userAgent);+ if (uaClassification.isBot) {+ addDetection("Bot detected using ua-pattern", {+ layer: "ua_pattern",+ botCategory: uaClassification.category,+ matchedPattern: uaClassification.matchedPattern,+ });+ }- // Layer 2: Header heuristic bot detection- const detection = detectBot(request, userAgent);- if (detection.isBot) {- addDetection("Bot detected using header heuristics", {- layer: "header_heuristics",- reason: detection.reason,- score: detection.score,- });+ // Layer 2: Header heuristic bot detection+ const detection = detectBot(request, userAgent);+ if (detection.isBot) {+ addDetection("Bot detected using header heuristics", {+ layer: "header_heuristics",+ reason: detection.reason,+ score: detection.score,+ });+ }}// Layer 3: Client-side and client-derived bot signal score checkdiff --git a/server/src/services/tracker/trackEvent.ts b/server/src/services/tracker/trackEvent.tsindex b845a798a..79a71854b 100644--- a/server/src/services/tracker/trackEvent.ts+++ b/server/src/services/tracker/trackEvent.ts@@ -293,6 +293,7 @@ export async function trackEvent(request: FastifyRequest, reply: FastifyReply) {request,blockBots: siteConfiguration.blockBots,trustedServerSideIngestion,+ isMobileSite: siteConfiguration.type === "mobile",payload: {siteId: validatedPayload.site_id,userAgent: trackingIdentity.userAgent,