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

Set nonces on <script> and <style> elements if configured #593

Merged
merged 2 commits into from
May 12, 2024

Conversation

nathanalderson
Copy link
Contributor

We use a strict Content Security Policy (CSP) on our site which disallows inline scripts and inline styles unless they include the correct nonce value. This change allows the user to configure keys that, if given, will be used to look up nonces in the conn.assigns which are then used on the corresponding <script> and <style> elements.

Configuration looks like this:

get "/swaggerui", OpenApiSpex.Plug.SwaggerUI,
  ...
  csp_nonce_assign_key: %{script: :script_src_nonce, style: :style_src_nonce}

Or to use the same nonce for both:

get "/swaggerui", OpenApiSpex.Plug.SwaggerUI,
  ...
  csp_nonce_assign_key: :nonce

This configuration matches the way this is handled by phoenix_live_dashboard and Oban Web.

If no keys are configured the nonce property is omitted, so this should be entirely backward compatible.

@nathanalderson nathanalderson marked this pull request as draft January 26, 2024 20:45
@nathanalderson
Copy link
Contributor Author

Converting to draft because I just realized I need to handle the script on the OAuth2Redirect Plug as well.

@nathanalderson nathanalderson marked this pull request as ready for review January 26, 2024 21:27
@nathanalderson
Copy link
Contributor Author

nathanalderson commented Jan 26, 2024

Okay, I handled OpenApiSpex.Plug.SwaggerUIOAuth2Redirect as well. I had to change the inline script from:

<body onload="run()">
  <script>
    function run() {...}
  </script>
</body>

to:

<head>
  <script>
    (function run() {...})();
  </script>
</head>

Because from my reading, there's no way to set a nonce on an event handler. Setting a nonce value on both the <script> tag and the <body> tag did not work.

Copy link
Collaborator

@mbuhot mbuhot left a comment

Choose a reason for hiding this comment

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

👍

@mbuhot mbuhot merged commit 78cf5fd into open-api-spex:master May 12, 2024
hamir-suspect added a commit to renderedtext/open_api_spex that referenced this pull request May 14, 2024
* Exclude empty paths from spec (open-api-spex#583)

* Exclude empty paths from spec

* fix: assert_operation_response header lookup (open-api-spex#584)

* fix: assert_operation_response header lookup

* Release version 3.18.1

* Fix 'AllOf cast returns a map, but I expected a struct' (open-api-spex#592)

* Add failing test

* Cast result of AllOf cast into a struct

* Shorter module name

* Add missing NoneCache test

* Release version 3.18.2

* Relax dependency constraint on ymlr to allow version ~> 5.0 (open-api-spex#586)

* relax dependency on ymlr, and fix some tests

* test with more elixir versions

* Update Elixir version test matrix (open-api-spex#602)

* Update Elixir version test matrix

* Fix map key order dependent test

* Release version 3.18.3

* Support response code ranges

See: https://swagger.io/docs/specification/describing-responses/

* Release version 3.19.0

* Add notice that body params are not merged into Conn.params whne using cast and validate plug (open-api-spex#589)

* Set nonces on <script> and <style> elements if configured (open-api-spex#593)

* Allow script and style nonces

* Allow nonces on the SwaggerUIOAuth2Redirect plug as well

---------

Co-authored-by: Alisina Bahadori <[email protected]>
Co-authored-by: Matt Sutkowski <[email protected]>
Co-authored-by: Dimitris Zorbas <[email protected]>
Co-authored-by: Angelika Tyborska <[email protected]>
Co-authored-by: Aleksandr Lossenko <[email protected]>
Co-authored-by: Nathan Alderson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants