# Support Nonces for CSP Inline JS Compliance \[RESOLVED\]

**URL:** <https://community.gravityforms.com/t/support-nonces-for-csp-inline-js-compliance-resolved/10499>\
**Category:** General Discussion\
**Tags:** nonce, csp\
**Created:** [August 3, 2021, 5:39pm UTC](https://community.gravityforms.com/t/support-nonces-for-csp-inline-js-compliance-resolved/10499 "2021-08-03T17:39:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![user5c6c672bc37734.4](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user5c6c672bc37734.4/32/3356_2.png) [@user5c6c672bc37734.4](https://community.gravityforms.com/u/user5c6c672bc37734.4)\
**Post date:** [August 3, 2021, 5:39pm UTC](https://community.gravityforms.com/t/support-nonces-for-csp-inline-js-compliance-resolved/10499/1 "2021-08-03T17:39:37Z")

</div>

This has been brought up before, but I think it needs real addressing so that site owners can implement good Content Security Policies (CSP). I have also made a request through the road map. I thought I would outline the issue here and see if anyone else has an issue with this and if they have found a way to get around it until this feature is implemented.

A post was made back in Mar 2020 ([Content-Security-Policy (CSP) header validation](https://community.gravityforms.com/t/content-security-policy-csp-header-validation/4205)) and was quickly shut down as a “contact support”. Really this is a feature that helps with security of sites and those who wish to implement correct security policies.

The main thing to address is the addition of some JS code that is inserted into the `<head>` tag of pages that contain forms on them. This beginning part of this code is shown below…

```auto
<script type="text/javascript">
if(!gform){document.addEventListener("gform_main_scripts_loaded"...
</script>

```

Honestly I think this is a bit of a hack, but whatever works I guess as long as it is following CSP guidelines of supporting a nonce attribute. For reference of CSP you can checkout this page… [CSP: script-src - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src)

Without the `<script type="text/javascript">` supporting a nonce attribute, site owners are going to be forced to support `unsafe-inline` as part of their `script-src` policy.

You can find the code that implements this JS insertion in common.php, function output\_hooks\_javascript() around line 5666.

What I propose is simply adding a setting that allows users to enter a nonce they can use (or even have it generated but retrievable through code) that then gets printed in that `output_hooks_javascript` function.

I see the amount of effort for this rather low and would be a good service to your site owner community.

If anyone has any thoughts, please do let me know.

Thanks!

---

<div class="post-metadata">

**Author:** ![richardw8k](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/richardw8k/32/2354_2.png) [@richardw8k](https://community.gravityforms.com/u/richardw8k)\
**Post date:** [August 4, 2021, 4:57pm UTC](https://community.gravityforms.com/t/support-nonces-for-csp-inline-js-compliance-resolved/10499/2 "2021-08-04T16:57:13Z")

</div>

Since the 2.5.8 release you can use the WordPress 5.7+ wp\_inline\_script\_attributes filter to add the CSP nonce to the scripts.

> **[Introducing script attributes related functions in WordPress 5.7](https://make.wordpress.org/core/2021/02/23/introducing-script-attributes-related-functions-in-wordpress-5-7/)**
>
> WordPress 5.7 adds a handful of new functions that enables passing attributes, such as async or nonce, to both regular and inline script tag attributes. This creates a path forward f…

---

<div class="post-metadata">

**Author:** ![user5c6c672bc37734.4](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user5c6c672bc37734.4/32/3356_2.png) [@user5c6c672bc37734.4](https://community.gravityforms.com/u/user5c6c672bc37734.4)\
**Post date:** [August 4, 2021, 6:49pm UTC](https://community.gravityforms.com/t/support-nonces-for-csp-inline-js-compliance-resolved/10499/3 "2021-08-04T18:49:15Z")

</div>

I am not sure that works. Those functions would work if we were using those functions to generate the `<script>` tags to begin with. But Gravity forms actually just echos out the actual `<script type="text/javascript">` tag in common.php, function `output_hooks_javascript()` on line around 5666. I don’t think WP even really sees such a tag.

Perhaps I am reading it wrong, but my initial testing shows that it doesn’t impact any script tags that a plugin or theme may directly be echoing out. Now I think that if Gravity Forms was to use one of those functions to actually output the `<script>` tag, THEN we would get access to filter them.

Am I missing something?

Edit: Oh I see you guys are calling this code from multiple locations and not just common.php. I also see you are using those functions. Looks like I will need to do further testing.

---

<div class="post-metadata">

**Author:** ![user5c6c672bc37734.4](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user5c6c672bc37734.4/32/3356_2.png) [@user5c6c672bc37734.4](https://community.gravityforms.com/u/user5c6c672bc37734.4)\
**Post date:** [August 4, 2021, 8:33pm UTC](https://community.gravityforms.com/t/support-nonces-for-csp-inline-js-compliance-resolved/10499/4 "2021-08-04T20:33:42Z")

</div>

Ok well after significant digging through gravity forms and through the WP codebase and documentation from that link you posted, I realized the documentation for showing the attribute filtering is wrong. Here is the wrong info and how it is actually…

> Example of use  
> Adds a nonce attribute to every script tag generated by either wp\_get\_script\_tag or wp\_get\_inline\_script\_tag:
> 
> ```auto
> function wporg_my_wp_script_attributes( $attr ) {
> if ( ! isset( $attr['nonce'] ) ) {
> $attr['nonce'] = get_my_custom_nonce(); // Random custom function
> }
> return $attr;
> );
> add_filter( 'wp_script_attributes', 'wporg_my_wp_script_attributes' );
> 
> ```

In fact inline scripts are using the filter `wp_inline_script_attributes`. So the statement about `wp_script_attributes` does not apply to `wp_get_inline_script_tag`. Once I discovered this, this filter will work for adding the required nonce.

Thanks for the help. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/f/fef63f0599c45e0218c30d9f5d7ca8bff9b00189.png) [@system](https://community.gravityforms.com/u/system)\
**Post date:** [August 5, 2021, 8:34pm UTC](https://community.gravityforms.com/t/support-nonces-for-csp-inline-js-compliance-resolved/10499/5 "2021-08-05T20:34:36Z")

</div>

Closed on August 5, 2021, 8:34pm UTC

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
