2024-10-29 10:36:18 +01:00
include::../description.adoc[]
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
As the ``++Math.random()++`` function relies on a weak pseudorandom number generator, this function should not be used for security-critical applications or for protecting sensitive data. In such context, a cryptographically strong pseudorandom number generator (CSPRNG) should be used instead.
2020-06-30 12:48:07 +02:00
include::../ask-yourself.adoc[]
== Recommended Secure Coding Practices
2024-10-29 10:36:18 +01:00
* Use a cryptographically secure pseudorandom number generator (CSPRNG) like ``++crypto.getRandomValues()++``.
2020-06-30 12:48:07 +02:00
* Use the generated random values only once.
* You should not expose the generated random value. If you have to store it, make sure that the database or file is secure.
== Sensitive Code Example
----
const val = Math.random(); // Sensitive
// Check if val is used in a security context.
----
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-06-30 12:48:07 +02:00
----
// === Client side ===
const crypto = window.crypto || window.msCrypto;
var array = new Uint32Array(1);
2024-10-29 10:36:18 +01:00
crypto.getRandomValues(array);
2020-06-30 12:48:07 +02:00
// === Server side ===
const crypto = require('crypto');
2024-10-29 10:36:18 +01:00
const buf = crypto.randomBytes(1);
2020-06-30 12:48:07 +02:00
----
include::../see.adoc[]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 8 Dec 2018, 19:30:39 Lars Svensson wrote:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto
https://nodejs.org/api/crypto.html
include::../comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]