-
Notifications
You must be signed in to change notification settings - Fork 231
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
New rule S6444: RegEx evaluation should have a time out specified #5693
New rule S6444: RegEx evaluation should have a time out specified #5693
Conversation
@Corniel Thank you for the contribution. Some notes for the review.
I reviewed the documentation, and @Corniel is right.
There is a global setting on the App Domain to set the default timeout. If not specified, the default timeout is We should also test the regex compilation scenarios (CompileToAssembly, RegexOptions.Compiled) and the new source generator. I don't expect any problems here, but we should test it. |
I do not see what the |
@martin-strecker-sonarsource I consider also adding a code fix. Should not be that hard in this case. |
01c2a39
to
49e67e0
Compare
@martin-strecker-sonarsource Any timeline for progressing this? |
49e67e0
to
3e610d5
Compare
@Corniel Sorry for the late response. The team looked into this, and we are eager to see this new rule added to our suite! I'm going to work on your RSpec PR first and get it approved. I will do the review of this PR afterward. Thanks for your patience. Can you please add a link to the RSpec PR in the PR description? SonarSource/rspec#1061 |
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
7cb8f6a
to
c23e821
Compare
@martin-strecker-sonarsource I added the detection of the |
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Corniel This looks really good so far. I want you to use MethodParameterLookup to find the right ArgumentSyntax for the parameters in question. See my comments for details.
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SpecifyTimeoutOnRegex.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SpecifyTimeoutOnRegex.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SpecifyTimeoutOnRegex.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SpecifyTimeoutOnRegex.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SpecifyTimeoutOnRegex.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now. Just some minor code style issues. One question remains though: Should we look up the parameters options
and matchTimeout
by name or by type? I will discuss this internally and come back to you.
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SpecifyTimeoutOnRegex.CSharp9.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor clean-ups, and we decided to go for the parameter.Name
for the parameter lookup.
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
/azp run Sonar.Net |
Azure Pipelines successfully started running 1 pipeline(s). |
analyzers/src/SonarAnalyzer.Common/Rules/SpecifyTimeoutOnRegexBase.cs
Outdated
Show resolved
Hide resolved
/azp run Sonar.Net |
Azure Pipelines successfully started running 1 pipeline(s). |
Hi @Corniel, Our continuous integration pipeline revealed, that your rule throws an
It seems the exception is thrown in The exception happens because of code somewhere in this code file: You can execute this test to reproduce the error:
The same error occurs for VB too. Here you need to execute Can you please have a look? If you need some help, please don't hesitate to ask. The full error message is:
|
I wrongly assumed that in case, there would always be an identifier token. I added a That being said, I think it is also worth to add: |
c83f6db
to
948024d
Compare
@pavel-mikula-sonarsource What is your stance on this? You can certainly do public static SyntaxToken? NodeIdentifier(this SyntaxNode node) =>
node.RemoveParentheses() switch
{
AttributeArgumentSyntax x => x.NameColon?.Name.Identifier,
BaseTypeDeclarationSyntax x => x.Identifier,
// ..
InvocationExpressionSyntax x => x.Expression is LiteralExpressionSyntax { RawKind: (int)SyntaxKind.ArgListExpression } literal
? literal.Token
: NodeIdentifier(x.Expression),
// ..
_ => null,
}; Background: I like the idea to treat this obscure invocation as every other invocation. On the other hand:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small typo.
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SpecifyTimeoutOnRegex.cs
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SpecifyTimeoutOnRegex.vb
Outdated
Show resolved
Hide resolved
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RuleFailure/SpecialCases.vb
Show resolved
Hide resolved
/azp run Sonar.Net |
Azure Pipelines successfully started running 1 pipeline(s). |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
@Corniel Big kudos to you for implementing this great rule and for your patience. It's much appreciated. |
Peach validation
csharpsquid.S6444 2022-12-16 12.40.txt <- change file extension to html |
See: community.sonarsource.com,
And RSPEC PR: SonarSource/rspec#1188
Description
When using
System.Text.RegularExpressions
to process untrusted input, pass atimeout.
A malicious user can provide input to RegularExpressions causing a
Denial-of-Service attack.
Noncompliant Code Example
Compliant Solution
Labels: security vulnerability
Message: Pass a timeout to limit the execution time.
Default Severity: Major
Impact: Low
Likelihood: Medium
Default Quality Profiles: Sonar way
Covered Languages: C#, VB.Net
Remediation Function: ?
Constant Cost: 5min
Analysis Level: ?
Syntactic Analysis Analysis Scope: Test Sources