Skip to content

Python: modernise Security/ queries #2739

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

Merged
merged 13 commits into from
Feb 18, 2020

Conversation

RasmusWL
Copy link
Member

No description provided.

@RasmusWL RasmusWL force-pushed the python-modernise-security branch 2 times, most recently from cc2d727 to 94750fc Compare February 4, 2020 10:15
Since `IntObjectInternal` extends `TInt`, and `TInt` is defined for all
instances of `Builtin.intValue`, and `Builtin.intValue` includes both `int` and
`long`, we don't need to handles Longs in a special manner, as we did in NumericObject.
also fixes test code to use the right argument name
Replacing `Value.booleanValue`. We wanted to match `Object.booleanValue` that
only gives a result if it is either `true` or `false`, but also wanted to keep
the flexibility to see if the Value _could_ be `true`/`false`. We don't have a
motivating usecase, so let's see if we ever need it :P

+ fix modernisation regression on py/jinja2/autoescape-false
@RasmusWL RasmusWL force-pushed the python-modernise-security branch from 94750fc to c1d073a Compare February 4, 2020 10:42
@RasmusWL
Copy link
Member Author

RasmusWL commented Feb 4, 2020

Ready to go now, except for one last question.

The characteristic predicate for NumericObject is

class NumericObject extends Object {
    NumericObject() {
        this.asBuiltin().getClass() = theIntType().asBuiltin() or
        this.asBuiltin().getClass() = theLongType().asBuiltin() or
        this.asBuiltin().getClass() = theFloatType().asBuiltin()
    }

but for NumericValue (added in this PR), I was not able to find a LongObjectInternal, and was wondering if we needed to add it or not.

class NumericValue extends Value {
    NumericValue() {
        this instanceof IntObjectInternal or
        this instanceof FloatObjectInternal
        // TODO: Should we have a LongObjectInternal ?
    }

Answering my own questions:

I concluded that we don't need to add it, and here is my reasoning.

I searched for uses of theLongType(), and it was only used in 3 places:

In IntegerLiteral.getLiteralObject

https://github.com/Semmle/ql/blob/c525ab325f7a8edbf3eebbfcaaec1e34d052e141/python/ql/src/semmle/python/Exprs.qll#L446-L450

and in NumericObject.intValue()

https://github.com/Semmle/ql/blob/66899942851a850017787f7a04f9b3a2278eb2d7/python/ql/src/semmle/python/types/Object.qll#L244-L252

It is also used in NumericObject.repr(), but I'm ignoring that for now.

_

Since NumericValue includes all IntObjectInternal, and IntObjectInternal extends TInt, and TInt is defined for all instances of Builtin.intValue, we can conclude that with NumericValue we cover all the cases that NumericObject does, and therefore we don't need LongObjectInternal.

https://github.com/Semmle/ql/blob/662aedcb133a47d04a2518f5442b12ecc2d4593b/python/ql/src/semmle/python/types/Builtins.qll#L90-L94

@RasmusWL RasmusWL changed the title WIP Python: modernise Security/ queries Python: modernise Security/ queries Feb 4, 2020
@RasmusWL RasmusWL marked this pull request as ready for review February 4, 2020 10:42
@RasmusWL RasmusWL requested review from felicitymay and a team as code owners February 4, 2020 10:42
@felicitymay felicitymay removed their request for review February 4, 2020 10:58
@felicitymay
Copy link
Contributor

I think I was automatically added as a reviewer when you added a comma to a qhelp file. Feel free to add me back if you later add changes to this PR that need a docs review 😄

Copy link
Contributor

@tausbn tausbn left a comment

Choose a reason for hiding this comment

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

A few comments, otherwise LGTM.

test(password='format_string_{}')

# TODO: we think this is a format string :\
test(password='''U]E8FPETCS_]{,y>bgyzh^$yC5>SP{E*2=`;3]G~k&+;khy3}4]jdpu;D(aP$SCFA{;hh4n46pUJ%+$nEP_gqNq#X!2$%*C-6y6%''')
Copy link
Contributor

Choose a reason for hiding this comment

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

And this means we do not consider it to be a string? That sounds wrong to me.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, this makes sense. We wouldn't want to flag random format strings as containing credentials (though I wonder how often this happens. We might want to see what the impact of removing not format_string(str) is on LGTM).

@tausbn tausbn merged commit ffbb5d0 into github:master Feb 18, 2020
@RasmusWL RasmusWL deleted the python-modernise-security branch February 18, 2020 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants