Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(inject): Make pragma detection stricter #1648

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/utils/sourcemaps/inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const SOURCEMAP_DEBUGID_KEY: &str = "debug_id";
const DEBUGID_COMMENT_PREFIX: &str = "//# debugId";

lazy_static! {
static ref USE_PRAGMA_RE: Regex = Regex::new(r#""use \w+";|'use \w+';"#).unwrap();
static ref USE_PRAGMA_RE: Regex = Regex::new(r#"^"use \w+";|^'use \w+';"#).unwrap();
}

fn print_section_with_debugid(
Expand Down Expand Up @@ -516,7 +516,7 @@ something else
//# sourceMappingURL=fake1

// some other comment
"use strict"; rest of the line
"use strict"; rest of the line
'use strict';
some line
//# sourceMappingURL=fake2
Expand All @@ -533,7 +533,7 @@ something else"#;
//# sourceMappingURL=fake1

// some other comment
"use strict"; rest of the line
"use strict"; rest of the line
'use strict';
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="00000000-0000-0000-0000-000000000000")}catch(e){}}();
some line
Expand All @@ -546,6 +546,42 @@ something else
assert_eq!(std::str::from_utf8(&source).unwrap(), expected);
}

#[test]
fn test_fixup_js_file_fake_use_strict() {
let source = r#"#!/bin/node
//# sourceMappingURL=fake1

// some other comment
"use strict"; rest of the line
(this.foo=this.bar||[]).push([[2],[function(e,t,n){"use strict"; […] }
some line
//# sourceMappingURL=fake2
//# sourceMappingURL=real
something else"#;

let debug_id = DebugId::default();

let mut source = Vec::from(source);

fixup_js_file(&mut source, debug_id).unwrap();

let expected = r#"#!/bin/node
//# sourceMappingURL=fake1

// some other comment
"use strict"; rest of the line
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="00000000-0000-0000-0000-000000000000")}catch(e){}}();
(this.foo=this.bar||[]).push([[2],[function(e,t,n){"use strict"; […] }
some line
//# sourceMappingURL=fake2
something else
//# debugId=00000000-0000-0000-0000-000000000000
//# sourceMappingURL=real
"#;

assert_eq!(std::str::from_utf8(&source).unwrap(), expected);
}

#[test]
fn test_normalize_sourcemap_url() {
assert_eq!(
Expand Down