rebrowser-patches

repository·main·Indexed 23 days ago

https://github.com/rebrowser/rebrowser-patches

A collection of source-code level patches for Puppeteer and Playwright designed to mitigate automation detection vectors and leaks used by anti-bot services like Cloudflare and DataDome. It includes a patcher CLI for applying and reversing modifications, support for custom environment variables to configure Runtime.enable fixes, sourceURL, and utility world names, and provides pre-patched drop-in replacements for browser automation packages.

Tokens
1.8K
Snippets
4
Records
14
Agent score
80%

What's inside rebrowser-patches

  1. Install rebrowser-puppeteer via npm alias

    main

    To use rebrowser patches without changing your source code, alias your existing Puppeteer dependencies in package.json. Replace your current Puppeteer entries with the following (ensure you use the version compatible with your current setup, e.g., ^23.3.1):

    "puppeteer": "npm:rebrowser-puppeteer@^23.3.1",
    "puppeteer-core": "npm:rebrowser-puppeteer-core@^23.3.1"

    Then run npm install or yarn install.

  2. Install rebrowser-playwright via npm alias

    main

    To use rebrowser patches without changing your source code for Playwright (Node.js), alias your existing Playwright dependencies in package.json. Replace your current Playwright entries with the following:

    "playwright": "npm:rebrowser-playwright@^version",
    "playwright-core": "npm:rebrowser-playwright-core@^version"

    Then run npm install or yarn install.

  3. Configure Playwright patches via environment variables

    main

    Playwright support includes patches for the Runtime.enable leak (supporting addBinding and alwaysIsolated modes) and the ability to change the utility world name.

    Note: These patches currently only work for Chrome.

    Debugging Tip: The page.pause() method may not work when the Runtime.enable fix is active. To disable the fix entirely for debugging, set the REBROWSER_PATCHES_RUNTIME_FIX_MODE environment variable to 0.

  4. Patch Playwright drivers for Java, Python, or .NET

    main

    Since Java, Python, and .NET versions of Playwright are wrappers around the Node.js driver, you must patch the driver folder directly.

    1. Locate the driver folder inside your Playwright package.
    2. Run the patcher using the --packagePath flag pointing to the driver's platform-specific package folder:
    npx rebrowser-patches@latest patch --packagePath $yourDriverFolder/$yourPlatform/package
  5. Configure `sourceURL` and `utilityWorldName` via environment variables

    main

    You can customize how scripts and utility worlds appear to the browser to avoid detection patterns.

    Change sourceURL

    By default, Puppeteer adds //# sourceURL=pptr:.... This patch changes it to a generic name or disables it.

    • REBROWSER_PATCHES_SOURCE_URL=<filename>: Set a custom filename (e.g., jquery.min.js).
    • REBROWSER_PATCHES_SOURCE_URL=0: Completely disable the patch.

    Change Utility World Name

    Changes the default utility world name from the standard Puppeteer pattern to a custom string.

    • REBROWSER_PATCHES_UTILITY_WORLD_NAME=<name>: Set a custom name (e.g., customUtilityWorld).
    • REBROWSER_PATCHES_UTILITY_WORLD_NAME=0: Completely disable the patch.

    Note: REBROWSER_PATCHES_UTILITY_WORLD_NAME must be set before the module is imported and cannot be changed on the fly.

  6. Configure the `Runtime.Enable` leak fix via environment variables

    main

    The Runtime.Enable patch prevents anti-bot software from detecting automation via CDP commands. You can switch between different mitigation techniques using the REBROWSER_PATCHES_RUNTIME_FIX_MODE environment variable.

    Available modes:

    • addBinding (Default): Creates a new binding in the main world to save the context ID. Most compatible.
    • alwaysIsolated: Runs all scripts in an isolated context via Page.createIsolatedWorld. Prevents detection via MutationObserver but limits access to main context variables.
    • enableDisable: Calls Runtime.Enable followed immediately by Runtime.Disable to catch context IDs. Low risk, but a tiny window for detection exists.
    • 0: Completely disables the fix.

    Debugging:

    • Set REBROWSER_PATCHES_DEBUG=1 to enable debugging messages.
  7. Use `rebrowser-puppeteer` with `puppeteer-extra`

    main

    To integrate rebrowser-puppeteer-core with the puppeteer-extra framework, use the addExtra method to wrap the rebrowser instance.

    // before
    import puppeteer from 'puppeteer-extra'
    
    // after
    import { addExtra } from 'puppeteer-extra'
    import rebrowserPuppeteer from 'rebrowser-puppeteer-core'
    const puppeteer = addExtra(rebrowserPuppeteer)
  8. Access the browser CDP connection via `_connection()`

    main

    The patch adds a _connection() method to the Browser class, allowing you to access the CDP session at the browser level. This is useful for implementing custom CDP commands and is not detectable by external website scripts.

    browser._connection().on('Rebrowser.addRunEvent', (params) => { ... });
    browser._connection().on('Rebrowser.addRunEvent', (params) => { ... })
  9. Configure the patcher CLI target directory

    main

    When using the patcher CLI, you can control which code directory is targeted for patching using the codeTarget option. This is useful if the package you are patching stores its primary logic in src instead of the default lib.

    • codeTarget: src | lib (default: lib)
  10. Use rebrowser drop-in replacements for Puppeteer and Playwright

    main

    Instead of manually applying patches, you can use pre-patched versions of Puppeteer and Playwright. These packages are identical to the originals but have rebrowser-patches already applied.

    This method allows you to keep your existing source code unchanged by aliasing the original package names to the rebrowser versions in package.json.

    1. Open package.json.
    2. Replace `
  11. Fix 'patch' command not found on Windows

    main

    If you encounter an error stating the patch command is not found on Windows, you must install Git (which includes patch.exe) and add the Git usr/bin directory to your system PATH.

    To add it via the command line:

    set PATH=%PATH%;C:\\