Safety checks your installed dependencies for known security vulnerabilities.
By default it uses the open Python vulnerability database Safety DB,
but can be upgraded to use pyup.io's Safety API using the --key option.
Install safety with pip
pip install safetyTo check your currently selected virtual environment for dependencies with known security vulnerabilites, run:
safety checkYou should get a report similar to this:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β /$$$$$$ /$$ β
β /$$__ $$ | $$ β
β /$$$$$$$ /$$$$$$ | $$ \__//$$$$$$ /$$$$$$ /$$ /$$ β
β /$$_____/ |____ $$| $$$$ /$$__ $$|_ $$_/ | $$ | $$ β
β | $$$$$$ /$$$$$$$| $$_/ | $$$$$$$$ | $$ | $$ | $$ β
β \____ $$ /$$__ $$| $$ | $$_____/ | $$ /$$| $$ | $$ β
β /$$$$$$$/| $$$$$$$| $$ | $$$$$$$ | $$$$/| $$$$$$$ β
β |_______/ \_______/|__/ \_______/ \___/ \____ $$ β
β /$$ | $$ β
β | $$$$$$/ β
β by pyup.io \______/ β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ‘
β REPORT β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ‘
β No known security vulnerabilities found. β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββNow, let's install something insecure:
pip install insecure-packageYeah, you can really install that.
Run safety check again:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β /$$$$$$ /$$ β
β /$$__ $$ | $$ β
β /$$$$$$$ /$$$$$$ | $$ \__//$$$$$$ /$$$$$$ /$$ /$$ β
β /$$_____/ |____ $$| $$$$ /$$__ $$|_ $$_/ | $$ | $$ β
β | $$$$$$ /$$$$$$$| $$_/ | $$$$$$$$ | $$ | $$ | $$ β
β \____ $$ /$$__ $$| $$ | $$_____/ | $$ /$$| $$ | $$ β
β /$$$$$$$/| $$$$$$$| $$ | $$$$$$$ | $$$$/| $$$$$$$ β
β |_______/ \_______/|__/ \_______/ \___/ \____ $$ β
β /$$ | $$ β
β | $$$$$$/ β
β by pyup.io \______/ β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ‘
β REPORT β
ββββββββββββββββββββββββββββ€ββββββββββββββββ€ββββββββββββββββββββ€ββββββββββββββββ‘
β package β installed β affected β source β
ββββββββββββββββββββββββββββ§ββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββ‘
β insecure-package β 0.1.0 β <0.2.0 β changelog β
ββββββββββββββββββββββββββββ§ββββββββββββββββ§ββββββββββββββββββββ§ββββββββββββββββJust like pip, Safety is able to read local requirement files:
safety check -r requirements.txtSafety is also able to read from stdin with the --stdin flag set.
To check a local requirements file, run:
cat requirements.txt | safety check --stdin
or the output of pip freeze:
pip freeze | safety check --stdin
or to check a single package:
echo "insecure-package==0.1" | safety check --stdin
For more examples, take a look at the options section.
Safety works great in your CI pipeline. It returns a non-zero exit status if it finds a vulnerability.
Run it before or after your tests. If Safety finds something, your tests will fail.
Travis
install:
- pip install safety
script:
- safety check
Deep GitHub Integration
If you are looking for a deep integration with your GitHub repositories: Safety is available as a part of pyup.io, called Safety CI. Safety CI checks your commits and pull requests for dependencies with known security vulnerabilities and displays a status on GitHub.
Safety is free and open source (MIT Licensed). The underlying open vulnerability database is updated once per month.
To get access to all vulnerabilites as soon as they are added, you need a Safety API key that comes with a paid pyup.io account, starting at $14.99 for individuals, or $49.99 for organizations.
API Key for pyup.io's vulnerability database. Can be set as SAFETY_API_KEY environment variable.
Example
safety check --key=12345-ABCDEFGHPath to a directory with a local vulnerability database including insecure.json and insecure_full.json
Example
safety check --db=/home/safety-db/dataOutput vulnerabilities in JSON format.
Example
safety check --json[
[
"django",
"<1.2.2",
"1.2",
"Cross-site scripting (XSS) vulnerability in Django 1.2.x before 1.2.2 allows remote attackers to inject arbitrary web script or HTML via a csrfmiddlewaretoken (aka csrf_token) cookie.",
"25701"
]
]Full reports include a security advisory (if available).
Example
safety check --full-reportββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β /$$$$$$ /$$ β
β /$$__ $$ | $$ β
β /$$$$$$$ /$$$$$$ | $$ \__//$$$$$$ /$$$$$$ /$$ /$$ β
β /$$_____/ |____ $$| $$$$ /$$__ $$|_ $$_/ | $$ | $$ β
β | $$$$$$ /$$$$$$$| $$_/ | $$$$$$$$ | $$ | $$ | $$ β
β \____ $$ /$$__ $$| $$ | $$_____/ | $$ /$$| $$ | $$ β
β /$$$$$$$/| $$$$$$$| $$ | $$$$$$$ | $$$$/| $$$$$$$ β
β |_______/ \_______/|__/ \_______/ \___/ \____ $$ β
β /$$ | $$ β
β | $$$$$$/ β
β by pyup.io \______/ β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ‘
β REPORT β
ββββββββββββββββββββββββββββββ€ββββββββββββ€βββββββββββββββββββββββββββ€βββββββββββ‘
β package β installed β affected β ID β
ββββββββββββββββββββββββββββββ§ββββββββββββ§βββββββββββββββββββββββββββ§βββββββββββ‘
β django β 1.2 β <1.2.2 β 25701 β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ‘
β Cross-site scripting (XSS) vulnerability in Django 1.2.x before 1.2.2 allows β
β remote attackers to inject arbitrary web script or HTML via a csrfmiddlewar β
β etoken (aka csrf_token) cookie. β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Output vulnerable packages only. Useful in combination with other tools.
Example
safety check --barecryptography django
Cache requests to the vulnerability database locally for 2 hours.
Example
safety check --cacheRead input from stdin.
Example
cat requirements.txt | safety check --stdinpip freeze | safety check --stdinecho "insecure-package==0.1" | safety check --stdinRead input from one (or multiple) requirement files.
Example
safety check -r requirements.txtsafety check --file=requirements.txtsafety check -r req_dev.txt -r req_prod.txtIgnore one (or multiple) vulnerabilities by ID
Example
safety check -i 1234safety check --ignore=1234safety check -i 1234 -i 4567 -i 89101
