-
-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
4dd4c3c
commit 222475d
Showing
20 changed files
with
621 additions
and
105 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -24,6 +24,11 @@ query Plugins { | |
description | ||
type | ||
} | ||
|
||
paths { | ||
css | ||
javascript | ||
} | ||
} | ||
} | ||
|
||
|
This file contains 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 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 |
---|---|---|
|
@@ -13,4 +13,5 @@ const ( | |
tagKey | ||
downloadKey | ||
imageKey | ||
pluginKey | ||
) |
This file contains 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 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,57 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/stashapp/stash/pkg/plugin" | ||
) | ||
|
||
type pluginURLBuilder struct { | ||
BaseURL string | ||
Plugin *plugin.Plugin | ||
} | ||
|
||
func (b pluginURLBuilder) javascript() []string { | ||
ui := b.Plugin.UI | ||
if len(ui.Javascript) == 0 && len(ui.ExternalScript) == 0 { | ||
return nil | ||
} | ||
|
||
var ret []string | ||
|
||
ret = append(ret, ui.ExternalScript...) | ||
ret = append(ret, b.BaseURL+"/plugin/"+b.Plugin.ID+"/javascript") | ||
|
||
return ret | ||
} | ||
|
||
func (b pluginURLBuilder) css() []string { | ||
ui := b.Plugin.UI | ||
if len(ui.CSS) == 0 && len(ui.ExternalCSS) == 0 { | ||
return nil | ||
} | ||
|
||
var ret []string | ||
|
||
ret = append(ret, b.Plugin.UI.ExternalCSS...) | ||
ret = append(ret, b.BaseURL+"/plugin/"+b.Plugin.ID+"/css") | ||
return ret | ||
} | ||
|
||
func (b *pluginURLBuilder) paths() *PluginPaths { | ||
return &PluginPaths{ | ||
Javascript: b.javascript(), | ||
CSS: b.css(), | ||
} | ||
} | ||
|
||
func (r *pluginResolver) Paths(ctx context.Context, obj *plugin.Plugin) (*PluginPaths, error) { | ||
baseURL, _ := ctx.Value(BaseURLCtxKey).(string) | ||
|
||
b := pluginURLBuilder{ | ||
BaseURL: baseURL, | ||
Plugin: obj, | ||
} | ||
|
||
return b.paths(), nil | ||
} |
This file contains 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 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,107 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/go-chi/chi/v5" | ||
|
||
"github.com/stashapp/stash/pkg/plugin" | ||
) | ||
|
||
type pluginRoutes struct { | ||
pluginCache *plugin.Cache | ||
} | ||
|
||
func getPluginRoutes(pluginCache *plugin.Cache) chi.Router { | ||
return pluginRoutes{ | ||
pluginCache: pluginCache, | ||
}.Routes() | ||
} | ||
|
||
func (rs pluginRoutes) Routes() chi.Router { | ||
r := chi.NewRouter() | ||
|
||
r.Route("/{pluginId}", func(r chi.Router) { | ||
r.Use(rs.PluginCtx) | ||
r.Get("/assets/*", rs.Assets) | ||
r.Get("/javascript", rs.Javascript) | ||
r.Get("/css", rs.CSS) | ||
}) | ||
|
||
return r | ||
} | ||
|
||
func (rs pluginRoutes) Assets(w http.ResponseWriter, r *http.Request) { | ||
p := r.Context().Value(pluginKey).(*plugin.Plugin) | ||
|
||
if !p.Enabled { | ||
http.Error(w, "plugin disabled", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
prefix := "/plugin/" + chi.URLParam(r, "pluginId") + "/assets" | ||
|
||
r.URL.Path = strings.Replace(r.URL.Path, prefix, "", 1) | ||
|
||
// http.FileServer redirects to / if the path ends with index.html | ||
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/index.html") | ||
|
||
pluginDir := filepath.Dir(p.ConfigPath) | ||
|
||
// map the path to the applicable filesystem location | ||
var dir string | ||
r.URL.Path, dir = p.UI.Assets.GetFilesystemLocation(r.URL.Path) | ||
if dir == "" { | ||
http.NotFound(w, r) | ||
} | ||
|
||
dir = filepath.Join(pluginDir, filepath.FromSlash(dir)) | ||
|
||
// ensure directory is still within the plugin directory | ||
if !strings.HasPrefix(dir, pluginDir) { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
http.FileServer(http.Dir(dir)).ServeHTTP(w, r) | ||
} | ||
|
||
func (rs pluginRoutes) Javascript(w http.ResponseWriter, r *http.Request) { | ||
p := r.Context().Value(pluginKey).(*plugin.Plugin) | ||
|
||
if !p.Enabled { | ||
http.Error(w, "plugin disabled", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "text/javascript") | ||
serveFiles(w, r, p.UI.Javascript) | ||
} | ||
|
||
func (rs pluginRoutes) CSS(w http.ResponseWriter, r *http.Request) { | ||
p := r.Context().Value(pluginKey).(*plugin.Plugin) | ||
|
||
if !p.Enabled { | ||
http.Error(w, "plugin disabled", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "text/css") | ||
serveFiles(w, r, p.UI.CSS) | ||
} | ||
|
||
func (rs pluginRoutes) PluginCtx(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
p := rs.pluginCache.GetPlugin(chi.URLParam(r, "pluginId")) | ||
if p == nil { | ||
http.Error(w, http.StatusText(404), 404) | ||
return | ||
} | ||
|
||
ctx := context.WithValue(r.Context(), pluginKey, p) | ||
next.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} |
This file contains 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.