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

LibWeb: Implement Content Security Policy Level 3 #2854

Open
wants to merge 44 commits into
base: master
Choose a base branch
from

Conversation

Lubrsi
Copy link
Contributor

@Lubrsi Lubrsi commented Dec 9, 2024

This implements the core of Content Security Policy Level 3. This does not implement directives outside of the base specification yet, such as trusted-types, require-trusted-types-for and upgrade-insecure-requests. Additionally, the report-to directive does not have an effect yet, as it relies on implementing the Reporting specification.

There are several implementations and fixes done on the side to support this implementation, such as implementing a new GenericLexer method and a new TC39 Stage 3 proposal called "Dynamic Code Brand Checks".

There are several specification issues that I found whilst implementing this. They are marked with FIXMEs through this change set. I haven't filed them yet as there are many and could already be known about, as there are 178 open issues on their GitHub repository. However, I thought I'd get this open for review before I start filing spec issues :^).

Additionally, this does not import any WPT tests, as it relies on the WPT server to check CSP reports, and it does some cross-origin resource requesting that confuses the WPT importer, as it'll treat such requests as part of the path of the wpt.live origin, instead of being a different origin entirely. This PR is big enough as is, without going into fixing the WPT importer.

@Lubrsi Lubrsi requested a review from alimpfard as a code owner December 9, 2024 19:46
.fetch_client = relevant_settings_object(),
.source_policy_container = m_policy_container
};
auto snapshot_params = realm().create<HTML::SourceSnapshotParams>();
Copy link
Contributor

@shannonbooth shannonbooth Dec 9, 2024

Choose a reason for hiding this comment

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

Just something I noticed - we use realm.create<T> here and heap().allocate<T> in some other places. There is no realm initialization needed for this thing, so I wonder if we should just make it a GC::Cell instead of a JS::Cell. That way I guess this would give a compile error. Though, this doesn't solve that problem the other way around for a JS::Cell not having realm.create<T> called on it🤔

@Lubrsi Lubrsi force-pushed the content-security-policy branch 5 times, most recently from b5f6320 to e8b5745 Compare December 10, 2024 16:04
@Lubrsi Lubrsi force-pushed the content-security-policy branch from e8b5745 to 19d3b80 Compare December 11, 2024 11:01
@github-actions github-actions bot added the conflicts Pull request has merge conflicts that need resolution label Dec 17, 2024
@Lubrsi Lubrsi force-pushed the content-security-policy branch from 19d3b80 to 031928e Compare December 31, 2024 19:29
@github-actions github-actions bot removed the conflicts Pull request has merge conflicts that need resolution label Dec 31, 2024
Copy link

github-actions bot commented Jan 2, 2025

Your pull request has conflicts that need to be resolved before it can be reviewed and merged. Make sure to rebase your branch on top of the latest master.

@github-actions github-actions bot added the conflicts Pull request has merge conflicts that need resolution label Jan 2, 2025
@Lubrsi Lubrsi force-pushed the content-security-policy branch from 031928e to cb0407d Compare January 15, 2025 16:07
@github-actions github-actions bot added conflicts Pull request has merge conflicts that need resolution and removed conflicts Pull request has merge conflicts that need resolution labels Jan 15, 2025
Copy link

Your pull request has conflicts that need to be resolved before it can be reviewed and merged. Make sure to rebase your branch on top of the latest master.

@Lubrsi Lubrsi force-pushed the content-security-policy branch from cb0407d to 0161a98 Compare January 24, 2025 15:22
@github-actions github-actions bot added conflicts Pull request has merge conflicts that need resolution and removed conflicts Pull request has merge conflicts that need resolution labels Jan 24, 2025
Copy link

Your pull request has conflicts that need to be resolved before it can be reviewed and merged. Make sure to rebase your branch on top of the latest master.

@Lubrsi Lubrsi force-pushed the content-security-policy branch from 0161a98 to b6e07d9 Compare January 29, 2025 16:13
@github-actions github-actions bot added conflicts Pull request has merge conflicts that need resolution and removed conflicts Pull request has merge conflicts that need resolution labels Jan 29, 2025
Copy link

Your pull request has conflicts that need to be resolved before it can be reviewed and merged. Make sure to rebase your branch on top of the latest master.

This is required to store Content Security Policies, as their
Directives are implemented as subclasses with overridden virtual
functions. Thus, they cannot be stored as generic Directive classes, as
it'll lose the ability to call overridden functions when they are
copied.
These form the basis of Content Security Policy. A policy is a
collection of directives that are parsed from either the
Content-Security-Policy(-Report-Only) HTTP header, or the `<meta>`
element.

The directives are what restrict the operations can be performed in the
current global execution context. For example, "frame-ancestors: none"
tells us to prevent the page from being loaded in an embedded context,
such as `<iframe>`.

You can see it a bit like OpenBSD's pledge() functionality, but for the
web platform: https://man.openbsd.org/pledge.2
This allows us to parse the Content-Security-Policy header and
Referrer-Policy header from navigation responses and actually allow
them to start having an effect.
Lubrsi added 26 commits February 5, 2025 14:18
This currently doesn't do anything, as the font loading code is using
ResourceLoader directly. See LadybirdBrowser#2634.
This is required for CSP to ignore the nonce attribute to prevent
duplicate attributes hijacking the attribute.

See https://w3c.github.io/webappsec-csp/#security-nonce-hijacking
This is an active proposal at stage 3 of the TC39 proposal process.
See: https://tc39.es/proposal-dynamic-code-brand-checks/
See: https://github.com/tc39/proposal-dynamic-code-brand-checks

This proposal essentially adds support for the TrustedScript type from
the Trusted Types specification to eval and Function. This in turn
pipes support for the type into the CSP hook to check if the CSP allows
dynamic code compilation.

However, it currently doesn't support ShadowRealms, so the
implementation here is a close approximation, using PerformEval as the
basis.
See: tc39/proposal-dynamic-code-brand-checks#19

This is required to support the new function signature for the CSP
hook, and will allow us to slot in Trusted Types support in the future.
This doesn't do anything by itself, the report a violation algorithm
will handle this directive itself.
This doesn't do anything by itself, the report a violation algorithm
will handle this directive itself.
This fixes the frame-ancestors WPT tests from crashing when an iframe
is blocked from loading. This is because it would get an undefined
location.href from the cross-origin iframe, which causes a crash as it
expects it to be there.
@Lubrsi Lubrsi force-pushed the content-security-policy branch from b6e07d9 to 9639b26 Compare February 5, 2025 14:26
@github-actions github-actions bot added conflicts Pull request has merge conflicts that need resolution and removed conflicts Pull request has merge conflicts that need resolution labels Feb 5, 2025
Copy link

github-actions bot commented Feb 5, 2025

Your pull request has conflicts that need to be resolved before it can be reviewed and merged. Make sure to rebase your branch on top of the latest master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conflicts Pull request has merge conflicts that need resolution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants