How to Safely Use Crypto.subtle Property for Local Testing Without Security Risks?

37 Views Asked by At

I'm developing a web application that relies on cryptographic operations for tasks such as hashing, encryption, and decryption. I've found the Crypto.subtle property in JavaScript to be quite useful for these purposes. However, I'm wondering about the safest approach to utilize it for local testing without compromising security.

During development and testing phases, I often work in local environments where security isn't a major concern. However, I want to ensure that my use of Crypto.subtle doesn't introduce any potential security risks, especially when deploying the application to production environments.

Could anyone recommend a best practice or method for safely using Crypto.subtle in local testing environments while ensuring security remains intact when the application is deployed to production? Any insights, tips, or examples would be greatly appreciated. Thank you!

1

There are 1 best solutions below

2
Maarten Bodewes On

However, I'm wondering about the safest approach to utilize it for local testing without compromising security.

Cryptography usually doesn't introduce too many side effects. It operates on the key and data you provide, and for most functions the key and data isn't even altered. For instance, for a cipher, you put in a key, IV and plaintext and it outputs ciphertext. In that sense it is relatively isolated from the rest of your application.

However, I want to ensure that my use of Crypto.subtle doesn't introduce any potential security risks, especially when deploying the application to production environments.

What kind of security risk are you afraid of? There is no such thing as generic security. Sometimes the CIA triangle consisting of confidentiality, integrity and availability is used to have a raw split down. But since CIA doesn't even cover the entire security spectrum (access control for critical functionality is where exactly?) I would not use that.

All in all, either you need to know which assets you are protecting, which adversaries are expected, do a risk analysis (etc.) or you need to follow standard security practices like OWASP and try to protect against a spectrum. If you're not a security expert that later direction should probably be preferred.

Could anyone recommend a best practice or method for safely using Crypto.subtle in local testing environments while ensuring security remains intact when the application is deployed to production? Any insights, tips, or examples would be greatly appreciated.

SubtleCrypto is a low level cryptographic library. That means it can be used to implement any protocol. For that you first need to create a protocol specification, even if it is just application specific. That way you have a central specification that can be evaluated. Cryptographic protocols are not as hard as creating your own cipher, but it is still extremely easy to shoot yourself in the foot if you don't know what you are doing. So you first eval the protocol and then the implementation of it.

The main issue with SubtleCrypto is that it doesn't contain clear indications on how to perform key management. Key management is very important in any cryptographic protocol. Unfortunately SubtleCrypto does not - to my knowledge - give any access to e.g. a central key store containing trusted public keys or certificates. Fortunately TLS is commonly used nowadays, which means that at least the server is generally authenticated and the data is send over a secured connection. So the code using SubtleCrypto only needs to provide additional security on top of TLS.

Unfortunately JavaScript is generally not able to get much information about the TLS connection nor of the certificate / private key used by the server to authenticate to the browser. Anyway, if you want to have any focus on security then key management should be a major concern.

Personally I would not overly rely on SubtleCrypto or indeed JavaScript for my security solution. It should be secure regardless of the code running in the browser. If the browser is compromised then the user is likely in trouble regardless of any SubtleCrypto code running.