Giter VIP home page Giter VIP logo

github / codeql Goto Github PK

View Code? Open in Web Editor NEW
7.1K 231.0 1.4K 339.86 MB

CodeQL: the libraries and queries that power security researchers around the world, as well as code scanning in GitHub Advanced Security

Home Page: https://codeql.github.com

License: MIT License

C++ 2.58% C 3.96% Python 5.73% Perl 0.01% C# 34.24% JavaScript 3.88% HTML 0.22% TypeScript 0.37% RAML 0.01% Shell 0.07% Java 9.52% Vue 0.01% Thrift 0.01% Emacs Lisp 0.01% CodeQL 39.38% Batchfile 0.01% ASP.NET 0.01% Smalltalk 0.01% EJS 0.01% Handlebars 0.01%
semmle-ql codeql github-advanced-security github-security-lab works-with-codespaces

codeql's Introduction

CodeQL

This open source repository contains the standard CodeQL libraries and queries that power GitHub Advanced Security and the other application security products that GitHub makes available to its customers worldwide.

How do I learn CodeQL and run queries?

There is extensive documentation on getting started with writing CodeQL using the CodeQL extension for Visual Studio Code and the CodeQL CLI.

Contributing

We welcome contributions to our standard library and standard checks. Do you have an idea for a new check, or how to improve an existing query? Then please go ahead and open a pull request! Before you do, though, please take the time to read our contributing guidelines. You can also consult our style guides to learn how to format your code for consistency and clarity, how to write query metadata, and how to write query help documentation for your query.

For information on contributing to CodeQL documentation, see the "contributing guide" for docs.

License

The code in this repository is licensed under the MIT License by GitHub.

The CodeQL CLI (including the CodeQL engine) is hosted in a different repository and is licensed separately. If you'd like to use the CodeQL CLI to analyze closed-source code, you will need a separate commercial license; please contact us for further help.

Visual Studio Code integration

If you use Visual Studio Code to work in this repository, there are a few integration features to make development easier.

CodeQL for Visual Studio Code

You can install the CodeQL for Visual Studio Code extension to get syntax highlighting, IntelliSense, and code navigation for the QL language, as well as unit test support for testing CodeQL libraries and queries.

Tasks

The .vscode/tasks.json file defines custom tasks specific to working in this repository. To invoke one of these tasks, select the Terminal | Run Task... menu option, and then select the desired task from the dropdown. You can also invoke the Tasks: Run Task command from the command palette.

codeql's People

Contributors

aibaars avatar alexrford avatar aschackmull avatar asger-semmle avatar asgerf avatar atorralba avatar calumgrant avatar egregius313 avatar erik-krogh avatar geoffw0 avatar hmac avatar hvitved avatar igfoo avatar jbj avatar jf205 avatar jketema avatar joefarebrother avatar mathiasvp avatar max-schaefer avatar michaelnebel avatar nickrolfe avatar owen-mc avatar rasmuswl avatar rdmarsh2 avatar redsun82 avatar semmle-qlci avatar smowton avatar tamasvajk avatar tausbn avatar yoff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

codeql's Issues

Python: False Positive in py/regex/duplicate-in-character-class

Example: https://lgtm.com/projects/g/edemo/PDEngine/snapshot/0aef4231561da169109752d6fdb020f259db5f97/files/tools/getGithubIssues?sort=name&dir=ASC&mode=heatmap#x8b87d5afc0b67d28:1

Reported here: https://discuss.lgtm.com/t/false-positive-python-regexp/1967

It looks like character sets that include [ confuse the grouping. In the above regex, the only square brackets that define a character class sets are as follows (the rest are escaped or are in the class themselves):

\[(?P<txt>[^[]*)\]\((?P<uri>[^)]*)
          ^  ^              ^  ^

Java: FP in java/index-out-of-bounds when modulo checked

When the length of the array is confirmed to be 0 (mod n) where n is the increment value within the for loop, and this check is done immediately before the loop starts. As long as:

  • the loop variable isn't modified anywhere else within the loop body
  • the length of the array does not change (which will not happen for primitives unless reassigned).

we can be fairly certain that array accesses arr[i] .. arr[i + n - 1] will not be out-of bounds.

see: https://discuss.lgtm.com/t/false-positive-java-index-out-of-bounds-with-preconditions-check/1787

Java: FP in java/integer-multiplication-cast-to-long when using Long.BYTES

See: https://discuss.lgtm.com/t/java-false-positives/1787/2

Example: https://lgtm.com/projects/g/apache/accumulo/snapshot/dist-1984940991-1551192209962/files/core/src/main/java/org/apache/accumulo/core/file/rfile/bcfile/BCFile.java?sort=name&dir=ASC&mode=heatmap#xc3783b1655fd55ea:1

Final fields that are well-known, and for which we don't have the source code (e.g. Long.BYTES) are not considered ConstantIntegerExpr in RangeUtils.qll, so we incorrectly flag some multiplications as potential overflows.

false positive: Uncontrolled data used in OS command

Hi,

We just started using https://github.com/firehol/netdata in LGTM. Thank you!

We found that LGTM reports cpp/command-line-injection false positives.

Here is a screenshot:

image

But the code is the other way around: We use fgets() after we run the command, to read the output of the program we execute, like this:

https://lgtm.com/projects/g/firehol/netdata/snapshot/2a7cf3528a14cd50a69af4d75e1441a4b035d231/files/src/cgroup-network.c?#xb0514f82e375bcb6:1

image

Python: FP in py/modification-of-default-value when modification is guarded by check of value

As reported here: https://discuss.lgtm.com/t/false-positive-for-default-value-causing-early-return/1923

In this example: https://lgtm.com/projects/g/sympy/sympy/snapshot/190c7facbe879630c849b79962159f6b37459048/files/sympy/codegen/array_utils.py?sort=name&dir=ASC&mode=heatmap the mutation only occurs when a condition is met that is never true for the default value. As the default value is [], not [] will always be true when no parameter is passed in, so the default value will never me modified.

Java: False positive double checked locking

Java: FP in java/integer-multiplication-cast-to-long with integer of max size

See: https://discuss.lgtm.com/t/java-false-positives/1787

Example: https://lgtm.com/projects/g/apache/accumulo/snapshot/dist-1984940991-1551192209962/files/core/src/main/java/org/apache/accumulo/core/clientImpl/ReplicationClient.java?sort=name&dir=ASC&mode=heatmap#xb063a21d9287922b:1

When the integer being multiplied is known to be small by range analysis (in this case, we can see that attempts is never greater than 10), then we should be able to determine that the multiplication does not overflow. It seems that this query currently takes into account when there is a constant value where we can work this out, but not when there is a maximum value.

false positives: js/xss

Hi,

I have a few alerts about js/xss I can't understand how to work around them.

Here is an example:

https://lgtm.com/projects/g/netdata/netdata/alerts/?mode=list&rule=js%2Fxss

As you can see, I have escaped everything, but still LGTM complaints about XSS:

function escapeUserInputHTML(s) {
    return s.toString()
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;')
        .replace(/#/g, '&#35;')
        .replace(/'/g, '&#39;')
        .replace(/\(/g,'&#40;')
        .replace(/\)/g,'&#41;')
        .replace(/\//g,'&#47;');
}

function verifyURL(s) {
    if(typeof(s) === 'string' && (s.startsWith('http://') || s.startsWith('https://')))
        return s
            .replace(/'/g, '%22')
            .replace(/"/g, '%27')
            .replace(/\)/g, '%28')
            .replace(/\(/g, '%29');

    console.log('invalid URL detected:');
    console.log(s);
    return 'javascript:alert("invalid url");';
}

Any ideas?

Thank you!

false positive with { in comments

This is in regard to the "commented out code" query. If there is a curly brace inside a comment (/* advance to { */) this is misdetected as commented out code. It's for sure a border case, but given the fact that before the brace no valid C is given, I think it could be sorted out. It's also pretty hard to work around this type of FP.

Sample in context: https://lgtm.com/projects/g/rsyslog/rsyslog/alerts/?mode=list
Right now, you need to scroll down a bit to see the sample.

C++: FPs in cpp/declaration-hides-variable when Nesting range for loops

See:

__begin, __end and __range are incorrectly flagged as masked, even though the variables are not visible in the code. Perhaps we should always exclude these, or just exclude them when the variables are unused?

False positives + query ideas for variables with leading underscores.

js/unused-local-variable has some false positives for variables that are intentionally unused, e.g. for array destructuring, etc...

An example can be seen in Shopify/quilt#434

It's a convention that local variables with leading underscores are designed to be unread, e.g:

const [a, _, c, d, e, f] = something
// or
const [a, _b, _c, d, e, f] = something

Moreover, tslint recently temporarily deprecated their no-unused-variable rule in favour of the new compiler flags which take leading underscores into account.

I suggest we:

  • ignore variables that start with an underscore in js/unused-local-variable
  • create a new query that finds variables that suggests they should be unused, but are actually used.

Java: FP in java/index-out-of-bounds when index known to be less than the length of the array

Query Timeout (UnsafeUseOfStrcat.ql)

The UnsafeUseOfStrcat.ql query reliably times out when run against the open source Pandas project when built on Linux, Python 3. I haven't tried running on Windows, so not sure if that's relevant.

[2018-10-18 15:29:16] [analysis] [EVALUATION 25/111] [FAIL] Error running query semmlecode-cpp-queries/Likely Bugs/Memory Management/UnsafeUseOfStrcat.ql: ExecutionFailed
                                 aborted query after 600014ms: the timeout was 600000ms.
[2018-10-18 15:29:16] [analysis]  Internal error during analysis for pandas - revision-2018-October-18--14-50-37:
                                 java.util.concurrent.CancellationException: Server shutdown
                                 com.semmle.queryserver.client.rpc.AbstractProtoBufClient.close(AbstractProtoBufClient.java:205)
                                 com.semmle.queryserver.client.CombinedQueryServerClient.close(CombinedQueryServerClient.java:204)
                                 com.semmle.cli.clientserver.client.SnapshotQueryRunner$Job.lambda$null$1(SnapshotQueryRunner.java:539)
                                 java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:602)
                                 java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
                                 java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)
                                 java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:1962)
                                 com.semmle.api.Java8EvaluationRun$1.acceptResult(Java8EvaluationRun.java:41)
                                 com.semmle.api.Java8EvaluationRun$1.acceptResult(Java8EvaluationRun.java:27)
                                 com.semmle.queryserver.client.TransformResultHandler.acceptResult(TransformResultHandler.java:24)
                                 com.semmle.queryserver.client.rpc.AbstractProtoBufClient$PairedResponseHandler.handleMessage(AbstractProtoBufClient.java:304)
                                 com.semmle.queryserver.client.rpc.AbstractProtoBufClient$ServerReadThread.run(AbstractProtoBufClient.java:402)

JS: Issue with destructuring defaults referencing variables previously destructured on the same object

Per https://lgtm.com/projects/g/brettz9/webappfind/snapshot/3109d90b09b73d445f05fc17b71566bf38721aa7/files/executable-builder/executable.js?sort=name&dir=ASC&mode=heatmap&showExcluded=false#xddf739a1f6a2cb51:1 ,

..."Variable 'parentNode' always evaluates to false here."

However, this is not the case.

To illustrate with a simpler example, in the following code:

     var obj = {a: 5};
     var {a, b = a + 10} = obj;

...a from obj is destructured first to the local a, and it is then available to act as the default for b if b on obj is undefined (which in this case it is). As you can confirm in a modern JavaScript console, b will be 15 here because a is in fact available...

In my code, parentNode will not always evaluate to false because the local parentNode will be set based on the one present on target. In fact, it would, in my case, almost never be falsey...

Python: location of py/iter-returns-non-iterator can be confusing

See https://discuss.lgtm.com/t/python-false-positive-the-iter-method-of-iterable-class-xxx-does-not-return-an-iterator/2116

In the case where __iter__ returns an instance of a class that partially implements an iterator (e.g. implements __next__ but not __iter__), then the alert location and message can be confusing, as the problem actually lies with the definition of the iterator, and not the use of it.

As a suggested improvement, when returning an instance of a class that implements __next__ but not __iter__, the alert location should instead be the iterator class, and the message something like "This class is returned as an iterator {here}, but is missing a definition for __next__, so does not fully implement the iterator interface."

Non-alert-suppression despite code comment

Despite my having an alert-suppression comment (at https://lgtm.com/projects/g/brettz9/C2D2/snapshot/4f8a73b02cc0ff4bc44c09a3ee6de709195d5712/files/explorercanvas/silverlight/excanvas.js?sort=name&dir=ASC&mode=heatmap&showExcluded=false#V158 ) and despite your site having my latest code, I find the alert is still showing. I guess this is because it is a multiline comment, but as your docs refer to the semi-colon, I was expecting it needed to be at the end of the statement...

Update: In the meantime, I just added a commit to add the alert suppression comment on the same line as the document.write, so will see what happens...

False positive in js/unused-local-variable with aliased destructuring assignments

As detailed here: Shopify/quilt#434 (comment)

When doing object destructuring with new variable names, we're determining the name of the variable incorrectly, and selecting the name of the property from the object being destructured instead.

E.g:

const {foo: bar} = {foo: 'foo', bar: 'bar'}
// foo is undefined
// bar is now a constant variable

Example FP: https://lgtm.com/projects/g/Shopify/quilt/snapshot/b8540654494cf2f30293faccbaee3b4c19287e76/files/packages/react-google-analytics/src/GaJS.tsx#xf859505316cad831:1

JS: false positive: `yield import blah`

Following up on https://discuss.lgtm.com/t/false-positive-yield-import-results-in-syntax-error/1883/2 which frustratingly I am unable to post to. (I opened the issue, got a reply with a question, posted my reply to the question, but got told by the interface that my reply would not appear until it was approved by an owner or moderator or staff member or whatever, and then got another reply prodding me to please answer the question I already answered but which apparently will sit in a moderator queue for who-knows-how-long. And now the interface doesn't even offer the option of replying. Fortunately, the last reply included a link to this repo so here we are...)

My original report:

lgtm flags yield import blah as a Syntax Error https://lgtm.com/rules/1800086/ 2. Perhaps lgtm is using acorn-dynamic-imports prior to 2.0.2?

lgtm staff reply:

could you provide a link to the project in question please?

My reply which never got posted:

https://lgtm.com/projects/g/ilios/frontend/snapshot/dist-1507417986286-1552400119401/files/app/components/programyear-objective-list.js?sort=name&dir=ASC&mode=heatmap#xa9a0de30b91adc3e:1

Thanks!

Inconsistent naming of stripParens and getProperExpr

The Java and JavaScript ql libraries have inconsistent naming for Expr member predicates that effectively do the same thing:

JavaScript: stripParens
https://github.com/Semmle/ql/blob/742c9708a9888c59391b49041ebb2f18ef2e1ffb/javascript/ql/src/semmle/javascript/Expr.qll#L83

Java: getProperExpr
https://github.com/Semmle/ql/blob/9012c3240f347655f859aa006817ff008b40aaf9/java/ql/src/semmle/code/java/Expr.qll#L50

I feel that we should unify the naming here, and deprecate whichever one we choose to no longer use, as this caused me some confusion when i was trying to find the Java version of this. IMO stripParens is a somewhat more obvious and intuitive name.

Python FP in py/non-iterable-in-for-loop

There appears to be an inconsistency for when py/non-iterable-in-for-loop is reported. It is reported for a list completion in the definition of an inner class, but not for the same list completion directly in the function body.

Alert

Forum Post

C++: FP in cpp/large-parameter with overloaded binary oeprators

In C++, one pattern for implementing operator+ and other overloaded binary operators is the following:

Type operator+(Type lhs, Type const& rhs ) {
   lhs += rhs;
   return lhs;
}

Exactly how this is compiled depends on both compiler version and optimization level, but it seems not to involve an additional copy at -O2 or -O3 on recent versions of the major compilers. See https://godbolt.org/z/W7BXKk for some experimentation with this.

CPP: Uninialized variable reported after while loop which always runs at least once

In our code we have this:

    bool connected = false;
    bool should_retry = true;
    int remaining_tries = MAX_CONNECT_RETRIES;
    int ret;
    while (!connected && should_retry)
    {
        ret = SSL_connect(conn_info->ssl);
        ...
    }
    if (!connected)
    {
        TLSLogError(conn_info->ssl, LOG_LEVEL_ERR,
                    "Failed to establish TLS connection", ret);
        ...

and the use of ret is reported as potentially using an uninitialized variable [1]. Which I believe cannot happen because the condition in while() is always going to be true at least the first time and thus the variable ret will get initialized.

[1] https://lgtm.com/projects/g/cfengine/core/rev/pr-8b1d1654c2026687e81b86d5d27cd081a3060ab5#1320885514

cpp: FPs in Microsoft.SAL

We noticed false positives when testing SAL annotations.

These three should demonstrate the issue:
sal.h:

#define _SAL_VERSION 20

test.cpp:

#include "sal.h"
int method1() {
    return _SAL_VERSION;
}
void method2();

test.ql:

import Microsoft.SAL
from SALAnnotation a
select a, a.getDeclaration()

Output from the query:

| test.cpp:3:12:3:23 | _SAL_VERSION | test.cpp:5:6:5:12 | method2 |

It seems the detection is based on "rank" and the fact that MacroInvocation _SAL_VERSION is before DeclarationEntry method2.

Not sure what is the best fix, can you please advise? Thank you.

Java: FP in java/abstract-to-concrete-cast when typechecked against wildcard type

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.