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: promote log injection by yoff · Pull Request #7735 · github/codeql

Conversation

@yoff

@yoff yoff commented Jan 25, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

This PR promotes the log injection (CWE-117) query out of experimental.

Some concerns:

  • the models of loggers, particularly for Django is a bit curated. In general, should we do way more than the contribution when we promote?
  • the sanitization seems very specific. I added the string const compare sanitizer, but there are probably other ways to sanitize also. Should we have a concept for sanitizers similar to escaping or just reuse escaping?

Sorry, something went wrong.

Uh oh!

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

All reactions
@yoff
yoff requested a review from a team as a code owner January 25, 2022 10:07
@github-actions github-actions Bot added documentation Python labels Jan 25, 2022
yoff added 4 commits January 31, 2022 11:27
@yoff
python: move log injection out of experimental
20d5454
- move from custom concept `LogOutput` to standard concept `Logging`
- remove `Log.qll` from experimental frameworks
  - fold models into standard models (naively for now)
    - stdlib:
      - make Logger module public
      - broaden definition of instance
      - add `extra` keyword as possible source
   - flak: add app.logger as logger instance
   - django: `add django.utils.log.request_logger` as logger instance
     (should we add the rest?)
- remove LogOutput from experimental concepts
@yoff
python: Add standard customization setup
8b5114d
- modernize the sanitizer, but do not make it less specific
@yoff
python: Add change note
bf1145e
should we have the `lgtm,codescanning` handshake or not?
@yoff
python: modern change note
9d41666
I set the category to newQuery since that is what users will see.
When we have tags, it would be nice to tag it as a query promotion.
@yoff
yoff force-pushed the python/promote-log-injection branch from 80da48e to 9d41666 Compare January 31, 2022 10:28
RasmusWL
RasmusWL requested changes Jan 31, 2022
View reviewed changes

@RasmusWL RasmusWL left a comment

Copy link
Copy Markdown
Member

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.

python/ql/src/Security/CWE-117/LogInjection.ql needs to have meta-data updated, to include security severity at least. I haven't been involved in looking at this query before, do you feel that @precision high is reasonable? For security-severity, we might just copy the one from java (which is the same as the one for JS)

Looking up those @security-severity scores, I noticed that both queires had @precision medium. That got me thinking, in the example below, neither username nor foo can contain newlines, but would probably end up causing an alert 😐 so we need to be mindful about the precision we set here.

@app.route("/users/<username>")
def show_user(username):
    foo = request.args.get("foo", 10)
    LOGGER.debug(f"showing user {username} with foo={foo}")

nitpick: QLDoc for classes doesn't follow our style-guide (must start with A or An)
nitpick: Would be very nice with documentations links.

