feat(daemon): csp nonce handling (#6587)
* Adding nonce handling * Nonce token and fix content length mismatch * Updating nonce injection for override situations * nonce handling in Content Security Policy for custom CSP scenarios * return original length in cspWriter to prevent truncation
This commit is contained in:
1 parent
af937ba6b1
commit
bf24d6607f
4 files changed
+239
-2
No files matched your search
@@ -10,6 +10,7 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -18,6 +19,41 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCspWriter_Write(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input string
|
||||
nonce string
|
||||
wantBody string
|
||||
}{
|
||||
{
|
||||
name: "replaces placeholder with nonce",
|
||||
input: "foo __BOUNDARY_CSP_NONCE__ bar",
|
||||
nonce: "'nonce-abc123'",
|
||||
wantBody: "foo 'nonce-abc123' bar",
|
||||
},
|
||||
{
|
||||
name: "no placeholder returns original unchanged",
|
||||
input: "no placeholder here",
|
||||
nonce: "'nonce-abc123'",
|
||||
wantBody: "no placeholder here",
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
w := &cspWriter{
|
||||
ResponseWriter: rec,
|
||||
nonce: tc.nonce,
|
||||
}
|
||||
n, err := w.Write([]byte(tc.input))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(tc.input), n, "must return original input length to conform to the io.Writer contract")
|
||||
assert.Equal(t, tc.wantBody, rec.Body.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUiRouting(t *testing.T) {
|
||||
// Create a temporary directory
|
||||
tempDir, err := ioutil.TempDir("", "boundary-test-")
|
||||
|
||||
Reference in new issue
Block a user