Create rule S2053: Password hashing functions should use an unpredictable salt (#4648)

* Add go to rule S2053

* Add description for S2053 for Go

---------

Co-authored-by: daniel-teuchert-sonarsource <daniel-teuchert-sonarsource@users.noreply.github.com>
Co-authored-by: Daniel Teuchert <daniel.teuchert@sonarsource.com>
Co-authored-by: daniel-teuchert-sonarsource <141642369+daniel-teuchert-sonarsource@users.noreply.github.com>
This commit is contained in:
github-actions[bot] 2025-02-06 13:14:34 +01:00 committed by GitHub
parent 1f6167eb91
commit d9e29030ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,2 @@
{
}

71
rules/S2053/go/rule.adoc Normal file
View File

@ -0,0 +1,71 @@
include::../summary.adoc[]
== Why is this an issue?
include::../rationale.adoc[]
include::../impact.adoc[]
include::../exceptions.adoc[]
// How to fix it section
== How to fix it
=== Code examples
include::../common/fix/code-rationale.adoc[]
==== Noncompliant code example
[source,go,diff-id=1,diff-type=noncompliant]
----
import (
"crypto/sha256"
"golang.org/x/crypto/pbkdf2"
)
func example(password []byte) {
pbkdf2.Key(password, []byte("fixedSalt"), 4096, 32, sha256.New) // Noncompliant
}
----
==== Compliant solution
[source,go,diff-id=1,diff-type=compliant]
----
import (
"crypto/rand"
"crypto/sha256"
"golang.org/x/crypto/pbkdf2"
)
func example(password []byte) {
randomSalt := make([]byte, 32)
rand.Read(randomSalt)
pbkdf2.Key(password, randomSalt, 4096, 32, sha256.New)
}
----
=== How does this work?
include::../common/fix/salt.adoc[]
Here, the compliant code example ensures the salt is random and has a sufficient
length by calling the `crypto.rand.Read` function. This function internally
uses a cryptographically secure pseudo-random number generator.
== Resources
include::../common/resources/standards.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
endif::env-github,rspecator-view[]