(I've made a suggestion for Django to fix the two nitpicks above)

For the qhelp, I see that it is in large part just a copy of the qhelp from the java query. I personally have a mild preference for the JS version (since it better highlights the problems of HTML). Have you made a consideration between the two?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread python/ql/lib/semmle/python/frameworks/Django.qll Resolved

Uh oh!

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

Comment thread python/ql/lib/semmle/python/frameworks/Stdlib.qll
|
this = Logger::instance().getMember(method).getACall()
or
this = API::moduleImport("logging").getMember(method).getACall()

@RasmusWL RasmusWL Jan 31, 2022

Copy link
Copy Markdown
Member

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 don't agree about this removal. I see that API::moduleImport("logging") is now modeled as a LoggerInstance... but that is not true, these are helper methods defined on the module leve.

Sorry, something went wrong.

Uh oh!

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

All reactions

@yoff yoff Feb 1, 2022

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.

Is there value in modelling instances specifically? Or could we just rename instance to something like loggingMethodProvider?

Sorry, something went wrong.

Uh oh!

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

All reactions

@yoff yoff Feb 1, 2022

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.

Reverted to previous modelling approach as discussed offline.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread python/ql/lib/semmle/python/security/dataflow/LogInjectionCustomizations.qll Resolved

Uh oh!

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

Comment thread python/ql/lib/semmle/python/security/dataflow/LogInjectionCustomizations.qll
Comment on lines +59 to +64
class ReplaceLineBreaksSanitizer extends Sanitizer, DataFlow::CallCfgNode {
ReplaceLineBreaksSanitizer() {
this.getFunction().(DataFlow::AttrRead).getAttributeName() = "replace" and
this.getArg(0).asExpr().(StrConst).getText() in ["\r\n", "\n"]
}
}

@RasmusWL RasmusWL Jan 31, 2022

Copy link
Copy Markdown
Member

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 sanitizer check seems a bit weak. What if it only did text.replace("\r\n", "\n")? I guess it's not trivial to write a sanitizer that knows we've eliminated both, so maybe this is ok? (what are your thoughts on this)

Sorry, something went wrong.

Uh oh!

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

All reactions

@yoff yoff Feb 1, 2022 •
edited
Loading

Uh oh!

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

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 have had the same concerns from the beginning.

Sorry, something went wrong.

Uh oh!

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

All reactions

@RasmusWL RasmusWL Feb 8, 2022

Copy link
Copy Markdown
Member

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 great to have a small comment in the code to explain this dilemma, and explain why the solution implemented was deemed ok.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread python/ql/lib/semmle/python/frameworks/Stdlib.qll
Comment on lines +249 to +275
module Logger {
/**
* An instance of `logging.Logger`. Extend this class to model new instances.
* Most major frameworks will provide a logger instance as a class attribute.
*/
abstract class LoggerInstance extends API::Node {
override string toString() { result = "logger" }
}

/** Gets a reference to the `logging.Logger` class or any subclass. */
API::Node subclassRef() {
result = API::moduleImport("logging").getMember("Logger").getASubclass*()
}

/** Gets a reference to an instance of `logging.Logger` or any subclass. */
API::Node instance() {
result instanceof LoggerInstance
or
result = subclassRef().getReturn()
or
result = API::moduleImport("logging")
or
result = API::moduleImport("logging").getMember("root")
or
result = API::moduleImport("logging").getMember("getLogger").getReturn()
}
}

@RasmusWL RasmusWL Jan 31, 2022

Copy link
Copy Markdown
Member

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 see that I created this non-standard setup with just using API::Node, but now that we're exposing it more publicly, we need to rewrite this to use the standard library modeling setup with InstanceSource and type-tracking (which has a snippet, so it's quite easy).

Looking over the code again, I don't quite know why I did this in the first place. LoggerLogCall can easily be written with the standard approach together with DataFlow::MethodCallNode 🤷

Sorry, something went wrong.

Uh oh!

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

All reactions

@yoff yoff Feb 1, 2022

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.

Surely it is nicer, if we can get away with using an API:Node?

Sorry, something went wrong.

Uh oh!

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

All reactions

@yoff yoff Feb 1, 2022

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.

Moved to standard setup as discussed offline.

Sorry, something went wrong.

Uh oh!

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

All reactions
@yoff

yoff commented Feb 1, 2022

Copy link
Copy Markdown
Contributor Author

For the qhelp, I see that it is in large part just a copy of the qhelp from the java query. I personally have a mild preference for the JS version (since it better highlights the problems of HTML). Have you made a consideration between the two?

I did not even consider that we would have such options, I just checked that it looked reasonable :-) Thanks for the heads-up!

All reactions

Sorry, something went wrong.

Uh oh!

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

yoff and others added 5 commits February 1, 2022 10:04
@yoff @RasmusWL
Apply suggestions from code review
c03f89d
Co-authored-by: Rasmus Wriedt Larsen <[email protected]>
@yoff
python: "command" -> "log"
7511b33
@yoff
python: drop precision and add severity score
26befeb
Given both the original FP score and our concerns
regarding sanitizers, `@precision medium`, which
is aligned with other languages, feels appropriate.
@yoff
python: rewrite qhelp overview
ecea392
(combining the Java version and the JS version)
@yoff
python: provide links for Flask
119a7e4
RasmusWL
RasmusWL reviewed Feb 1, 2022
View reviewed changes

@RasmusWL RasmusWL left a comment

Copy link
Copy Markdown
Member

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.

nice updates so far 👍

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread python/ql/src/Security/CWE-117/LogInjection.ql Resolved

Uh oh!

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

yoff added 2 commits February 1, 2022 13:31
@yoff
python: use standard InstanceSource construction
c587084
@yoff
python: update change note
bec8c0d
@yoff
yoff requested a review from RasmusWL February 1, 2022 12:42
@yoff
python: logging.root is not a call
448e078
RasmusWL
RasmusWL requested changes Feb 8, 2022
View reviewed changes

@RasmusWL RasmusWL left a comment

Copy link
Copy Markdown
Member

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.

Two minor things, then we should be good to go 👍

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread python/ql/lib/semmle/python/frameworks/Stdlib.qll Resolved

Uh oh!

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

Comment thread python/ql/lib/semmle/python/security/dataflow/LogInjectionCustomizations.qll
Comment on lines +59 to +64
class ReplaceLineBreaksSanitizer extends Sanitizer, DataFlow::CallCfgNode {
ReplaceLineBreaksSanitizer() {
this.getFunction().(DataFlow::AttrRead).getAttributeName() = "replace" and
this.getArg(0).asExpr().(StrConst).getText() in ["\r\n", "\n"]
}
}

@RasmusWL RasmusWL Feb 8, 2022

Copy link
Copy Markdown
Member

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 great to have a small comment in the code to explain this dilemma, and explain why the solution implemented was deemed ok.

Sorry, something went wrong.

Uh oh!

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

All reactions
yoff and others added 2 commits February 9, 2022 09:22
@yoff @RasmusWL
Update python/ql/lib/semmle/python/frameworks/Stdlib.qll
f21ac04
Co-authored-by: Rasmus Wriedt Larsen <[email protected]>
@yoff
python: add apologetic comment
bd14ade
@yoff
yoff requested a review from RasmusWL February 14, 2022 10:38
@yoff

yoff commented Feb 14, 2022

Copy link
Copy Markdown
Contributor Author

I think it would be great to have a small comment in the code to explain this dilemma, and explain why the solution implemented was deemed ok.

I actually got a bit annoyed with the explanation. Do let me know if we can live with postponing this, or if I should attempt a rewrite in this PR..

All reactions

Sorry, something went wrong.

Uh oh!

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

RasmusWL
RasmusWL reviewed Feb 14, 2022
View reviewed changes

@RasmusWL RasmusWL left a comment

Copy link
Copy Markdown
Member

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 great to have a small comment in the code to explain this dilemma, and explain why the solution implemented was deemed ok.

I actually got a bit annoyed with the explanation. Do let me know if we can live with postponing this, or if I should attempt a rewrite in this PR..

Thanks for writing this 👍 I think it's very nice that the next person thinking about this code can just read the comment explaining we thought about this 👍 I have a small suggestion for writing it, but otherwise LGTM 👍

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread python/ql/lib/semmle/python/security/dataflow/LogInjectionCustomizations.qll Resolved

Uh oh!

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

Comment thread python/ql/lib/semmle/python/security/dataflow/LogInjectionCustomizations.qll Resolved

Uh oh!

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

yoff and others added 2 commits February 14, 2022 16:07
@yoff @RasmusWL
Update python/ql/lib/semmle/python/security/dataflow/LogInjectionCust…
62598c0
…omizations.qll

Co-authored-by: Rasmus Wriedt Larsen <[email protected]>
@yoff @RasmusWL
Update python/ql/lib/semmle/python/security/dataflow/LogInjectionCust…
3a995ec
…omizations.qll

Co-authored-by: Rasmus Wriedt Larsen <[email protected]>
@yoff
yoff requested a review from RasmusWL February 14, 2022 15:08
@RasmusWL
Python: Autoformat
62d4bb5
Trailing whitespace is a bit too easy with the ```suggestions through
the UI :|
RasmusWL
RasmusWL approved these changes Feb 15, 2022
View reviewed changes
@yoff

yoff commented Feb 15, 2022

Copy link
Copy Markdown
Contributor Author

Thanks for the auto format fix :-) (it confused me for a bit that I could not get it to change the file locally 😁)

All reactions

Sorry, something went wrong.

Uh oh!

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

@RasmusWL

RasmusWL commented Feb 17, 2022

Copy link
Copy Markdown
Member

Python 3 Language tests failed seems to have been flaky, so I've rerun them. 🤞

All reactions

Sorry, something went wrong.

Uh oh!

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

@RasmusWL

RasmusWL commented Feb 17, 2022 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Tests are all good, so shipit

Actually, we should do a performance test for this 😳

All reactions

Sorry, something went wrong.

Uh oh!

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

@RasmusWL
Merge branch 'main' into python/promote-log-injection
b59ab7f
@RasmusWL

RasmusWL commented Feb 23, 2022

Copy link
Copy Markdown
Member

Performance does looks fine, so in it goes 👍

All reactions

Sorry, something went wrong.

Uh oh!

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

@RasmusWL
RasmusWL merged commit aeba497 into github:main Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@RasmusWL RasmusWL RasmusWL approved these changes

Uh oh!

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

Assignees

No one assigned

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.

2 participants

@yoff @RasmusWL
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.