Skip to content
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

Enhanced LogForgingQuery to treat C# Enums as simple types. #17866

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ianroof
Copy link

@ianroof ianroof commented Oct 29, 2024

Pull Request checklist

All query authors

Internal query authors only

  • Autofixes generated based on these changes are valid, only needed if this PR makes significant changes to .ql, .qll, or .qhelp files. See the documentation (internal access required).
  • Changes are validated at scale (internal access required).
  • Adding a new query? Consider also adding the query to autofix.

Copy link
Contributor

@michaelnebel michaelnebel left a comment

Choose a reason for hiding this comment

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

Thank you very much for the contribution! This looks like a very reasonable change.
I have added some review comments - please let me know, if you have any questions or need any help.
If you have the CodeQL CLI installed on your machine you can execute the log forging tests by

codeql test run <path_to_folder_with_test> --show-extractor-output

Example

codeql test run csharp/ql/test/query-tests/Security\ Features/CWE-117/ --show-extractor-output

After the review comments have been addressed, we

  • Trigger the CI (to see if there is any other impact)
  • Run our queries on a larger set of repos (DCA) to see which effect it has to consider enums as simple type sanitizers.

@@ -0,0 +1,4 @@
---
category: fix
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
category: fix
category: minorAnalysis

Copy link
Contributor

Choose a reason for hiding this comment

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

This is as such not a "fix" but rather a change in, how we perceive enums.

Copy link
Author

Choose a reason for hiding this comment

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

That makes sense.

TestEnumValue
}

public class LogForgingSimpleTypes
Copy link
Contributor

Choose a reason for hiding this comment

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

The test code doesn't compile. Consider adding the test code in https://github.com/github/codeql/blob/main/csharp/ql/test/query-tests/Security%20Features/CWE-117/LogForging.cs

Compilation issues:

  • Duplicate declaration of ILogger (as this class also exists in the file mentioned above).
  • Arguments to the Warn calls.

public void Execute(HttpContext ctx)
{
// GOOD: int
logger.Warn("Logging simple type (int):" 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you intend to concatenate? "Logging simple type (int):" + 1


public class LogForgingSimpleTypes
{
public void Execute(HttpContext ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

The test cases doesn't test the simple type sanitization as the input to the logger are hardcoded constants and not a data flow source for the log forging query (maybe use the same pattern as in https://github.com/github/codeql/blob/main/csharp/ql/test/query-tests/Security%20Features/CWE-117/LogForging.cs)

Copy link
Author

Choose a reason for hiding this comment

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

Interestingly, I have seen hardcoded Enums trigger this, but I will address this to use a true data flow source.

@@ -57,7 +57,8 @@ class SimpleTypeSanitizedExpr extends DataFlow::ExprNode {
SimpleTypeSanitizedExpr() {
exists(Type t | t = this.getType() or t = this.getType().(NullableType).getUnderlyingType() |
t instanceof SimpleType or
t instanceof SystemDateTimeStruct
t instanceof SystemDateTimeStruct or
t instanceof Enum
Copy link
Contributor

Choose a reason for hiding this comment

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

@hvitved : Do you know of any historical reason why we haven't considered enums as being sanitizers (I think it looks very reasonable that they should be considered sanitizers - but just checkin with you as this change has a wide impact)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants