Skip to content

fix(webdriver): handle large images on screenshot (fix for 14489 bug)#14491

Merged
christian-bromann merged 2 commits intowebdriverio:mainfrom
nikoslytras:fix-14489
May 18, 2025
Merged

fix(webdriver): handle large images on screenshot (fix for 14489 bug)#14491
christian-bromann merged 2 commits intowebdriverio:mainfrom
nikoslytras:fix-14489

Conversation

@nikoslytras
Copy link
Contributor

@nikoslytras nikoslytras commented May 16, 2025

Related issue

Closes #14489

Proposed changes

Problem Recap

Validating extremely large Base64-encoded strings using a regular expression directly can lead to performance issues or runtime errors such as RangeError: Maximum call stack size exceeded. This typically occurs with very large payloads, such as image data encoded in Base64 (e.g., screenshots with 4+ million characters).

Updated Approach

To address this, we implemented a scalable validation strategy in the isBase64Safe function. The key change is the use of adaptive chunking for large inputs to avoid overwhelming the regex engine.

How It Works:

For small strings (length ≤ BASE_64_SAFE_STRING_TO_PROCESS_LENGTH), the regular expression is applied directly to the entire string.

For larger strings:

The string’s total length is used to determine the number of digits (i.e., its order of magnitude).

The input is split into fixed-size chunks, where each chunk’s size is calculated as:
Math.floor(length / digitCount / 4) * 4
(ensuring chunk lengths are divisible by 4 for valid Base64 structure).

Each chunk is tested individually against the BASE_64_REGEX.

If any chunk fails validation, the entire string is considered invalid.

If all chunks pass, the string is considered valid.

Benefits

Scalable: Handles very large Base64 strings without risking stack overflows or regex engine limitations.

Efficient: Reduces validation cost by applying regex in smaller, manageable chunks.

Safe: Ensures accurate Base64 validation while preserving application stability, even for very large inputs.

This enhancement allows the system to safely validate large, complex Base64 strings—such as those generated from high-resolution screenshots—without compromising performance or reliability.

Types of changes

  • Polish (an improvement for an existing feature)
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update (improvements to the project's docs)
  • Specification changes (updates to WebDriver command specifications)
  • Internal updates (everything related to internal scripts, governance documentation and CI files)

Checklist

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)
  • I have added proper type definitions for new commands (if appropriate)

Backport Request

//: # (The current main branch is the development branch for WebdriverIO v9. If your change should be released to the current major version of WebdriverIO (v8), please raise another PR with the same changes against the v8 branch.)

  • This change is solely for v9 and doesn't need to be back-ported
  • Back-ported PR at #XXXXX

Further comments

Reviewers: @webdriverio/project-committers

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented May 16, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: christian-bromann / name: Christian Bromann (b869edc)
  • ✅ login: nikoslytras / name: Nikos Lytras (e623928)

@erwinheitzman
Copy link
Member

Thank you for the awesome work 👏

Could you add some unit tests for these changes so we know it is and keeps working as intended in the future? 😉

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment, also could we add a unit test for this?

@nikoslytras nikoslytras force-pushed the fix-14489 branch 3 times, most recently from 88bd5b1 to a1e2bd7 Compare May 17, 2025 09:48
@nikoslytras nikoslytras changed the title fix(webdriverio): handle large images on screenshot (fix for 14489 bug) fix(webdriver): handle large images on screenshot (fix for 14489 bug) May 17, 2025
Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment, otherwise LGTM 👍

@pkg-pr-new
Copy link

pkg-pr-new bot commented May 17, 2025

Open in StackBlitz

eslint-plugin-wdio

npm i https://pkg.pr.new/webdriverio/webdriverio/eslint-plugin-wdio@14491

@wdio/allure-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/allure-reporter@14491

@wdio/browser-runner

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/browser-runner@14491

@wdio/appium-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/appium-service@14491

@wdio/browserstack-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/browserstack-service@14491

@wdio/cli

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/cli@14491

@wdio/concise-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/concise-reporter@14491

@wdio/config

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/config@14491

@wdio/cucumber-framework

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/cucumber-framework@14491

@wdio/dot-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/dot-reporter@14491

@wdio/firefox-profile-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/firefox-profile-service@14491

@wdio/globals

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/globals@14491

@wdio/jasmine-framework

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/jasmine-framework@14491

@wdio/json-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/json-reporter@14491

@wdio/junit-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/junit-reporter@14491

@wdio/lighthouse-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/lighthouse-service@14491

@wdio/local-runner

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/local-runner@14491

@wdio/logger

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/logger@14491

@wdio/mocha-framework

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/mocha-framework@14491

@wdio/protocols

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/protocols@14491

@wdio/repl

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/repl@14491

@wdio/reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/reporter@14491

@wdio/runner

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/runner@14491

@wdio/sauce-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/sauce-service@14491

@wdio/shared-store-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/shared-store-service@14491

@wdio/smoke-test-cjs-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/smoke-test-cjs-service@14491

@wdio/smoke-test-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/smoke-test-reporter@14491

@wdio/smoke-test-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/smoke-test-service@14491

@wdio/spec-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/spec-reporter@14491

@wdio/static-server-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/static-server-service@14491

@wdio/sumologic-reporter

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/sumologic-reporter@14491

@wdio/testingbot-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/testingbot-service@14491

@wdio/types

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/types@14491

@wdio/utils

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/utils@14491

@wdio/webdriver-mock-service

npm i https://pkg.pr.new/webdriverio/webdriverio/@wdio/webdriver-mock-service@14491

webdriver

npm i https://pkg.pr.new/webdriverio/webdriverio/webdriver@14491

webdriverio

npm i https://pkg.pr.new/webdriverio/webdriverio@14491

commit: b869edc

@christian-bromann christian-bromann added the PR: Bug Fix 🐛 PRs that contain bug fixes label May 18, 2025
Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@christian-bromann christian-bromann merged commit 954f710 into webdriverio:main May 18, 2025
80 of 81 checks passed
@wdio-bot
Copy link
Contributor

Hey nikoslytras 👋

Thank you for your contribution to WebdriverIO! Your pull request has been marked as an "Expensable" contribution.

We've sent you an email with further instructions on how to claim your expenses from our development fund.
Please make sure to check your spam folder as well. If you have any questions, feel free to reach out to us at expense@webdriver.io or in the contributing channel on Discord.

We are looking forward to more contributions from you in the future 🙌

Have a nice day,
The WebdriverIO Team 🤖

@bhanuagarwal73
Copy link
Contributor

I used to see issue with PNG image.
Thanks for fixing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Expensable $35 💸 PR: Bug Fix 🐛 PRs that contain bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: RangeError: Maximum Call Stack Size Exceeded When Validating Large Base64 Strings

5 participants