Make WordPress Core

Opened 8 weeks ago

Last modified 8 weeks ago

#54214 new enhancement

Make it posible to add custom attribute to inline script

Reported by: erikdemarco Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Script Loader Keywords:
Focuses: Cc:


Currently All wordpress sites in the world will not pass Google LightHouse 'best practice' test. It will always fail "Ensure CSP is effective against XSS attacks"

The problem is. To enable CSP we need to add 'nonce' attributes to all <script> tag. (https://web.dev/csp-xss/?utm_source=lighthouse&utm_medium=devtools)

We can only add this to external script by hooking into 'script_loader_tag'.

But for inline script (which added using 'wp_add_inline_script') its not possible to add nonce attribute because its hardcoded by WP (https://github.com/WordPress/WordPress/blob/2cb4ebefe2ee98fc36a5962e92590cb0451ad2a6/wp-includes/class.wp-scripts.php#L365)

So currently no way for wordpress sites to apply CSP other than modifying core wp files directly.

Change History (2)

#1 @audrasjb
8 weeks ago

  • Component changed from General to Script Loader

Related: #51124

#2 @swissspidy
8 weeks ago

See also https://make.wordpress.org/core/2021/02/23/introducing-script-attributes-related-functions-in-wordpress-5-7/

Those functions were added specifically because of CSP.

The goal is to eventually convert all usage in WP core to use these functions.

Note: See TracTickets for help on using tickets.