forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 21
JWT token created with none algorithm #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
07dfa9b
Add JWT none algorithm detection query for PowerShell
chanel-y 1f0f8e5
changed to just detect the .net usage, updated tests, added qhelp
chanel-y 20f33c9
Merge branch 'main' into users/chanely/jwt-none-algorithm
chanel-y d3c338e
Merge branch 'main' into users/chanely/jwt-none-algorithm
chanel-y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
powershell/ql/src/queries/security/cwe-347/JwtNoneAlgorithm.qhelp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
| <overview> | ||
|
|
||
| <p>Using the <code>"none"</code> algorithm when creating a JWT token with | ||
| <code>JwtSecurityTokenHandler</code> disables signature verification. This allows attackers to | ||
| forge arbitrary tokens that will be accepted as valid, potentially leading to authentication | ||
| bypass and privilege escalation.</p> | ||
|
|
||
| </overview> | ||
| <recommendation> | ||
|
|
||
| <p>Always use a secure signing algorithm such as <code>HS256</code>, <code>RS256</code>, or | ||
| <code>ES256</code> when creating JWT tokens. Ensure that tokens are signed with a strong secret | ||
| or key pair.</p> | ||
|
|
||
| </recommendation> | ||
| <example> | ||
|
|
||
| <p>In this example, a JWT token is created using the <code>"none"</code> algorithm, which produces | ||
| an unsigned token:</p> | ||
|
|
||
| <sample src="examples/JwtNoneAlgorithmBad.ps1" /> | ||
|
|
||
| <p>The fix is to use a secure algorithm instead:</p> | ||
|
|
||
| <sample src="examples/JwtNoneAlgorithmGood.ps1" /> | ||
|
|
||
| </example> | ||
| <references> | ||
|
|
||
| <li> | ||
| RFC 7518: | ||
| <a href="https://datatracker.ietf.org/doc/html/rfc7518#section-3.6">JSON Web Algorithms - Unsecured JWS</a>. | ||
| </li> | ||
|
|
||
| <li> | ||
| CWE-347: | ||
| <a href="https://cwe.mitre.org/data/definitions/347.html">Improper Verification of Cryptographic Signature</a>. | ||
| </li> | ||
|
|
||
| </references> | ||
| </qhelp> |
37 changes: 37 additions & 0 deletions
37
powershell/ql/src/queries/security/cwe-347/JwtNoneAlgorithm.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /** | ||
| * @name JWT none algorithm usage | ||
| * @description Using the "none" algorithm for JWT tokens disables signature verification, | ||
| * allowing token forgery. | ||
| * @kind problem | ||
| * @problem.severity error | ||
| * @security-severity 9.8 | ||
| * @precision high | ||
| * @id powershell/jwt-none-algorithm | ||
| * @tags security | ||
| * external/cwe/cwe-347 | ||
| */ | ||
|
|
||
| import powershell | ||
| import semmle.code.powershell.dataflow.DataFlow | ||
|
|
||
| /** | ||
| * A string literal "none" passed as an algorithm argument to .NET JWT methods | ||
| * on a JwtSecurityTokenHandler instance. | ||
| */ | ||
| class JwtNoneInDotNetCall extends StringConstExpr { | ||
| JwtNoneInDotNetCall() { | ||
| this.getValueString().toLowerCase() = "none" and | ||
| exists(DataFlow::CallNode cn, DataFlow::ObjectCreationNode ocn | | ||
| cn.getQualifier().getALocalSource() = ocn and | ||
| ocn.getConstructedTypeNode().asExpr().getExpr().(TypeNameExpr).hasQualifiedName("system.identitymodel.tokens.jwt", "jwtsecuritytokenhandler") and | ||
| cn.getLowerCaseName() in [ | ||
| "createtoken", "writetoken", "createjwtsecuritytoken", "createencodedjwt" | ||
| ] and | ||
| cn.getAnArgument().asExpr().getExpr() = this | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| from JwtNoneInDotNetCall noneAlg | ||
| select noneAlg, | ||
| "JWT token created with 'none' algorithm via .NET API, disabling signature verification." |
4 changes: 4 additions & 0 deletions
4
powershell/ql/src/queries/security/cwe-347/examples/JwtNoneAlgorithmBad.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| $handler = [System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler]::new() | ||
|
|
||
| # BAD: Creating a JWT token with the "none" algorithm disables signature verification. | ||
| $token = $handler.CreateToken("none") |
4 changes: 4 additions & 0 deletions
4
powershell/ql/src/queries/security/cwe-347/examples/JwtNoneAlgorithmGood.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| $handler = [System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler]::new() | ||
|
|
||
| # GOOD: Creating a JWT token with a secure algorithm. | ||
| $token = $handler.CreateToken("HS256") |
2 changes: 2 additions & 0 deletions
2
powershell/ql/test/query-tests/security/cwe-347/JwtNoneAlgorithm/JwtNoneAlgorithm.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| | test.ps1:7:31:7:36 | none | JWT token created with 'none' algorithm via .NET API, disabling signature verification. | | ||
| | test.ps1:10:36:10:41 | none | JWT token created with 'none' algorithm via .NET API, disabling signature verification. | |
1 change: 1 addition & 0 deletions
1
powershell/ql/test/query-tests/security/cwe-347/JwtNoneAlgorithm/JwtNoneAlgorithm.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| queries/security/cwe-347/JwtNoneAlgorithm.ql | ||
17 changes: 17 additions & 0 deletions
17
powershell/ql/test/query-tests/security/cwe-347/JwtNoneAlgorithm/test.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # =================================================================== | ||
| # ========== TRUE POSITIVES (should trigger alert) ================== | ||
| # =================================================================== | ||
|
|
||
| # --- Case 1: .NET JwtSecurityTokenHandler.CreateToken with "none" --- | ||
| $handler = [System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler]::new() | ||
| $token = $handler.CreateToken("none") # BAD | ||
|
|
||
| # --- Case 2: .NET JwtSecurityTokenHandler.CreateEncodedJwt with "none" --- | ||
| $token = $handler.CreateEncodedJwt("none") # BAD | ||
|
|
||
| # =================================================================== | ||
| # ========== TRUE NEGATIVES (should NOT trigger alert) ============== | ||
| # =================================================================== | ||
|
|
||
| # --- Safe: .NET CreateToken with HS256 --- | ||
| $token = $handler.CreateToken("HS256") # GOOD |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.