Skip to content

Conversation

@xiemaisi
Copy link

@xiemaisi xiemaisi commented Sep 24, 2018

This is a surprisingly common mistake.

@xiemaisi xiemaisi requested a review from a team as a code owner September 24, 2018 16:01
@xiemaisi xiemaisi added the JS label Sep 24, 2018
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Modulo support for some additional regex features.

Should this query be added to a query suite?

*/
predicate matchesString(Expr e, string s) {
exists (RegExpConstant c |
matchesConstant(e.(RegExpLiteral).getRoot(), c) and
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should exclude the i-flag for cases like: x.replace(/foo/i, 'foo').

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An excellent point, thanks!

exists (RegExpCharacterClass recc | recc = t and not recc.isInverted() |
recc.getNumChild() = 1 and
matchesConstant(recc.getChild(0), c)
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should handle anchors for cases like /^x$/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea; and also other zero-width assertions like \b.

* @kind problem
* @problem.severity warning
* @id js/identity-replacement
* @tags correctness
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered the security tag?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit of a stretch, but I suppose it does cover CWE-116, so might as well claim it.

@xiemaisi xiemaisi force-pushed the js/identity-replacement branch from 9ca1ebd to 9002d7f Compare September 25, 2018 10:03
@xiemaisi
Copy link
Author

@esben-semmle: Updated as suggested. I've amended the original commit to include the changes to the query metadata and added it to a suite, the other commits roughly correspond to your other suggestions.

This query is now a bit more complex. I could run a full performance evaluation if desired, but it still looks fairly snappy on the query console, and takes about 20 seconds on gecko-dev, which has some pretty gnarly regular expressions.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor nits left.

repl.getArgument(1).getStringValue() = s
select repl.getArgument(0), "This replaces '" + s + "' with itself."
repl.getArgument(1).getStringValue() = s and
(if s = "" then friendly = "the empty string" else friendly = "'" + s + "'")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a friendly formatting of the two quote characters: ' and "?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could do, but once we start going down that route there are many other friendliness improvements we could also consider (such as special handling of newlines and whatnot). This one is very simple and straightforward, the other ones not so much.

|-----------------------------------------------|------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Enabling Node.js integration for Electron web content renderers (`js/enabling-electron-renderer-node-integration`) | security, frameworks/electron, external/cwe/cwe-094 | Highlights Electron web content renderer preferences with Node.js integration enabled, indicating a violation of [CWE-94](https://cwe.mitre.org/data/definitions/94.html). Results are not shown on LGTM by default. |
| Stored cross-site scripting (`js/stored-xss`) | security, external/cwe/cwe-079, external/cwe/cwe-116 | Highlights uncontrolled stored values flowing into HTML content, indicating a violation of [CWE-079](https://cwe.mitre.org/data/definitions/79.html). Results shown on LGTM by default. |
| Replacement of a substring with itself (`js/identity-replacement`) | correctness | Highlights string replacements that replace a string with itself, which usually indicates a mistake. Results shown on LGTM by default. |
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is now missing some Tags.
Optionally, the Purpose could also be the standard "... indicating a violation a violation of CWE-116..."

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, of course; fixed. I decided not to go for the violation-of-CWE phrasing, though, since I don't want to claim that that's the main purpose of the query.

@xiemaisi xiemaisi force-pushed the js/identity-replacement branch from 9002d7f to 0e63ea1 Compare September 25, 2018 10:27
@xiemaisi xiemaisi requested a review from mchammer01 September 25, 2018 10:30
@xiemaisi
Copy link
Author

Nits addressed/responded to.

@ghost
Copy link

ghost commented Sep 25, 2018

Ping @mc-semmle for doc-review.

@mchammer01
Copy link
Contributor

@xiemaisi - editorial review completed, LGTM.
Good to go from my point of view 👍

@ghost ghost merged commit 7c006d4 into github:master Sep 26, 2018
@xiemaisi xiemaisi deleted the js/identity-replacement branch October 4, 2018 09:35
aibaars added a commit that referenced this pull request Oct 14, 2021
Add support for LGTM_INDEX_FILTERS environment variable
smowton pushed a commit to smowton/codeql that referenced this pull request Feb 7, 2022
MathiasVP pushed a commit to MathiasVP/ql that referenced this pull request Aug 10, 2025
…-stub-models

Revert "Update the C# stub models"
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants