Skip to content

Option to remove "sourcesContent" from generated source map file? #956

Description

@umairkhan-dev

Hi, I am obfuscating JS code using javascript-obfuscator CLI with source map enabled. The generated map file contains sourcesContent which in turn contains all the source of obfuscated code. Is there an option to tell javascript-obfuscator not to include source content in map file.

The CLI command I am using.

javascript-obfuscator themes/blue/.source/js --output themes/blue/dist/js --compact true --self-defending true --source-map true

Thanks.

Activity

  1. sanex3339 commented on Jul 18, 2021

    @sanex3339
    Member

    For correct source map work, it should contain one of two fields:

    1. sourcesContent field with original source code content.
    2. sources field that will be used by the browser to download the original source code for source map remapping.

    So, both ways will provide the original source code, and i don't understand how do you want to use it without these fields.

    Probably you should disable source map completely

  2. umairkhan-dev commented on Jul 18, 2021

    @umairkhan-dev
    Author

    I know. I do not have any problem with sources. Just want to remove sourcesContent.

    The scenario is that my source file is in separate directory, which I do not upload. The dev tools may suggest me the line number in case of any log or error, which I can view from source file. However as dev tools can not access source file, the browser can not convert obfuscated code to normal code. But with sourcesContent, the browser does not need source file to convert obfuscated code to normal code. It can just read normal code from map file.

    Hopefully, I am able to explain my scenario.

  3. da411d commented on Jul 18, 2021

    @da411d
    Contributor

    @umairkhan-dev try to replace all sourcesContent strings with random data.

    Before:

    {
      "version": 3,
      "sources": [
        "sourceMap"
      ],
      "names": [
        "hi",
        "console"
      ],
      "mappings": "ohRACA,SAASA,EAAT,E,+05CACEC,OAAA,C,yCAAA,E,mDAAA,C,4yHAEFD,EAAA,G",
      "sourcesContent": [
        "// Paste your JavaScript code here\nfunction hi() {\n  console.log(\"Hello World!\");\n}\nhi();"
      ]
    }
    

    image

    After:

    {
       "version": 3,
       "sources": [
          "sourceMap"
       ],
       "names": [
          "hi",
          "console"
       ],
       "mappings": "0tFACA,SAASA,EAAT,E,6ngBACEC,OAAA,C,gFAAA,E,sFAAA,C,8tKAEFD,EAAA",
       "sourcesContent": [
          "// uu9p6 se6i kqkxw0eb51 1wfl 3mtz\n3csyckp4 q2() {\n  odmmfs0.71f(\"a2l6u lsx8x!\");\n}\nxl();"
       ]
    }
    

    image

    My code:

    const randomLetter = () => Math.random().toString(36).substr(2, 1);
    const map = {...}; // Just copied sourcemap file content
    map.sourcesContent = map.sourcesContent.map(row => row.replace(/\w/ig, randomLetter));
    
  4. sanex3339 commented on Jul 18, 2021

    @sanex3339
    Member

    I'm not sure the best way to fix this.
    Right now all source code is placed into the sourcesContent field of source map.

    I can add a new option sourceMapSourcesMode with 2 values:

    • sourcesContent - default, current behaviour. When enabled adds sources field with fake source named sourceMap and adds sourcesContent field with the original source code
    • sources - When enabled - only adds sources field with source description of original source code file
  5. umairkhan-dev commented on Jul 19, 2021

    @umairkhan-dev
    Author

    I can add a new option sourceMapSourcesMode with 2 values:

    * `sourcesContent` - default, current behaviour. When enabled adds `sources` field with fake  source named `sourceMap` and adds `sourcesContent` field with the original source code
    
    * `sources` - When enabled - only adds `sources` field with source description of original source code file
    

    @sanex3339 May I suggest:

    javascript-obfuscator .source/js --output js --source-map true --source-map-include-sources false
    

    sourceMapIncludeSources

    Type: boolean Default: true

    ⚠️ This option forcibly enables sourceMap option.

    Whether you want to include the contents of source files in the source map as sourcesContent property or not.

  6. da411d commented on Jul 19, 2021

    @da411d
    Contributor

    Okay, I didn't think that you can just remove sourcesContent field

  7. umairkhan-dev commented on Jul 19, 2021

    @umairkhan-dev
    Author

    @da411d Yup, For the time being, I am manually removing / deleting sourcesContent property from map file.

  8. sanex3339 commented on Jul 19, 2021

    @sanex3339
    Member

    I personally prefer my variant with sourceMapSourcesMode option. Will implement it this week.

  9. sanex3339 commented on Jul 19, 2021

    @sanex3339
    Member
  10. sanex3339 commented on Jul 19, 2021

    @sanex3339
    Member

    Released as 2.17.0.
    For CLI use the new option --source-map-sources-mode sources
    When using NodeJS API when sourceMapSourcesMode option has sources value, inputFileName option is required

  11. sanex3339 commented on Jul 19, 2021

    @sanex3339
    Member

    Feel free to ask about the new option here or create a new issue.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions