-
Notifications
You must be signed in to change notification settings - Fork 0
Add Weak Random testcases #18
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /** | ||
| * OWASP Benchmark Project v1.2 | ||
| * | ||
| * <p>This file is part of the Open Web Application Security Project (OWASP) Benchmark Project. For | ||
| * details, please see <a | ||
| * href="https://owasp.org/www-project-benchmark/">https://owasp.org/www-project-benchmark/</a>. | ||
| * | ||
| * <p>The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms | ||
| * of the GNU General Public License as published by the Free Software Foundation, version 2. | ||
| * | ||
| * <p>The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | ||
| * PURPOSE. See the GNU General Public License for more details. | ||
| * | ||
| * @author Nick Sanidas | ||
| * @created 2015 | ||
| */ | ||
| package org.owasp.benchmark.testcode; | ||
|
|
||
| import java.io.IOException; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| @WebServlet(value = "/weakrand-01/Benchmark00898") | ||
| public class Benchmark00898 extends HttpServlet { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| @Override | ||
| public void doGet(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| doPost(request, response); | ||
| } | ||
|
|
||
| @Override | ||
| public void doPost(HttpServletRequest request, HttpServletResponse response) | ||
| throws ServletException, IOException { | ||
| response.setContentType("text/html;charset=UTF-8"); | ||
|
|
||
| org.owasp.benchmark.helpers.SeparateClassRequest scr = | ||
| new org.owasp.benchmark.helpers.SeparateClassRequest(request); | ||
| String param = scr.getTheValue("Benchmark00898"); | ||
|
|
||
| String bar = ""; | ||
| if (param != null) { | ||
| bar = | ||
| new String( | ||
| org.apache.commons.codec.binary.Base64.decodeBase64( | ||
| org.apache.commons.codec.binary.Base64.encodeBase64( | ||
| param.getBytes()))); | ||
| } | ||
|
|
||
| byte[] bytes = new byte[10]; | ||
| new java.util.Random().nextBytes(bytes); | ||
| String rememberMeKey = org.owasp.esapi.ESAPI.encoder().encodeForBase64(bytes, true); | ||
|
|
||
| String user = "Byron"; | ||
| String fullClassName = this.getClass().getName(); | ||
| String testCaseNumber = | ||
| fullClassName.substring(fullClassName.lastIndexOf('.') + 1 + "Benchmark".length()); | ||
| user += testCaseNumber; | ||
|
|
||
| String cookieName = "rememberMe" + testCaseNumber; | ||
|
|
||
| boolean foundUser = false; | ||
| javax.servlet.http.Cookie[] cookies = request.getCookies(); | ||
| if (cookies != null) { | ||
| for (int i = 0; !foundUser && i < cookies.length; i++) { | ||
| javax.servlet.http.Cookie cookie = cookies[i]; | ||
| if (cookieName.equals(cookie.getName())) { | ||
| if (cookie.getValue().equals(request.getSession().getAttribute(cookieName))) { | ||
| foundUser = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (foundUser) { | ||
| response.getWriter().println("Welcome back: " + user + "<br/>"); | ||
| } else { | ||
| javax.servlet.http.Cookie rememberMe = | ||
| new javax.servlet.http.Cookie(cookieName, rememberMeKey); | ||
| rememberMe.setSecure(true); | ||
| rememberMe.setHttpOnly(true); | ||
| rememberMe.setDomain(new java.net.URL(request.getRequestURL().toString()).getHost()); | ||
| rememberMe.setPath(request.getRequestURI()); // i.e., set path to JUST this servlet | ||
| // e.g., /benchmark/sql-01/Benchmark01001 | ||
| request.getSession().setAttribute(cookieName, rememberMeKey); | ||
| response.addCookie(rememberMe); | ||
| response.getWriter() | ||
| .println( | ||
| user | ||
| + " has been remembered with cookie: " | ||
| + rememberMe.getName() | ||
| + " whose value is: " | ||
| + rememberMe.getValue() | ||
| + "<br/>"); | ||
|
||
| } | ||
|
|
||
| response.getWriter().println("Randomness java.util.Random.nextBytes() executed"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,116 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * OWASP Benchmark Project v1.2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <p>This file is part of the Open Web Application Security Project (OWASP) Benchmark Project. For | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * details, please see <a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * href="https://owasp.org/www-project-benchmark/">https://owasp.org/www-project-benchmark/</a>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <p>The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * of the GNU General Public License as published by the Free Software Foundation, version 2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <p>The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * PURPOSE. See the GNU General Public License for more details. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @author Nick Sanidas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @created 2015 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package org.owasp.benchmark.testcode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import javax.servlet.ServletException; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import javax.servlet.annotation.WebServlet; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import javax.servlet.http.HttpServlet; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import javax.servlet.http.HttpServletRequest; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import javax.servlet.http.HttpServletResponse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @WebServlet(value = "/weakrand-01/Benchmark00899") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class Benchmark00899 extends HttpServlet { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static final long serialVersionUID = 1L; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void doGet(HttpServletRequest request, HttpServletResponse response) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throws ServletException, IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| doPost(request, response); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void doPost(HttpServletRequest request, HttpServletResponse response) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throws ServletException, IOException { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response.setContentType("text/html;charset=UTF-8"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| org.owasp.benchmark.helpers.SeparateClassRequest scr = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new org.owasp.benchmark.helpers.SeparateClassRequest(request); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String param = scr.getTheValue("Benchmark00899"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String bar; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String guess = "ABC"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| char switchTarget = guess.charAt(1); // condition 'B', which is safe | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Simple case statement that assigns param to bar on conditions 'A', 'C', or 'D' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| switch (switchTarget) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 'A': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bar = param; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 'B': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bar = "bob"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 'C': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case 'D': | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bar = param; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bar = "bob's your uncle"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| double value = new java.util.Random().nextDouble(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String rememberMeKey = Double.toString(value).substring(2); // Trim off the 0. at the front. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String user = "Donna"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String fullClassName = this.getClass().getName(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String testCaseNumber = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fullClassName.substring(fullClassName.lastIndexOf('.') + 1 + "Benchmark".length()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| user += testCaseNumber; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String cookieName = "rememberMe" + testCaseNumber; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| boolean foundUser = false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| javax.servlet.http.Cookie[] cookies = request.getCookies(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (cookies != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; !foundUser && i < cookies.length; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| javax.servlet.http.Cookie cookie = cookies[i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (cookieName.equals(cookie.getName())) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (cookie.getValue().equals(request.getSession().getAttribute(cookieName))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foundUser = true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (foundUser) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response.getWriter().println("Welcome back: " + user + "<br/>"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| javax.servlet.http.Cookie rememberMe = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new javax.servlet.http.Cookie(cookieName, rememberMeKey); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Check failureCode scanning / CodeQL Insecure randomness High test
Potential Insecure randomness due to a
Insecure randomness source. Error loading related location Loading
Copilot AutofixAI about 20 hours ago In general, the fix is to replace uses of For this specific file, we should:
Concretely:
Suggested changeset
1
src/main/java/org/owasp/benchmark/testcode/Benchmark00899.java
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rememberMe.setSecure(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rememberMe.setHttpOnly(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rememberMe.setDomain(new java.net.URL(request.getRequestURL().toString()).getHost()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rememberMe.setPath(request.getRequestURI()); // i.e., set path to JUST this servlet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // e.g., /benchmark/sql-01/Benchmark01001 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| request.getSession().setAttribute(cookieName, rememberMeKey); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response.addCookie(rememberMe); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response.getWriter() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .println( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + " has been remembered with cookie: " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + rememberMe.getName() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + " whose value is: " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + rememberMe.getValue() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + "<br/>"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response.getWriter().println("Randomness java.util.Random.nextDouble() executed"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Check failure
Code scanning / CodeQL
Insecure randomness High test
Copilot Autofix
AI about 20 hours ago
To fix the problem, replace the use of
java.util.Randomwith a cryptographically secure RNG such asjava.security.SecureRandomwhen generating the bytes forrememberMeKey. This preserves existing functionality (random remember‑me tokens) while making them unpredictable to an attacker.Concretely, within
doPostinBenchmark00898.java, change the line that currently creates a newjava.util.Randomand callsnextBytesso that it uses ajava.security.SecureRandominstance instead. Since this class doesn’t currently importSecureRandom, add an import forjava.security.SecureRandomat the top of the file. No other logic needs to change:nextBytes(byte[])exists onSecureRandomwith the same signature, and the subsequent ESAPI Base64 encoding and cookie handling remain unchanged.Specifically:
import java.security.SecureRandom;near the other imports.new java.util.Random().nextBytes(bytes);withnew SecureRandom().nextBytes(bytes);.No new helper methods or fields are required; using a local
SecureRandominstance is sufficient for this context.