|
4 | 4 | "encoding/json" |
5 | 5 | "fmt" |
6 | 6 | "net/http" |
| 7 | + "os" |
| 8 | + "regexp" |
7 | 9 | "sync" |
8 | 10 |
|
9 | 11 | "github.com/SteamServerUI/SteamServerUI/v7/src/api/pluginproxy" |
@@ -40,11 +42,22 @@ func RegisterPluginRouteHandler(w http.ResponseWriter, r *http.Request, apiMux * |
40 | 42 | http.Error(w, `{"status":"error","message":"Missing required field pluginname"}`, http.StatusBadRequest) |
41 | 43 | return |
42 | 44 | } |
| 45 | + // sanatize plugin name (allow alphanumeric, underscores, and hyphens only) |
| 46 | + if !isValidPluginName(req.PluginName) { |
| 47 | + http.Error(w, `{"status":"error","message":"Invalid plugin name. Use only alphanumeric characters, underscores, or hyphens"}`, http.StatusBadRequest) |
| 48 | + return |
| 49 | + } |
43 | 50 |
|
44 | | - // Dynamically register the plugin route in protectedMux |
45 | 51 | route := fmt.Sprintf("/plugins/%s/", req.PluginName) |
46 | 52 | socketPath := fmt.Sprintf("/tmp/ssui/%s.sock", req.PluginName) |
47 | 53 |
|
| 54 | + // check if the plugin socket exists |
| 55 | + if !pluginSocketExists(socketPath) { |
| 56 | + w.WriteHeader(http.StatusNotImplemented) |
| 57 | + json.NewEncoder(w).Encode(map[string]string{"status": "failed", "message": "Plugin socket does not exist. Make sure to call PluginLib.ExposeAPI before calling PluginLib.RegisterPluginAPI"}) |
| 58 | + return |
| 59 | + } |
| 60 | + |
48 | 61 | err := checkRoute(route) |
49 | 62 | if err { |
50 | 63 | w.WriteHeader(http.StatusConflict) |
@@ -72,3 +85,15 @@ func checkRoute(route string) (registered bool) { |
72 | 85 | pluginRoutes[route] = true |
73 | 86 | return false |
74 | 87 | } |
| 88 | + |
| 89 | +func isValidPluginName(name string) bool { |
| 90 | + // Allow alphanumeric, underscores, and hyphens (minimum 1 character, maximum 50 characters) |
| 91 | + pattern := `^[a-zA-Z0-9_-]{1,50}$` |
| 92 | + matched, err := regexp.MatchString(pattern, name) |
| 93 | + return err == nil && matched |
| 94 | +} |
| 95 | + |
| 96 | +func pluginSocketExists(socketPath string) bool { |
| 97 | + _, err := os.Stat(socketPath) |
| 98 | + return err == nil |
| 99 | +} |
0 commit comments