+
+
+
+A JSON Web Token (JWT) has three base64url-encoded segments separated by .: a header, a payload
+with the claims, and a signature. The claims can only be trusted after the signature has been checked against
+a trusted key. Anyone can create a token with any payload, for example one that claims an administrator role.
+
+
+This query reports code that splits a token on . and base64-decodes the payload segment
+(index 1) without evidence that the signature has been verified. Claims that are decoded without
+being authenticated can be forged.
+
+
+
+
+
+Do not parse JWT claims by hand. Use a JWT library such as
+System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken or
+Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ValidateTokenAsync, with
+TokenValidationParameters that require a valid signature, and read the claims from the validated result.
+
+
+If the token was already validated somewhere else, for example by a remote authentication service, read the
+claims from that validated result, not from the raw token string.
+
+
+
+
+
+In the following example, the payload is decoded and the roles claim is trusted without checking the
+signature. An attacker can send a forged token with "roles": ["ADMIN"]:
+
+
+
+
+
+The following example validates the token signature before it reads any claims:
+
+
+
+
+
+
+
+-
+A read of the signature segment (index
2) of the same Split result counts as evidence of
+verification only when it, or its base64-decoded value, reaches a comparison or an argument of a method whose name
+contains verify or equals. The query does not check that the result of that comparison
+is used, or that the comparison is a correct cryptographic check. Logging the segment does not count.
+
+-
+Validation by a library is recognized when a call to
JwtSecurityTokenHandler.ValidateToken on the same
+token dominates the decode, and the TokenValidationParameters object initializer passed to it enables
+ValidateIssuerSigningKey and sets IssuerSigningKey. For
+JsonWebTokenHandler.ValidateTokenAsync, the decode must also be dominated by an
+if (!result.IsValid) check whose branch throws.
+
+-
+Code that has the token validated elsewhere before it decodes the payload, for example by a remote service, by
+middleware, by a custom helper, or with parameters that are configured indirectly, is still reported.
+
+
+
+
+
+CWE: CWE-345: Insufficient Verification of Data Authenticity.
+CWE: CWE-347: Improper Verification of Cryptographic Signature.
+IETF: RFC 7519: JSON Web Token (JWT).
+OWASP: JSON Web Token Cheat Sheet.
+
+
+
diff --git a/csharp/src/security/CWE-345/UnverifiedJwtPayloadDecode.ql b/csharp/src/security/CWE-345/UnverifiedJwtPayloadDecode.ql
new file mode 100644
index 00000000..4b29cbff
--- /dev/null
+++ b/csharp/src/security/CWE-345/UnverifiedJwtPayloadDecode.ql
@@ -0,0 +1,170 @@
+/**
+ * @name JWT payload decoded without signature verification
+ * @description Base64-decoding the payload segment of a JSON Web Token without verifying
+ * the token signature lets an attacker forge claims such as roles or user
+ * identifiers.
+ * @kind path-problem
+ * @problem.severity error
+ * @security-severity 8.1
+ * @precision medium
+ * @id githubsecuritylab/cs/unverified-jwt-payload-decode
+ * @tags security
+ * experimental
+ * external/cwe/cwe-345
+ * external/cwe/cwe-347
+ */
+
+import csharp
+import semmle.code.csharp.dataflow.TaintTracking
+import UnverifiedJwtPayloadDecodeFlow::PathGraph
+
+/** A call to `string.Split` whose separator is the JWT segment separator `.`. */
+class JwtSplitCall extends MethodCall {
+ JwtSplitCall() {
+ this.getTarget().hasFullyQualifiedName("System", "String", "Split") and
+ exists(Expr sep | sep = this.getArgument(0) |
+ sep.(CharLiteral).getValue() = "."
+ or
+ sep.(StringLiteral).getValue() = "."
+ )
+ }
+
+ /** Holds if `aa` reads segment `index` of the result of this call. */
+ predicate readsSegment(ArrayAccess aa, int index) {
+ DataFlow::localExprFlow(this, aa.getQualifier()) and
+ aa.getIndex(0).getValue().toInt() = index
+ }
+
+ /**
+ * Gets an expression that holds the signature segment, or its decoded value. Local taint
+ * steps follow conversions such as `byte[]` to `ReadOnlySpan