Search code examples
javascripthmaccryptojs

HMAC in client side JavaScript and identity spoofing


CryptoJS has functions to create HMAC from a message and the secret key.

How can this be secure considering that the secret key must be stored in plain sight in the JavaScript source deployed on the client ?

Anyone can take the key and issue similar requests to the server under the identity of the original client of the API. Isn't "identity" the problem that HMAC is supposed to solve ?

All in all, I do not understand the purpose of HMAC in client side JS since the key can't be kept secret.

Is there a use case to computing HMAC in JavaScript ?


Solution

  • JavaScript now has WebRTC where two clients can communicate peer-to-peer, this would be a scenario where clients can generate and use their own "secret".

    There are some cases where client -> server could be usable as well. If your server was "dynamically" serving the JavaScript then it could insert a "secret" based on the clients current session/login. Assuming you are using HTTPS (if not there could be a man in the middle slurping up the "secret") then it's not unreasonable to assume that communication to the server signed with that specific "secret" (even over unsecured HTTP) belongs to only that client.