Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
113 lines
5.0 KiB
Go
113 lines
5.0 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const externalResourceTestSRI = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
|
|
|
|
func TestExternalResourceApprovalNormalization(t *testing.T) {
|
|
item, err := normalizeExternalResourceApproval(externalResourceApproval{
|
|
Kind: " SCRIPT ", URL: "https://CDN.Example.test/app.js?v=1", Integrity: strings.Repeat("00", 32),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if item.Kind != "script" || item.URL != "https://cdn.example.test/app.js?v=1" || item.Integrity != externalResourceTestSRI {
|
|
t.Fatalf("unexpected normalization: %+v", item)
|
|
}
|
|
for _, raw := range []string{"http://cdn.example.test/app.js", "https://user@cdn.example.test/app.js", "https://cdn.example.test/app.js#fragment", "//cdn.example.test/app.js"} {
|
|
if _, err := normalizeExternalResourceURL(raw); err == nil {
|
|
t.Fatalf("unsafe URL accepted: %s", raw)
|
|
}
|
|
}
|
|
if _, err := normalizeExternalResourceApproval(externalResourceApproval{Kind: "script", URL: "https://cdn.example.test/app.js"}); err == nil {
|
|
t.Fatal("script without SRI accepted")
|
|
}
|
|
}
|
|
|
|
func TestHTMLProjectionAllowsOnlyApprovedExternalResources(t *testing.T) {
|
|
s, _, domain := configFixture(t)
|
|
items := []externalResourceApproval{
|
|
{Kind: "image", URL: "https://media.example.test/avatar.png"},
|
|
{Kind: "stylesheet", URL: "https://cdn.example.test/profile.css", Integrity: externalResourceTestSRI},
|
|
{Kind: "script", URL: "https://cdn.example.test/profile.js", Integrity: externalResourceTestSRI},
|
|
}
|
|
if err := s.saveExternalResourceApprovals(domain, items); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := `<img src="https://media.example.test/avatar.png" onerror="alert(1)">` +
|
|
`<img src="https://media.example.test/not-approved.png">` +
|
|
`<link rel="stylesheet" href="https://cdn.example.test/profile.css" integrity="sha256-bogus">` +
|
|
`<link rel="stylesheet" href="https://cdn.example.test/other.css">` +
|
|
`<script src="https://cdn.example.test/profile.js" integrity="sha256-bogus">alert(1)</script>` +
|
|
`<script src="https://cdn.example.test/other.js"></script><script>alert(1)</script>`
|
|
got := s.safeHTMLProjection(httptest.NewRequest(http.MethodGet, "http://localhost/u/anya", nil), body, "/u/anya", true)
|
|
for _, want := range []string{
|
|
`src="https://media.example.test/avatar.png"`,
|
|
`href="https://cdn.example.test/profile.css"`,
|
|
`src="https://cdn.example.test/profile.js"`,
|
|
`integrity="` + externalResourceTestSRI + `"`,
|
|
`crossorigin="anonymous"`,
|
|
} {
|
|
if !strings.Contains(got, want) {
|
|
t.Fatalf("approved resource missing %q: %s", want, got)
|
|
}
|
|
}
|
|
for _, forbidden := range []string{"not-approved.png", "other.css", "other.js", "sha256-bogus", "onerror", ">alert(1)<"} {
|
|
if strings.Contains(got, forbidden) {
|
|
t.Fatalf("unapproved resource survived %q: %s", forbidden, got)
|
|
}
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
if _, err := setProjectionCSP(w, s.externalResourcePolicy(domain)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
csp := w.Header().Get("Content-Security-Policy")
|
|
for _, want := range []string{"https://media.example.test", "https://cdn.example.test", "'" + externalResourceTestSRI + "'", "connect-src 'self'"} {
|
|
if !strings.Contains(csp, want) {
|
|
t.Fatalf("CSP missing %q: %s", want, csp)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExternalResourceApprovalConfig(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
page := configRequest(s, token, http.MethodGet, "/-/sys/config?panel=external-resources", nil)
|
|
if page.Code != http.StatusOK || !strings.Contains(page.Body.String(), `id="panel-external-resources"`) {
|
|
t.Fatalf("config page status=%d body=%s", page.Code, page.Body.String())
|
|
}
|
|
w := configRequest(s, token, http.MethodPost, "/-/sys/config", url.Values{
|
|
"section": {"external-resource-add"}, "kind": {"image"}, "url": {"https://media.example.test/avatar.png"},
|
|
})
|
|
if w.Code != http.StatusFound {
|
|
t.Fatalf("add status=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
if _, ok := s.externalResourcePolicy(domain).approved("image", "https://media.example.test/avatar.png"); !ok {
|
|
t.Fatal("approval was not saved")
|
|
}
|
|
w = configRequest(s, token, http.MethodPost, "/-/sys/config", url.Values{
|
|
"section": {"external-resource-delete"}, "kind": {"image"}, "url": {"https://media.example.test/avatar.png"},
|
|
})
|
|
if w.Code != http.StatusFound || len(s.externalResourcePolicy(domain).items) != 0 {
|
|
t.Fatalf("delete status=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
w = configRequest(s, token, http.MethodPost, "/-/sys/config", url.Values{
|
|
"section": {"external-resource-add"}, "kind": {"script"}, "url": {"https://cdn.example.test/app.js"}, "integrity": {externalResourceTestSRI},
|
|
})
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("script risk confirmation bypassed: %d", w.Code)
|
|
}
|
|
w = configRequest(s, token, http.MethodPost, "/-/sys/config", url.Values{
|
|
"section": {"external-resource-add"}, "kind": {"script"}, "url": {"https://cdn.example.test/app.js"}, "integrity": {externalResourceTestSRI}, "confirm_script_risk": {"1"},
|
|
})
|
|
if w.Code != http.StatusFound {
|
|
t.Fatalf("confirmed script add status=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
}
|