ollien
From the Token docs
The data stored in the token is signed to prevent tampering but not encrypted. This means it is safe to store identification information (such as user IDs) but should not be used to store confidential information (such as credit card numbers).
However, there seem to be two functions one can use on Token, sign/4 (which is mentioned in the examples), and encrypt/4 (which is not).
Looking at the source code for encrypt/4, it seems that this actually uses Plug.Crypto.encrypt/4, which does seem to actually perform crypto with chacha20 (plug_crypto/lib/plug/crypto/message_encryptor.ex at v2.0.0 · elixir-plug/plug_crypto · GitHub - one layer deeper, but this is called directly).
Are the docs wrong, or is it actually unsafe to use Token.encrypt/4 to keep data private while sending it over the wire? If so, why? Obviously, I’m not using this to send credit card numbers or some sort to the client (tbh, signing alone may even be sufficient for my usecase), but I noticed this in the docs and wanted to check
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 4- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
realcorvus
Signing and encrypting are different operations. Token.encrypt is good for keeping data private. Token.sign is not meant to keep data private, rather its so you can verify the integrity of the message sent over the wire. https://stackoverflow.com/questions/454048/what-is-the-difference-between-encrypting-and-signing-in-asymmetric-encryption
ollien
Yes, sorry, I am aware of the difference between signing and encrypting in general, but what was peculiar to me was that the docs say not to use
Tokenfor private data (as the “data stored in the token is signed to prevent tampering but not encrypted”), and yet, there seems to be anencrypt/4function, which would imply it could be used to provide secrecy.Really, the question is: are the docs wrong about the use of the module, or is the
encryptname misleading me?dimitarvp
If you look at the function docs of
Phoenix.Tokenyou’ll see thatencryptdoes the same thing assignbut also encrypts the payload. So by usingencryptyou do not lose any features, integrity or security, you get everything thatsignprovides PLUS encryption – so you can also store data that you don’t want decrypted.It seems rather pointless to encrypt on the server and decrypt on the JS side though; after all, the decrypted data will be then available in plaintext inside the browser and malicious browser addons and 3rd party JS can sniff it out, circumventing your encryption.
The more valuable scenario is to encrypt something on the JS side and send it to the server so as to thwart parties who would modify the payload in-flight (f.ex. amount of money to be withdrawn from an account, or a password).
ollien
Yeah, I saw that, but it contradicts the moduledoc. I guess I’ll open an issue about it. Thanks for clarifying
Definitely, this wouldn’t be valuable. The short answer is: I’m passing some data to the frontend that I need to ensure authenticity of (where signing would be valuable), but I also would prefer to keep the data itself secret from a client as a means of hiding implementation details. Like I said, encryption isn’t a true necessity in my usecase, but the docs made me curious.