Earlier this year, I was asked to evaluate using JavaScript to encrypt sensitive information in the user's browser before sending it to our server. While my first response was "ask an expert", my boss was adamant so I went ahead and researched expert opinions on the topic as well as I could. Based on that research I came back to him with a resounding "no expert thinks that's a good idea" and was able to convince him, but I wasn't able to actually specifically answer some of his rebuttals for REASONS it was a bad idea. Specifically the main reasons I found for JavaScript in-browser encryption being bad were:
- Just use TLS in the first place, it's 100% certain to be better
- JavaScript doesn't offer cryptographically secure PRNG
- Any Man-In-The-Middle attacker would just modify the encryption code, rendering it useless
However, the specific use-case I was investigating mitigated those:
- We were already planning to enforce TLS, with the JavaScript portion only intended to cover for future TLS vulnerabilities
- We would generate an RSA key-pair elsewhere and send the public key with the encryption code, rather than needing the client to generate or send keys of its own
- Although it wouldn't protect against an attacker which could modify our javascript, we could still protect against vulnerabilities that allow only READING messages
I wasn't able to come up with a real answer to the last argument in particular, and could only fall back to "overwhelming consensus". I can think of some POSSIBLE arguments, but wasn't able to definitively support them:
- Encryption inside of TLS adds no value in the first place
- Read-only vulnerabilities in TLS aren't realistic, any crack would inherently allow modification as well
- Even without modifying the original encryption code, an attacker could inject JavaScript in a different manner and prevent encryption that way
- It could add a modest amount of security, but the effort and knowledge required to create and maintain it would be prohibitive
TL;DR: The main arguments I've seen against JavaScript encryption are 1) use TLS instead and 2) JavaScript doesn't generate secure keys. If TLS is also used, and keys are generated elsewhere, does JavaScript encryption start adding value?'
Note: In response to a couple comments, the use-case here is NOT end-to-end encryption; the server needs to, at least in some situations, read the original data.