GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Python: CWE-117 Log injection by haby0 · Pull Request #6182 · github/codeql

Conversation

@haby0

@haby0 haby0 commented Jun 29, 2021

Copy link
Copy Markdown
Contributor

If unsanitized user input is written to a log entry, a malicious user may be able to forge new log entries.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@haby0
haby0 requested a review from a team as a code owner June 29, 2021 12:53
@github-actions github-actions Bot added documentation Python labels Jun 29, 2021
@haby0 haby0 mentioned this pull request Jun 30, 2021
[Python]: CWE-117 Log Injection github/securitylab#388
Closed
1 task done
@RasmusWL

RasmusWL commented Jun 30, 2021

Copy link
Copy Markdown
Member

Hi @haby0, thanks for this contribution 👍 We'll take a look at this as soon, but we do have a few other things we're also working on at the moment 😊

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@haby0

haby0 commented Jun 30, 2021

Copy link
Copy Markdown
Contributor Author

Hi @haby0, thanks for this contribution 👍 We'll take a look at this as soon, but we do have a few other things we're also working on at the moment 😊

Ok, thanks for your reply.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RasmusWL RasmusWL assigned yoff Aug 23, 2021
@haby0
Fix conflicting
c60eded
@haby0

haby0 commented Sep 15, 2021

Copy link
Copy Markdown
Contributor Author

@yoff The conflict has been resolved, please review, thank you.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yoff
yoff requested changes Sep 15, 2021
View reviewed changes

@yoff yoff left a comment

Copy link
Copy Markdown
Contributor

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.

You are not handling HTML logging and encoding, did you plan to? It might require a second configuration, seeing as the sinks dictate which sanitizers are required...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread python/ql/src/experimental/semmle/python/frameworks/Log.qll Outdated
* See https://docs.python.org/3/library/logging.html#logger-objects
*/
private class LogOutputMethods extends string {
LogOutputMethods() { this in ["info", "error", "warn", "warning", "debug", "critical"] }

@yoff yoff Sep 15, 2021

Copy link
Copy Markdown
Contributor

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.

We seem to be missing "debug", "log" , and "exception" from this list.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@haby0 haby0 Sep 17, 2021

Copy link
Copy Markdown
Contributor Author

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.

Pr updated.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread python/ql/src/experimental/semmle/python/frameworks/Log.qll Outdated
this = API::moduleImport("logging").getMember(any(LogOutputMethods m)).getACall()
}

override DataFlow::Node getAnInput() { result = this.getArg(_) }

@yoff yoff Sep 15, 2021

Copy link
Copy Markdown
Contributor

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.

Note that getArg(_), perhaps confusingly, will not return all arguments. In particular, starred arguments are filtered out, as are keyword only arguments. This means that for info(msg, *args, **kwargs) you will see neither args, nor kwargs.

Note also, that if you add log, its first argument is level which should probably not be considered an input.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@haby0 haby0 Sep 17, 2021

Copy link
Copy Markdown
Contributor Author

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.

Pr updated.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread python/ql/src/experimental/semmle/python/security/injection/LogInjection.qll
Comment on lines +18 to +23
override predicate isSanitizer(DataFlow::Node node) {
exists(CallNode call |
node.asCfgNode() = call.getFunction().(AttrNode).getObject("replace") and
call.getArg(0).getNode().(StrConst).getText() in ["\r\n", "\n"]
)
}

@yoff yoff Sep 15, 2021

Copy link
Copy Markdown
Contributor

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.

This is slightly unsafe as it deems either replacement ("\r\n" or "\n") a sanitizer when really both are required.
I am not sure how to improve that easily, though. Perhaps we will turn it into a concept with a single implementation and improve it over time, but I am not sure you need to do that now.

