Plugin assets, external scripts and CSP overrides (#4260)

* Add assets for plugins
* Move plugin javascript and css into separate endpoints
* Allow loading external scripts
* Add csp overrides
* Only include enabled plugins
* Move URLMap to utils
* Use URLMap for assets
* Add documentation
This commit is contained in:
WithoutPants
2023-11-19 10:41:16 +11:00
committed by GitHub
parent 4dd4c3c658
commit 222475df82
20 changed files with 621 additions and 105 deletions

30
pkg/utils/urlmap.go Normal file
View File

@@ -0,0 +1,30 @@
package utils
import "strings"
// URLMap is a map of URL prefixes to filesystem locations
type URLMap map[string]string
// GetFilesystemLocation returns the adjusted URL and the filesystem location
func (m URLMap) GetFilesystemLocation(url string) (newURL string, fsPath string) {
newURL = url
if m == nil {
return
}
root := m["/"]
for k, v := range m {
if k != "/" && strings.HasPrefix(url, k) {
newURL = strings.TrimPrefix(url, k)
fsPath = v
return
}
}
if root != "" {
fsPath = root
return
}
return
}

70
pkg/utils/urlmap_test.go Normal file
View File

@@ -0,0 +1,70 @@
package utils
import (
"testing"
)
func TestURLMap_GetFilesystemLocation(t *testing.T) {
// create the URLMap
urlMap := make(URLMap)
urlMap["/"] = "root"
urlMap["/foo"] = "bar"
empty := make(URLMap)
var nilMap URLMap
tests := []struct {
name string
urlMap URLMap
url string
wantNewURL string
wantFsPath string
}{
{
name: "simple",
urlMap: urlMap,
url: "/foo/bar",
wantNewURL: "/bar",
wantFsPath: "bar",
},
{
name: "root",
urlMap: urlMap,
url: "/baz",
wantNewURL: "/baz",
wantFsPath: "root",
},
{
name: "root",
urlMap: urlMap,
url: "/baz",
wantNewURL: "/baz",
wantFsPath: "root",
},
{
name: "empty",
urlMap: empty,
url: "/xyz",
wantNewURL: "/xyz",
wantFsPath: "",
},
{
name: "nil",
urlMap: nilMap,
url: "/xyz",
wantNewURL: "/xyz",
wantFsPath: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotNewURL, gotFsPath := tt.urlMap.GetFilesystemLocation(tt.url)
if gotNewURL != tt.wantNewURL {
t.Errorf("URLMap.GetFilesystemLocation() gotNewURL = %v, want %v", gotNewURL, tt.wantNewURL)
}
if gotFsPath != tt.wantFsPath {
t.Errorf("URLMap.GetFilesystemLocation() gotFsPath = %v, want %v", gotFsPath, tt.wantFsPath)
}
})
}
}