-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Javascript: Regex Global Flag in Test Function #15163
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?
Conversation
I will create issue on |
QHelp previews: javascript/ql/src/experimental/Security/CWE-020/RegexValidation.qhelpRegex Global Flag in Test FunctionThe use of the global flag in regular expressions in JavaScript can lead to unexpected behaviors in the test function. This issue arises because the global regex maintains its last index position across multiple calls, resulting in the test function sometimes returning true and other times false for the same input. RecommendationTo avoid this issue, it is recommended to either avoid using the global flag with the test method or reset the lastIndex of the regular expression to 0 before each test call. Alternatively, use the match method for scenarios where global search is required. ExampleVulnerable code example: Using a global regex in a test function without resetting lastIndex. The function may return inconsistent results over repeated calls. import { Request, Response, Application } from 'express';
import express from 'express';
import db from './postgres';
const FORBIDDEN_CHARS = /['\\]/g;
const isForbidden = (str: string) => FORBIDDEN_CHARS.test(str);
const app: Application = express();
app.get('/api/users/:name', async (req: Request, res: Response) => {
const { name } = req.params;
if (isForbidden(name)) {
return res.sendStatus(400);
}
const user = await db.query(`SELECT * FROM users WHERE name='${name}'`);
res.json(user);
});
app.listen(1337); ExampleSecure code example: Resetting the lastIndex of the regex to 0 before each test call or using match for global searches. import { Request, Response, Application } from 'express';
import express from 'express';
import db from './postgres';
const FORBIDDEN_CHARS = /['\\]/;
const isForbidden = (str: string) => FORBIDDEN_CHARS.test(str);
const app: Application = express();
app.get('/api/users/:name', async (req: Request, res: Response) => {
const { name } = req.params;
if (isForbidden(name)) {
return res.sendStatus(400);
}
const user = await db.query(`SELECT * FROM users WHERE name='${name}'`);
res.json(user);
});
app.listen(1337); References
|
Please create that issue first. It makes the process for us easier. |
@erik-krogh I created the issue thank you. |
Hello aydinnyunus 👋 In the meantime, feel free to make changes to the pull request. If you'd like to maximize payout for your this and future submissions, here are a few general guidelines, that we might take into consideration when reviewing a submission.
Please note that these are guidelines, not rules. Since we have a lot of different types of submissions, the guidelines might vary for each submission. Happy hacking! |
Thank you. I am waiting for your answer <3 |
Pull Request: Add Regex Global Flag in Test Function Query for CodeQL
Overview
This pull request adds a new CodeQL query designed to detect issues related to the use of the global flag (
g
) in regular expressions within JavaScript and TypeScript codebases. This query focuses on identifying instances where the global flag might lead to inconsistent or erroneous behavior, particularly when used in conjunction with thetest
method of RegExp objects. The goal is to help developers identify and rectify potential bugs in their code related to global regular expressions.Changes Introduced
RegexValidation.ql
- Identifies potentially problematic uses of the global flag in regular expressions, especially when used intest
method calls./CWE-020
directory, showcasing both problematic (bad) and corrected (good) usage scenarios.Implementation Details
RegExp
literals with a global flag and their use intest
method calls.lastIndex
property.Testing and Validation
Future Work
References