Skip to content

Try and deal with CodeQL reports on replace("*", ...)#56607

Merged
jakebailey merged 7 commits into
microsoft:mainfrom
jakebailey:first-star-replace
Dec 5, 2023
Merged

Try and deal with CodeQL reports on replace("*", ...)#56607
jakebailey merged 7 commits into
microsoft:mainfrom
jakebailey:first-star-replace

Conversation

@jakebailey

@jakebailey jakebailey commented Nov 29, 2023

Copy link
Copy Markdown
Member

We constantly have to dismiss this warning: https://github.com/microsoft/TypeScript/security/code-scanning/206

But, downstream users are now seeing this if they happen to vendor our code. There must be something we can do to make the code scanner not complain anymore.

For now, I'm moving this out to a helper to make sure CodeQL is still mad, then will experiment later.

Using the replace method directly has tricked CodeQL. Maybe that is enough.

We already bypassed this elsewhere using an arrow function, so I've done that instead.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 29, 2023
@jakebailey jakebailey changed the title Move all replacements of star prefix out to common function Try and deal with CodeQL reports on replace("*", ...) Nov 29, 2023
Comment thread src/compiler/utilities.ts Fixed
@jakebailey jakebailey marked this pull request as ready for review November 29, 2023 22:39
@fatcerberus

Copy link
Copy Markdown

FYI: the link in the OP 404s.

@RyanCavanaugh

Copy link
Copy Markdown
Member

Context: CodeQL really doesn't like that we only replace the first * in a string, since it looks like a mistake

image

@fatcerberus

Copy link
Copy Markdown

Can you pass a (non-global) regex instead of a string to get rid of the warning?

@RyanCavanaugh

Copy link
Copy Markdown
Member

Surprisingly, it's slower

image

@jakebailey

Copy link
Copy Markdown
Member Author

The analyzer also detects non-global regex replacements, so it's moot.

IndexOf and slicing also would work but any of these are plenty fast as to not matter.

@fatcerberus

fatcerberus commented Dec 1, 2023

Copy link
Copy Markdown

The analyzer also detects non-global regex replacements, so it's moot.

:-\

Like, I get that this is a common mistake people make in JS but it would be nice if they provided a way to say, yes, I really do want the documented behavior here…

This is why I can’t do linters.

@jakebailey

Copy link
Copy Markdown
Member Author

This is why I can’t do linters.

The thing is that nobody don't lints node_modules or vendored deps, and yet here we are...

@jakebailey

Copy link
Copy Markdown
Member Author

I ran codeql locally to see what all is left after this PR. The only other remaining bad code is:

stripQuotes(quoted).replace(/'/g, "\\'").replace(/\\"/g, '"')

Where it thinks that we should also be escaping \ in the input, when in actuality we're just swapping one quote for another.

I would also like to fix this one (so our codebase is clean), but this makes me wonder if it'd be better to instead just export stringReplace as a function and use it everywhere instead, at the risk of them eventually detecting the pattern.

I'm also noticing that we bypassed this detection in another way:

/** @internal */
export function escapeSnippetText(text: string): string {
    return text.replace(/\$/gm, () => "\\$");
}

If you use a replacer function, it doesn't care. Introduced in #46716. This would be easier for the one-off quote thing, but I'm curious if anyone has a preference to whether I just use this trick too in the other replacements. @andrewbranch do you have a preference?

@andrewbranch

Copy link
Copy Markdown
Member

I do not have a preference.

Comment thread src/compiler/moduleSpecifiers.ts Fixed
Comment thread src/compiler/moduleSpecifiers.ts Fixed
Comment thread src/compiler/moduleSpecifiers.ts Fixed
Comment thread src/compiler/moduleNameResolver.ts Fixed
Comment thread src/compiler/moduleNameResolver.ts Fixed
Comment thread src/compiler/moduleNameResolver.ts Fixed
@jakebailey

jakebailey commented Dec 5, 2023

Copy link
Copy Markdown
Member Author

Well, that didn't work. I guess the arrow thing only works for the backslash test. Mistake for uploading it assuming that would work, bah.

This reverts commit 5b4bd7c.
@jakebailey

Copy link
Copy Markdown
Member Author

Alright, settling on the new function for the star thing, plus the arrow function which mirrors the backslashing error we handle in snippets in the same way. Which is to say the original PR but with the one new arrow function 😄

@jakebailey jakebailey merged commit 1d7c0c9 into microsoft:main Dec 5, 2023
@jakebailey jakebailey deleted the first-star-replace branch December 5, 2023 21:58
c0sta pushed a commit to c0sta/TypeScript that referenced this pull request Dec 20, 2023
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants