Skip to content

Commit 7b4bbd8

Browse files
added some sanity checking to plugin proxy
1 parent 7185932 commit 7b4bbd8

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

src/api/pluginsapi/registerroute.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"os"
8+
"regexp"
79
"sync"
810

911
"github.com/SteamServerUI/SteamServerUI/v7/src/api/pluginproxy"
@@ -40,11 +42,22 @@ func RegisterPluginRouteHandler(w http.ResponseWriter, r *http.Request, apiMux *
4042
http.Error(w, `{"status":"error","message":"Missing required field pluginname"}`, http.StatusBadRequest)
4143
return
4244
}
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+
}
4350

44-
// Dynamically register the plugin route in protectedMux
4551
route := fmt.Sprintf("/plugins/%s/", req.PluginName)
4652
socketPath := fmt.Sprintf("/tmp/ssui/%s.sock", req.PluginName)
4753

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+
4861
err := checkRoute(route)
4962
if err {
5063
w.WriteHeader(http.StatusConflict)
@@ -72,3 +85,15 @@ func checkRoute(route string) (registered bool) {
7285
pluginRoutes[route] = true
7386
return false
7487
}
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

Comments
 (0)