-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin_asset_handler.go
More file actions
143 lines (124 loc) · 4.21 KB
/
Copy pathplugin_asset_handler.go
File metadata and controls
143 lines (124 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
"net/http"
"path"
"regexp"
"strings"
"github.com/omniviewdev/omniview/internal/appstate"
logging "github.com/omniviewdev/plugin-sdk/log"
"github.com/wailsapp/mimetype"
)
// PluginAssetHandler serves plugin assets from the local filesystem.
// It is used as middleware in the Wails v3 AssetOptions to handle
// requests for plugin-specific static files (JS, CSS, images, fonts).
type PluginAssetHandler struct {
logger logging.Logger
stateRoot *appstate.ScopedRoot
}
// NewPluginAssetHandler creates a new PluginAssetHandler.
func NewPluginAssetHandler(logger logging.Logger, stateRoot *appstate.ScopedRoot) *PluginAssetHandler {
return &PluginAssetHandler{
logger: logger,
stateRoot: stateRoot,
}
}
// allowedPathRegex is compiled once at init — regexp.MustCompile is expensive
// and should not be called on every HTTP request.
var allowedPathRegex = regexp.MustCompile(
`^/plugins/[^/]+/(assets|dist)/.*\.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf|html|js\.map)$`,
)
func isAllowed(path string) bool {
return allowedPathRegex.MatchString(path)
}
func forceMimeType(path string) string {
switch {
case strings.HasSuffix(path, ".js.map"):
return "application/javascript"
case strings.HasSuffix(path, ".js"):
return "application/javascript"
case strings.HasSuffix(path, ".css"):
return "text/css"
case strings.HasSuffix(path, ".png"):
return "image/png"
case strings.HasSuffix(path, ".jpg"), strings.HasSuffix(path, ".jpeg"):
return "image/jpeg"
case strings.HasSuffix(path, ".gif"):
return "image/gif"
case strings.HasSuffix(path, ".svg"):
return "image/svg+xml"
case strings.HasSuffix(path, ".ico"):
return "image/x-icon"
case strings.HasSuffix(path, ".woff"):
return "font/woff"
case strings.HasSuffix(path, ".woff2"):
return "font/woff2"
case strings.HasSuffix(path, ".ttf"):
return "font/ttf"
case strings.HasSuffix(path, ".html"):
return "text/html"
default:
return "text/plain"
}
}
// ServeHTTP handles HTTP requests for plugin assets.
func (h *PluginAssetHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
var err error
ctx := req.Context()
respondUnauthorized := func() {
res.WriteHeader(http.StatusUnauthorized)
if _, err = res.Write([]byte("Unauthorized")); err != nil {
h.logger.Errorw(ctx, "error writing unauthorized response", "error", err)
}
}
requestedFilename := req.URL.Path
// must start with /_/
if !strings.HasPrefix(requestedFilename, "/_/") {
respondUnauthorized()
return
}
requestedFilename = strings.TrimPrefix(requestedFilename, "/_/")
// Normalize the path to prevent traversal via sequences like "foo/../bar".
requestedFilename = path.Clean(requestedFilename)
h.logger.Debugw(ctx, "requested file", "path", requestedFilename)
if !isAllowed("/"+requestedFilename) {
respondUnauthorized()
return
}
// ScopedRoot enforces containment — no manual checks needed.
fileData, err := h.stateRoot.ReadFile(requestedFilename)
if err != nil {
res.WriteHeader(http.StatusBadRequest)
if _, err = fmt.Fprintf(res, "Could not load file %s", requestedFilename); err != nil {
h.logger.Errorw(ctx, "error serving file", "error", err)
}
return
}
// set content type
contentType := mimetype.Detect(fileData).String()
h.logger.Infow(ctx, "content type", "type", contentType)
if strings.HasPrefix(contentType, "text/plain") {
// don't like this but it's the only way to force the right mime type.
contentType = forceMimeType(requestedFilename)
}
res.Header().Set("Content-Type", contentType)
// if remoteEntry.js, do NOT cache
if strings.HasSuffix(requestedFilename, "entry.js") {
res.Header().Set("Cache-Control", "no-store")
}
if _, err = res.Write(fileData); err != nil {
h.logger.Errorw(ctx, "error serving file", "error", err)
}
}
// Middleware returns an application.Middleware that intercepts plugin asset
// requests (those prefixed with /_/) and delegates them to the
// PluginAssetHandler. All other requests pass through to the next handler.
func (h *PluginAssetHandler) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/_/") {
h.ServeHTTP(w, r)
return
}
next.ServeHTTP(w, r)
})
}