-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Allow plugin settings to add connect-src CSP sources #7166
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
Open
cc1234475
wants to merge
6
commits into
stashapp:develop
Choose a base branch
from
cc1234475:csp-plugin-settings
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c87106f
feat: allow plugin settings to add connect-src CSP sources
cc1234475 119c512
fix: reject CSP-breaking characters in plugin csp_ setting URLs
cc1234475 c3ebc85
docs: document csp_ plugin settings as connect-src sources
cc1234475 c77bef4
style: gofmt server_test.go
cc1234475 cf532bf
fix: address PR #7166 review comments
cc1234475 70ded0a
fix: gate plugin config read on opt-in, dedupe CSP warnings, test rea…
cc1234475 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stashapp/stash/pkg/plugin" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestCspConnectSrcFromSettings(t *testing.T) { | ||
| for _, tt := range []struct { | ||
| name string | ||
| settings map[string]interface{} | ||
| valid []string | ||
| skippedKeys []string | ||
| }{ | ||
| { | ||
| name: "no csp keys", | ||
| settings: map[string]interface{}{"foo": "bar", "other_setting": "https://x.com"}, | ||
| valid: nil, | ||
| }, | ||
| { | ||
| name: "valid https url", | ||
| settings: map[string]interface{}{"csp_x": "https://api.example.com"}, | ||
| valid: []string{"https://api.example.com"}, | ||
| }, | ||
| { | ||
| name: "valid http url", | ||
| settings: map[string]interface{}{"csp_x": "http://localhost:7860"}, | ||
| valid: []string{"http://localhost:7860"}, | ||
| }, | ||
| { | ||
| name: "disallowed scheme", | ||
| settings: map[string]interface{}{"csp_x": "ftp://example.com"}, | ||
| skippedKeys: []string{"csp_x"}, | ||
| }, | ||
| { | ||
| name: "not a url", | ||
| settings: map[string]interface{}{"csp_x": "not a url"}, | ||
| skippedKeys: []string{"csp_x"}, | ||
| }, | ||
| { | ||
| name: "non string value", | ||
| settings: map[string]interface{}{"csp_x": 123}, | ||
| skippedKeys: []string{"csp_x"}, | ||
| }, | ||
| { | ||
| name: "wildcard host", | ||
| settings: map[string]interface{}{"csp_x": "http://*:7860"}, | ||
| skippedKeys: []string{"csp_x"}, | ||
| }, | ||
| { | ||
| name: "csp directive breakout via semicolon in path", | ||
| settings: map[string]interface{}{"csp_x": "https://evil.com/; script-src 'none'"}, | ||
| skippedKeys: []string{"csp_x"}, | ||
| }, | ||
| { | ||
| name: "whitespace in path", | ||
| settings: map[string]interface{}{"csp_x": "https://evil.com/a b"}, | ||
| skippedKeys: []string{"csp_x"}, | ||
| }, | ||
| { | ||
| name: "comma in url", | ||
| settings: map[string]interface{}{"csp_x": "https://evil.com/a,b"}, | ||
| skippedKeys: []string{"csp_x"}, | ||
| }, | ||
| { | ||
| name: "degenerate host port only", | ||
| settings: map[string]interface{}{"csp_x": "https://:7860"}, | ||
| skippedKeys: []string{"csp_x"}, | ||
| }, | ||
| { | ||
| name: "userinfo in url", | ||
| settings: map[string]interface{}{"csp_x": "https://user@attacker.com"}, | ||
| skippedKeys: []string{"csp_x"}, | ||
| }, | ||
| { | ||
| name: "empty string", | ||
| settings: map[string]interface{}{"csp_x": ""}, | ||
| skippedKeys: []string{"csp_x"}, | ||
| }, | ||
| { | ||
| name: "mixed valid and invalid", | ||
| settings: map[string]interface{}{"csp_a": "https://api.example.com", "csp_b": "javascript:alert(1)", "csp_c": "http://localhost:7860", "other": "ignored"}, | ||
| valid: []string{"https://api.example.com", "http://localhost:7860"}, | ||
| skippedKeys: []string{"csp_b"}, | ||
| }, | ||
| } { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| valid, skippedKeys := cspConnectSrcFromSettings(tt.settings) | ||
| assert.ElementsMatch(t, tt.valid, valid) | ||
| assert.ElementsMatch(t, tt.skippedKeys, skippedKeys) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestSetPageSecurityHeaders_CSPSettingsOptIn(t *testing.T) { | ||
|
cc1234475 marked this conversation as resolved.
Outdated
|
||
| // Plugin with CSPSettings enabled — csp_ settings should appear in connect-src | ||
| t.Run("opt-in enabled", func(t *testing.T) { | ||
| plugins := []*plugin.Plugin{ | ||
| { | ||
| Enabled: true, | ||
| UI: plugin.PluginUI{ | ||
| CSPSettings: true, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| assert.True(t, plugins[0].UI.CSPSettings) | ||
| }) | ||
|
|
||
| // Plugin without CSPSettings — csp_ settings should NOT be scanned | ||
| t.Run("opt-in disabled", func(t *testing.T) { | ||
| plugins := []*plugin.Plugin{ | ||
| { | ||
| Enabled: true, | ||
| UI: plugin.PluginUI{ | ||
| CSPSettings: false, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| assert.False(t, plugins[0].UI.CSPSettings) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.