rspec/rules/S5659/java/rule.adoc

78 lines
2.0 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
include::../description.adoc[]
2021-01-23 04:07:47 +00:00
=== Noncompliant code example
2021-01-23 04:07:47 +00:00
Using https://github.com/jwtk/jjwt[jwtk/Java JWT] library (to verify a signed token (containing a JWS) don't use the ``++parse++`` method as it doesn't throw an exception if an unsigned token is provided):
2021-01-23 04:07:47 +00:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-01-23 04:07:47 +00:00
----
// Signing:
2021-01-23 04:07:47 +00:00
io.jsonwebtoken.Jwts.builder() // Noncompliant, token is not signed.
.setSubject(USER_LOGIN)
.compact();
// Verifying:
io.jsonwebtoken.Jwts.parser().setSigningKey(SECRET_KEY).parse(token).getBody(); // Noncompliant
2021-01-23 04:07:47 +00:00
----
Using https://github.com/auth0/java-jwt[auth0/Java JWT] library:
2022-02-04 17:28:24 +01:00
[source,java]
2021-01-23 04:07:47 +00:00
----
// Signing:
2021-01-23 04:07:47 +00:00
com.auth0.jwt.JWT.create()
.withSubject(SUBJECT)
.sign(Algorithm.none()); // Noncompliant, use only strong cipher algorithms when signing this JWT.
// Verifying:
JWTVerifier nonCompliantVerifier = com.auth0.jwt.JWT.require(Algorithm.none()) // Noncompliant
.withSubject(LOGIN)
.build();
----
=== Compliant solution
2021-01-23 04:07:47 +00:00
Using https://github.com/jwtk/jjwt[Java JWT] library (to verify a signed token (containing a JWS) use the ``++parseClaimsJws++`` method that will throw an exception if an unsigned token is provided):
2021-01-23 04:07:47 +00:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-01-23 04:07:47 +00:00
----
// Signing:
2021-01-23 04:07:47 +00:00
Jwts.builder() // Compliant
.setSubject(USER_LOGIN)
.signWith(SignatureAlgorithm.HS256, SECRET_KEY)
.compact();
// Verifying:
Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody(); // Compliant
----
Using https://github.com/auth0/java-jwt[auth0/Java JWT] library. I
2021-01-23 04:07:47 +00:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-01-23 04:07:47 +00:00
----
// Signing:
2021-01-23 04:07:47 +00:00
JWT.create()
.withSubject(SUBJECT)
2021-05-21 01:24:06 +00:00
.sign(Algorithm.HMAC256(SECRET_KEY)); // Compliant
2021-01-23 04:07:47 +00:00
// Verifying:
2021-05-21 01:24:06 +00:00
JWTVerifier nonCompliantVerifier = JWT.require(Algorithm.HMAC256(SECRET_KEY)) // Compliant
2021-01-23 04:07:47 +00:00
.withSubject(LOGIN)
.build();
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]