Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Fixed potential malicious script execution (#1536)

Matt Robertsd5a80ea5cdac
diff --git a/app/pkg/web/react.go b/app/pkg/web/react.go
index d4ac11176..145ad6447 100644
--- a/app/pkg/web/react.go
+++ b/app/pkg/web/react.go
@@ -70,7 +70,12 @@ func (r *ReactRenderer) Render(u *url.URL, props Map) (string, error) {
return "", errors.Wrap(err, "failed to marshal props")
}
- renderCmd := fmt.Sprintf(`ssrRender("%s", %s)`, u.String(), string(jsonArg))
+ urlArg, err := json.Marshal(u.String())
+ if err != nil {
+ return "", errors.Wrap(err, "failed to marshal url")
+ }
+
+ renderCmd := fmt.Sprintf(`ssrRender(%s, %s)`, string(urlArg), string(jsonArg))
val, err := v8ctx.RunScript(renderCmd, r.scriptPath)
if err != nil {
if jsErr, ok := err.(*v8go.JSError); ok {
diff --git a/app/pkg/web/react_test.go b/app/pkg/web/react_test.go
index 4d60c7204..2d5589e2d 100644
--- a/app/pkg/web/react_test.go
+++ b/app/pkg/web/react_test.go
@@ -2,6 +2,7 @@ package web_test
import (
"net/url"
+ "strings"
"testing"
"github.com/getfider/fider/app/models/entity"
@@ -60,6 +61,41 @@ func TestReactRenderer_RenderEmptyHomeHTML(t *testing.T) {
Expect(err).IsNil()
}
+func TestReactRenderer_RenderWithMaliciousURL(t *testing.T) {
+ RegisterT(t)
+
+ r, err := web.NewReactRenderer("ssr.js")
+ Expect(err).IsNil()
+
+ // Attempt to break out of the JS string literal and inject code via the URL.
+ // The URL contains a literal double-quote followed by JS that would, if interpolated
+ // unsafely, execute server-side and emit attacker-controlled content into the SSR
+ // output. After the fix the URL must be JSON-encoded into the ssrRender call so the
+ // injection becomes inert.
+ u := &url.URL{
+ Scheme: "https",
+ Host: "demo.test.fider.io",
+ Path: "/",
+ RawQuery: `q=",{"page":"Error/Error404.page","settings":{},"tenant":{"locale":"en"}});` + "`<h1>RCE-PROOF</h1>`" + `//`,
+ }
+ html, err := r.Render(u, web.Map{
+ "page": "Home/Home.page",
+ "tenant": &entity.Tenant{
+ Locale: "en",
+ },
+ "settings": web.Map{
+ "locale": "en",
+ },
+ "props": web.Map{
+ "posts": make([]web.Map, 0),
+ "tags": make([]web.Map, 0),
+ "countPerStatus": web.Map{},
+ },
+ })
+ Expect(err).IsNil()
+ Expect(strings.Contains(html, `<h1>RCE-PROOF</h1>`)).IsFalse()
+}
+
func TestReactRenderer_RenderEmptyHomeHTML_Portuguese(t *testing.T) {
RegisterT(t)