Further, this seems overly specific, so I would expect some false positives from this. Consider adding at least StringConstCompare as a sanitizer option. (Either directly here in this predicate, or by a construction similar to here.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@haby0 haby0 Sep 17, 2021

Copy link
Copy Markdown
Contributor Author

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.

Do you have a specific case here? Because the safety laboratory did not give me advice on the elimination of FP.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@yoff yoff Sep 22, 2021

Copy link
Copy Markdown
Contributor

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.

If the user does replace("\r\n", ""), then that will match your predicate and be considered a sanitizer. However, that would not do any replacements on a message having only "\n"'s in it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@yoff yoff Sep 22, 2021

Copy link
Copy Markdown
Contributor

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.

The other comment is that if the message is compared against a known string, that should be considered a sanitizer (since that is much stronger than doing a replacement).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@haby0 haby0 Sep 22, 2021

Copy link
Copy Markdown
Contributor Author

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.

I think it would be better if you give me the actual case.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@haby0 haby0 Sep 24, 2021

Copy link
Copy Markdown
Contributor Author

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.

@yoff Excuse me again. Do you have examples in actual open source projects for me?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@yoff yoff Oct 4, 2021

Copy link
Copy Markdown
Contributor

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.

Hm, I had to do a bit of digging. My comment came from seeing code like this, although in that particular case we do want to alert (because the logged value is outside the known set). I think I have seen also that only values in a known set is logged, which would be safe, and that is the sanitizer I proposed. However, it does not seem to be very common, so we could also add it once we see the first FP...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@haby0

haby0 commented Sep 15, 2021

Copy link
Copy Markdown
Contributor Author

You are not handling HTML logging and encoding, did you plan to? It might require a second configuration, seeing as the sinks dictate which sanitizers are required...

Thanks for the review, about this suggestion, can you describe it again? I am not too understanding.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yoff

yoff commented Sep 16, 2021

Copy link
Copy Markdown
Contributor

You are not handling HTML logging and encoding, did you plan to? It might require a second configuration, seeing as the sinks dictate which sanitizers are required...

Thanks for the review, about this suggestion, can you describe it again? I am not too understanding.

I meant that to find html-based log injection, you would need sinks to be logs displayed as HTML and sanitizers to be HTML-relevant escaping. On the whole that seems to be an entirely separate taint tracking configuration. I wonder if that is out of scope for now, but it is mentioned in the help file.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@haby0

haby0 commented Sep 17, 2021

Copy link
Copy Markdown
Contributor Author

You are not handling HTML logging and encoding, did you plan to? It might require a second configuration, seeing as the sinks dictate which sanitizers are required...

Thanks for the review, about this suggestion, can you describe it again? I am not too understanding.

I meant that to find html-based log injection, you would need sinks to be logs displayed as HTML and sanitizers to be HTML-relevant escaping. On the whole that seems to be an entirely separate taint tracking configuration. I wonder if that is out of scope for now, but it is mentioned in the help file.

Thank you for your reply. I am not going to add HTML injection due to log injection in this PR. I will create a new PR to complete this query.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@haby0
Modify sinks
9916753
yoff
yoff requested changes Sep 22, 2021
View reviewed changes

@yoff yoff left a comment

Copy link
Copy Markdown
Contributor

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.

I have tried to clarify my points.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread python/ql/src/experimental/semmle/python/frameworks/Log.qll
Comment on lines +38 to +44
override DataFlow::Node getAnInput() {
this.getFunction().(DataFlow::AttrRead).getAttributeName() != "log" and
result = this.getArg(0)
or
this.getFunction().(DataFlow::AttrRead).getAttributeName() = "log" and
result = this.getArg(1)
}

@yoff yoff Sep 22, 2021

Copy link
Copy Markdown
Contributor

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.

Suggested change
override DataFlow::Node getAnInput() {
this.getFunction().(DataFlow::AttrRead).getAttributeName() != "log" and
result = this.getArg(0)
or
this.getFunction().(DataFlow::AttrRead).getAttributeName() = "log" and
result = this.getArg(1)
}
override DataFlow::Node getAnInput() {
this.getFunction().(DataFlow::AttrRead).getAttributeName() != "log" and
result in [this.getArg(_), this.getArgByName(_) ] // this includes the arg named "msg"
or
this.getFunction().(DataFlow::AttrRead).getAttributeName() = "log" and
result in [this.getArg(any(int i | i > 0)), this.getArgByName(any(string s | s != "level")]
}

I think my previous comment was a bit unclear, it should be something like the above.

If you have a signature like

warning(msg, *args, **kwargs)

you can call it with

warning("msg", "arg1", "arg2", kwarg1="kwarg1", kwargs="kwarg2")

and all of these arguments seem to end up in the log.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread python/ql/src/experimental/semmle/python/security/injection/LogInjection.qll
Comment on lines +18 to +23
override predicate isSanitizer(DataFlow::Node node) {
exists(CallNode call |
node.asCfgNode() = call.getFunction().(AttrNode).getObject("replace") and
call.getArg(0).getNode().(StrConst).getText() in ["\r\n", "\n"]
)
}

@yoff yoff Sep 22, 2021

Copy link
Copy Markdown
Contributor

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.

If the user does replace("\r\n", ""), then that will match your predicate and be considered a sanitizer. However, that would not do any replacements on a message having only "\n"'s in it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread python/ql/src/experimental/semmle/python/security/injection/LogInjection.qll
Comment on lines +18 to +23
override predicate isSanitizer(DataFlow::Node node) {
exists(CallNode call |
node.asCfgNode() = call.getFunction().(AttrNode).getObject("replace") and
call.getArg(0).getNode().(StrConst).getText() in ["\r\n", "\n"]
)
}

@yoff yoff Sep 22, 2021

Copy link
Copy Markdown
Contributor

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.

The other comment is that if the message is compared against a known string, that should be considered a sanitizer (since that is much stronger than doing a replacement).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@haby0

haby0 commented Sep 22, 2021

Copy link
Copy Markdown
Contributor Author

@yoff Compared with the known string, is the known string "\r\n" or "\n"?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@haby0
Apply @yoff's suggestion
6c07a3e
@haby0
haby0 requested a review from yoff September 23, 2021 09:05
@haby0

haby0 commented Sep 30, 2021

Copy link
Copy Markdown
Contributor Author

Friendly ping.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yoff
yoff previously approved these changes Oct 4, 2021
View reviewed changes

@yoff yoff left a comment

Copy link
Copy Markdown
Contributor

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.

I am still vary of the FP rate, but I do not have any evidence for it, so happy to approve for now.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@haby0

haby0 commented Oct 5, 2021

Copy link
Copy Markdown
Contributor Author

I am still vary of the FP rate, but I do not have any evidence for it, so happy to approve for now.

Thank you for your reply. If you have any suggestions, please let me know and I will improve the configuration query.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@yoff

yoff commented Oct 11, 2021

Copy link
Copy Markdown
Contributor

The language tests fail because we now have a mechanism for computing subpaths. You can merge from main and rerun tests. I will also make a suggestion, that I think will make them pass..

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yoff
yoff reviewed Oct 11, 2021
View reviewed changes
Comment thread python/ql/test/experimental/query-tests/Security/CWE-117/LogInjection.expected Resolved

Uh oh!

There was an error while loading. Please reload this page.

@haby0 @yoff
Update python/ql/test/experimental/query-tests/Security/CWE-117/LogIn…
29ddc76
…jection.expected

Co-authored-by: yoff <[email protected]>
@haby0
haby0 dismissed yoff’s stale review via 29ddc76 October 11, 2021 08:43
@yoff

yoff commented Oct 11, 2021

Copy link
Copy Markdown
Contributor

The final error is due to formatting, particularly ql/python/ql/src/experimental/semmle/python/frameworks/Log.qll and ql/python/ql/src/experimental/semmle/python/Frameworks.qll.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@haby0
Auto Formatting
d52f95d
@haby0

haby0 commented Oct 12, 2021

Copy link
Copy Markdown
Contributor Author

The final error is due to formatting, particularly ql/python/ql/src/experimental/semmle/python/frameworks/Log.qll and ql/python/ql/src/experimental/semmle/python/Frameworks.qll.

@yoff Automatic formatting is done, please review whether it is passed, thank you.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

yoff
yoff approved these changes Oct 12, 2021
View reviewed changes

@yoff yoff left a comment

Copy link
Copy Markdown
Contributor

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.

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@yoff
yoff merged commit 43f7eed into github:main Oct 12, 2021
@haby0
haby0 deleted the python/LogInjection branch October 13, 2021 08:29
@yoff yoff mentioned this pull request Feb 1, 2022
Python: promote log injection #7735
Merged
@Onyx2406 Onyx2406 mentioned this pull request Jul 14, 2023
Mitigation of Log Injection Vulnerabilities in Sender.java apache/fineract#3308
Closed
6 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@yoff yoff yoff approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@yoff yoff

Labels

documentation Python

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@haby0 @RasmusWL @yoff
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.