Skip to content

Commit 1d1b9d9

Browse files
fix(inject): Skip non-base64 embedded sourcemaps (#3243)
When you bundle tools like terser or babel into a single JS file (common in browser-based IDEs or bundler workers), their minified code contains template literals that build `//# sourceMappingURL=data:application/json;base64,...` strings as output. After minification, these can land at the start of a line, and `discover_sourcemaps_location` treats them as real sourcemap directives. The problem is the "base64" payload is actually just more JS code, so decoding fails and `sourcemaps inject` bails with `Invalid embedded sourcemap`, stopping the whole injection. This changes the `bail!` to a `warn!` + `continue`, which matches how the injection flow already handles other non-fatal cases like missing sourcemaps, ambiguous paths, and unfound files. The file gets skipped and everything else processes normally. Includes a test with a minimal fixture that reproduces the issue. ### Issues #3244 ### Legal Boilerplate Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms. --------- Co-authored-by: Sebastian Zivota <loewenheim@mailbox.org>
1 parent 06cbbda commit 1d1b9d9

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- (sourcemaps) Skip non-base64 embedded sourcemaps during injection ([#3243](https://github.com/getsentry/sentry-cli/pull/3243))
8+
59
### New Features ✨
610

711
- Add `sentry-cli build download` command to download installable builds (IPA/APK) by build ID ([#3221](https://github.com/getsentry/sentry-cli/pull/3221)).

src/utils/sourcemaps.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,12 @@ impl SourceMapProcessor {
790790

791791
let Ok(mut decoded) = data_encoding::BASE64.decode(encoded.as_bytes())
792792
else {
793-
bail!("Invalid embedded sourcemap in source file {source_url}");
793+
// The data URL isn't valid base64, so we can't use it.
794+
// This commonly happens when minified bundles contain
795+
// sourceMappingURL strings inside template literals
796+
// (e.g from bundled terser or babel)
797+
warn!("Skipping {source_url}: embedded sourcemap is not valid base64");
798+
continue;
794799
};
795800

796801
let mut sourcemap =
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var x = 1 + 2;
2+
var output =
3+
code +
4+
`
5+
//# sourceMappingURL=data:application/json;base64,` +
6+
btoa(json);
7+
console.log(x);

tests/integration/sourcemaps/inject.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,29 @@ fn command_sourcemaps_inject_malformed_map() {
241241
);
242242
}
243243

244+
#[test]
245+
fn command_sourcemaps_inject_sourcemap_url_in_string_literal() {
246+
// When tools like terser or babel are bundled into a worker, their minified
247+
// code contains string concatenations that produce sourceMappingURL comments.
248+
// After minification these can land at column 0, making them look like real
249+
// sourcemap directives. sentry-cli should not treat them as embedded
250+
// sourcemaps and should not fail.
251+
let testcase_cwd_path =
252+
"tests/integration/_cases/sourcemaps/sourcemaps-inject-sourcemap-in-string-literal.in/";
253+
if std::path::Path::new(testcase_cwd_path).exists() {
254+
remove_dir_all(testcase_cwd_path).unwrap();
255+
}
256+
copy_recursively(
257+
"tests/integration/_fixtures/inject_sourcemap_in_string_literal/",
258+
testcase_cwd_path,
259+
)
260+
.unwrap();
261+
262+
TestManager::new()
263+
.assert_cmd(vec!["sourcemaps", "inject", testcase_cwd_path])
264+
.run_and_assert(AssertCommand::Success);
265+
}
266+
244267
/// Recursively assert that the contents of two directories are equal.
245268
///
246269
/// We only support directories that contain exclusively text files.

0 commit comments

Comments
 (0)