Git Product home page Git Product logo

csp-evaluator's Introduction

CSP Evaluator Core Library

Introduction


Please note: this is not an official Google product.

CSP Evaluator allows developers and security experts to check if a Content Security Policy (CSP) serves as a strong mitigation against cross-site scripting attacks. It assists with the process of reviewing CSP policies, and helps identify subtle CSP bypasses which undermine the value of a policy. CSP Evaluator checks are based on a large-scale study and are aimed to help developers to harden their CSP and improve the security of their applications. This tool is provided only for the convenience of developers and Google provides no guarantees or warranties for this tool.

CSP Evaluator comes with a built-in list of common CSP allowlist bypasses which reduce the security of a policy. This list only contains popular bypasses and is by no means complete.

The CSP Evaluator library + frontend is deployed here: https://csp-evaluator.withgoogle.com/

Installing

This library is published to https://www.npmjs.com/package/csp_evaluator. You can install it via:

npm install csp_evaluator

Building

To build, run:

npm install && tsc --build

Testing

To run unit tests, run:

npm install && npm test

Example Usage

import {CspEvaluator} from "csp_evaluator/dist/evaluator.js";
import {CspParser} from "csp_evaluator/dist/parser.js";

const parsed = new CspParser("script-src https://google.com").csp;
console.log(new CspEvaluator(parsed).evaluate());

csp-evaluator's People

Contributors

0xiso avatar ccloes avatar ddworken avatar dependabot[bot] avatar lweichselbaum avatar rknoll avatar seirdy avatar smaury avatar zigoo0 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

csp-evaluator's Issues

www.googletagmanager.com does not need unsafe-eval for CSP bypass

Currently the evaluator believes that a bypass via www.googletagmanager.com requires unsafe-eval.
However, this endpoint hosts AngularJS: https://www.googletagmanager.com/debug/badge
Also, this endpoint returns JSONP: https://www.googletagmanager.com/debug/api/vtinfo?gtm_auth=xFSd[...]&env_id=env-3&public_id=GTM-[GTMID_HERE]&templates=&callback=element.click
Therefore, actually unsafe-eval is not needed.
Since Google Tag Manager is a very popular tool, I think it would be better if this bypass was detected.

`require-sri-for` support

Directive "require-sri-for" is not a known CSP directive.

There are probably more directives not yet added to the known ones, but I haven't gone through all of them. Also, not sure what's the policy is, i.e. you wait for WD status or not.

Don't recommend trusted-types if CSP blocks scripts

If a CSP has script-src: none or equivalent to forbid script loading, or if it has a sandbox directive to forbid script execution, the CSP evaluator shouldn't recommend requires-trusted-types-for: script because there is no script execution happening in the first place.

improve parsing of multi-value CSP headers

Intro

This might be controversial because RFC 2616 states "each separated by a comma":

It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma.

but I figured that it would be good to create such issue anyway so that you are aware and can decide

Steps to reproduce

lets assume that https://example.com responds with this headers:

Content-Security-Policy: frame-ancestors 'none'
Content-Security-Policy: object-src 'none'
Content-Security-Policy: script-src 'self' 'sha256-ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0='

put https://example.com into textarea of https://csp-evaluator.withgoogle.com/

Actual result

in textarea of https://csp-evaluator.withgoogle.com/ they will be parsed as:

Content-Security-Policy: frame-ancestors 'none', object-src 'none', script-src 'self' 'sha256-ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0='

(notice , instead of ;). Because of commas object-src + script-src will be marked as missing

Expected result

headers from steps to reproduce are parsed as

Content-Security-Policy: frame-ancestors 'none'; object-src 'none'; script-src 'self' 'sha256-ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0='

Additional info

google chrome 87 interprets headers from Steps to reproduce in a way that object-src and script-src are respected

Proposed solution

Multi value headers should be joined by using ; instead of ,

CSP evaluator doesn't support newest the newest CSP directives and keywords and breaks some policies

Like stated in #54 and #56 there are some additions to CSP that the evaluator does not recognize, which makes it inaccurate in analyzing most up-to-date policies. The directives that aren't supported include but aren't limited to:

  1. wasm-unsafe-eval #54, Mozilla
  2. inline-speculation-rules #56
  3. unsafe-hashesCSP.com, Mozilla
    Also, the evaluator gets the some keywords wrong , for example hashes, and autocompletes to sha-512- and sha-384- in stead of sha512- and sha384- which breaks the policy by prodiving inaccurate keywords.

Whitelists should be in external json files

Hello hello! Would it be possible to move the whitelists to external json files, instead of having them in the code? It would make it much easier for other projects that might use the data to consume. :)

Thanks so much!

`CspParser` wrongly split directive using `data:` source containing `;base64...`

"data" URL schemes can contain a ; when using the base64 extension.
When such scheme is used as a directive source, it seems that this ; trips the current CspParser, which wrongly splits the directive, as shown in the example below:

