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

evasive-transform: evade hashbangs #2736

Open
boneskull opened this issue Mar 7, 2025 · 3 comments
Open

evasive-transform: evade hashbangs #2736

boneskull opened this issue Mar 7, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@boneskull
Copy link
Contributor

boneskull commented Mar 7, 2025

Node.js has a carve-out for hashbangs/shebangs at the top of an executable file. This is not valid JS syntax, and as such, SES does not approve.

In @lavamoat/node I had to evade this, but it may be applicable to @endo/evasive-transform at some point.

Example transform (naive):

/**
 * Hashbangs seem to offend SES.
 *
 * @param {string} source
 * @returns {string}
 */
const decapitateHashbang = (source) => {
  // careful with the source map
  return source.replace(/^#!(.+?)(?=\r?\n)/, '// $1')
}

This replaces the #! with //, essentially. It could be rewritten without a RegExp, assuming that hashbangs must always be #!/path/to/some/executable [args] at position 0 in a file. I don't know if that's true, so, either I will need to do my own research or someone can drop science here. I don't know if [args] is actually just [arg], either.

I do know that Node.js will allow hashbangs in files that are not the "main" module; you can require() such a file.

@mhofman
Copy link
Contributor

mhofman commented Mar 8, 2025

This is not valid JS syntax, and as such, SES does not approve.

Actually this is now allowed JS syntax, and considered to be a comment: https://github.com/tc39/proposal-hashbang when at the start of a script or module file.

As such it should be allowed in eval as well, but not in new Function

@boneskull
Copy link
Contributor Author

I thought I recalled something like that! Does this mean, then, that we just need to add support for it in ses or module-source or..?

@mhofman
Copy link
Contributor

mhofman commented Mar 11, 2025

I think it's makes more sense to to do an evasive transform like you have, transforming from #! to // in the limited cases where allowed (which I believe is beginning of source text). I would like to avoid different evaluators for Function and eval, which we'd otherwise require if we were to allow it in SES itself.

@erights @kriskowal thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants