fix: fix with protected string issue - visibility, concat expressions and escape sequences #710
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fix String Protection
bytecodePlugin
#552, in some editors it's very easy to identify the protected string. Inspired by subframe7536, we can add obfuscation to fix issue and improve the difficulty of cracking while considering performance(I'll illustrate the principles and improvements later)."a" + "b"
) to ensure merged strings are also protected."\n"
,"\t"
, ...) to ensure this special characters are also protected.Additional context
Reproduction
When I was trying to define a CA cert and client cert in string and protect them, I double checked the generated
.jsc
file, and find it's easy to identify them in VSCode.Failure Example
String Variable with
concatenation expressions
(also mentioned in #455):String Variable with
Template Strings
:String Variable with
Escape Sequences
:String Variable with
Ternary Expression
, or we can say, any expression:Then, it goes with no effect:

Then I try simple string like:
It works, but seems easy to identify, just like who described in #552.

WHY THIS HAPPENS?
We use
String.fromCharCode(...)
correct? This is exactly the key issue. The old protection method simply converted the target string into a series of fixed character codes and used String.fromCharCode(...) to restore it at runtime. As a result:So when we open many different editors, this part is particularly prominent and eye-catching. Therefore, although the protection process transforms the source code, the generated code pattern is overly simple and fixed. This makes it easy for V8 to recognize and restore the string after compilation - which means, we have find another approach to do at least low level obfuscation.
Secondly, let's talk about why
Expressions
andEscape Sequences
does not work. In my guessing:However, based on my experience and existing examples, combined with a developer's mindset, it is still not recommended to use template literals or complex expressions for protected strings. These should be pre-generated in advance.
Inspired by subframe7536, I find this approach works well. Although a fixed offset is applied to each character, the small range of offset values (approximately 1–32) means that the same input always produces the same encrypted result, making it vulnerable to static analysis or pattern-based decryption. To address this, I did a little revise - I introduced a 4-byte random key and combined addition with XOR operations, ensuring that each character's obfuscation result is not solely dependent on the input. For V8/JIT optimization, the new function utilizes an inline IIFE. While this increases the number of computation steps slightly, it has minimal impact on most use cases. The resulting
.jsc
file size does increase, but this is a trade-off that comes as a necessary side effect of the enhanced protection.Supported Types of Expression After Modification
Thus I only modify the
transform
function and add support only for this types of expression, and util now it has been tested well(manually) and performed well.const str = "abcd";
const str = "ab" + "cd";
const str = "ab\ncd";
const str = "ab\t" + 'cd' + "\n" + "efg";
Unsupported usage
const ABC = "ABCD" + (condition ? "E" : "F");
const ABC = "ABC" + someString
These examples would cover most of cases during our development. I strongly recommend the maintainers mention these cases in official documentation to help community users quickly understand supported and unsupported usages. After the modification, it shows like:

That's it. Hope electron-vite will become more and more powerful! This is the best framework for electron and vite that I have ever used.
What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).