import { CspParser } from "csp_evaluator/dist/parser.js";

const example = "script-src 'nonce-xyz' data:image/png;base64,SGVsbG8sIFdvcmxkIQ== 'strict-dynamic'";
const { csp } = new CspParser(example);

console.log(csp)
Csp {
  directives: {
    'script-src': [ "'nonce-xyz'", 'data:image/png' ],
    'base64,sgvsbg8sifdvcmxkiq==': [ "'strict-dynamic'" ]
  }
}

Not setting directives that don't fallback to default-src should be raised as a severity finding.

Hi, thanks for this website, it's very useful.

There are multiple directives that don't fallback to default-src.

  • base-uri
  • form-action
  • frame-ancestors
  • plugin-types
  • report-uri
  • sandbox

Not setting them is the same as allowing anything for these directives.

Without some of these directive set, like form-action or base-url, it would be possible for a script to manipulate the DOM and allowing transmission of data to malicious websites.

Because of that, not setting these directives should be raised as a severity finding.

object-src [missing]

default-src 'self';
script-src 'self' cdnjs.cloudflare.com www.google-analytics.com www.googletagmanager.com;
img-src 'self' www.google-analytics.com;
style-src 'self' 'unsafe-inline' fonts.googleapis.com cdnjs.cloudflare.com;
font-src 'self' fonts.gstatic.com cdnjs.cloudflare.com;
form-action 'self';
report-uri https://scotthelme.report-uri.com/r/default/csp/enforce

With this policy in place there is a warning for object-src [missing]. Is this desired functionality considering there is default-src directive?

CSP with commas are parsed in a confusing way

Consider the input:

base-uri 'none';
default-src 'self';
object-src 'none';
style-src 'unsafe-inline';
script-src 'unsafe-inline' 'sha256-HEtTzbIgu0I33A3DZbJTheKFftQg+kS2n0OFuHExFuc='

That passes the evaluator just fine, with no warnings.

Now, consider this stronger combination of two policies:

base-uri 'none';
default-src 'self';
object-src 'none';
style-src 'unsafe-inline',
script-src 'unsafe-inline' 'sha256-HEtTzbIgu0I33A3DZbJTheKFftQg+kS2n0OFuHExFuc='

The intent of this second input is to require that a script be loaded from self AND match the given hash, if the browser supports CSP hash, by asking for the intersection of two policies. This is a stronger policy than the original policy. However, the evaluator complains that "'self' can be problematic if you host JSONP, Angular or user uploaded files" because it doesn't notice the script-src. It also complains about the style-src directive because it doesn't recognize the comma that separates the two policies.

Ideally, the evaluator should be extended to understand multiple policies joined using ,.

This example uses CSP hash, which is rare. However, I believe several people have advocated for a similar technique of combining multiple policies that uses CSP nonce instead of CSP hash, so it would be good to support this pattern.

Add support for `navigate-to`

While not currently enabled on Chrome or Firefox, CSP3 does define the navigate-to directive:

https://www.w3.org/TR/CSP3/#directive-navigate-to

I think it just needs to be added to the Directive enum:

export enum Directive {

And maybe FETCH_DIRECTIVES, even though I don't think it's technically a fetch (returning for this document), it's still a serialized-source-list:

export const FETCH_DIRECTIVES: Directive[] = [

Needs eval for Google Analytics

Is this true? I'm pretty sure that I've seen a lot of sites with GA + CSP, but without eval(). I don't think that's the case for GTM, but I'm pretty sure it is for GA.

csp.withgoogle.com

There's no contact information for https://csp.withgoogle.com/, do you know if it is maintained for new CSP directives etc? Also, the site is not mobile-friendly, and there's no github repo so I can't PR.

Trusted Types should allow the 'none' keyword

Trusted Types uses the keyword 'none' to show that no policies are allowed:

https://w3c.github.io/webappsec-trusted-types/dist/spec/#trusted-types-csp-directive

This is used to enforce Trusted Types restrictions (disabling unsafe APIs), without needing a policy to bypass these restrictions.

if (value === '\'allow-duplicates\'') {

if (value === '\'allow-duplicates\'' || value === '\'none\'') {

I believe this would fix the issue where LightHouse shows the error message 'none' seems to be an invalid keyword.

Csp-evaluator installation problem

problem

Hi! I try to install the csp-evaluator .
I have java 11 and ubuntu18
When i try to build (./do.sh build) the project ,i have the error in photo. Any ideas?

Thank you very much

Change requests from Lighthouse

GoogleChrome/lighthouse#12804 (comment)

Hey folks, we had some useful feedback about the CSP XSS audit in Lighthouse. I think the changes needed to be made here are pretty simple:

  • Only report a high severity finding on object-src if it is missing (i.e. object-src does not need to be 'none')
  • Remove the report-uri requirement and rely on the docs to recommend configuring a reporting destination.

I'm happy to work on this in g3 if you are on board with the changes :)

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.