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

taint on empty strings #4620

Closed
staabm opened this issue Nov 19, 2020 · 5 comments
Closed

taint on empty strings #4620

staabm opened this issue Nov 19, 2020 · 5 comments

Comments

@staabm
Copy link
Contributor

staabm commented Nov 19, 2020

I dont expect a error in this case

https://psalm.dev/r/a020e4ddc2

as soon as I drop the if ($file) { it works like expected.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/a020e4ddc2
<?php // --taint-analysis

    function importScript(string $filename)
    {
        if (is_file($filename)) {
            $c = file_get_contents($filename);
        }
    }


$file = $_GET['filename'];

if ($file != '') {
    
    /**
     * @psalm-taint-escape text
     */
    $file = basename($file);
}

importScript($file);
Psalm output (using commit 95de6cf):

ERROR: TaintedText - 6:36 - Detected tainted text

@staabm
Copy link
Contributor Author

staabm commented Nov 20, 2020

the initially reported taint error is still reproducible

https://psalm.dev/r/a020e4ddc2

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/a020e4ddc2
<?php // --taint-analysis

    function importScript(string $filename)
    {
        if (is_file($filename)) {
            $c = file_get_contents($filename);
        }
    }


$file = $_GET['filename'];

if ($file != '') {
    
    /**
     * @psalm-taint-escape text
     */
    $file = basename($file);
}

importScript($file);
Psalm output (using commit ccf6e28):

ERROR: TaintedFile - 6:36 - Detected tainted file handling

@weirdan
Copy link
Collaborator

weirdan commented Nov 20, 2020

the initially reported taint error is still reproducible

At the very least it's titled misleadingly - there's no guarantee that $_GET['filename'] contains a string, as evidenced by type errors reported by Psalm: https://psalm.dev/r/d25318bfb9

If you make sure it's a string (e.g. by using strict comparison) the taint is gone: https://psalm.dev/r/3fd7e9379c

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/d25318bfb9
<?php // --no-taint-analysis

    function importScript(string $filename): void
    {
        if (is_file($filename)) {
            $c = file_get_contents($filename);
        }
    }

if (!isset($_GET['filename'])) throw new RuntimeException('missing');
$file = $_GET['filename'];

if ($file != '') {
    
    /**
     * @psalm-taint-escape text
     */
    $file = basename($file);
}

importScript($file);
Psalm output (using commit ccf6e28):

INFO: MixedAssignment - 11:1 - Unable to determine the type that $file is being assigned to

INFO: MixedArgument - 18:22 - Argument 1 of basename cannot be mixed, expecting string

INFO: MixedArgument - 21:14 - Argument 1 of importScript cannot be mixed|string, expecting string
https://psalm.dev/r/3fd7e9379c
<?php // --taint-analysis

    function importScript(string $filename): void
    {
        if (is_file($filename)) {
            $c = file_get_contents($filename);
        }
    }

if (!isset($_GET['filename'])) throw new RuntimeException('missing');
$file = $_GET['filename'];

if ($file !== '') {
    
    /**
     * @psalm-taint-escape text
     */
    $file = basename($file);
}

importScript($file);
Psalm output (using commit ccf6e28):

No issues!

danog pushed a commit to danog/psalm that referenced this issue Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants