Skip to content

Commit

Permalink
security(contribs/gnodev/pkg/emitter): use html/template not text/tem…
Browse files Browse the repository at this point in the history
…plate for HTML generation

This change uses html/template instead of text/template for HTML
generation and also locks in tests to detect such subtle regressions
and thus help prevent future cross-side scripting (XSS) attacks
if later the scripts evolve and take in user input.

Fixes #3544
  • Loading branch information
odeke-em committed Jan 19, 2025
1 parent d2813f8 commit c014723
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions contribs/gnodev/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/charmbracelet/wish v1.4.0
github.com/fsnotify/fsnotify v1.7.0
github.com/gnolang/gno v0.0.0-00010101000000-000000000000
github.com/google/go-cmp v0.6.0
github.com/gorilla/websocket v1.5.3
github.com/lrstanley/bubblezone v0.0.0-20240624011428-67235275f80c
github.com/muesli/reflow v0.3.0
Expand Down
2 changes: 1 addition & 1 deletion contribs/gnodev/pkg/emitter/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
_ "embed"
"encoding/json"
"fmt"
"html/template"
"net/http"
"strings"
"sync"
"text/template"

"github.com/gnolang/gno/contribs/gnodev/pkg/events"
)
Expand Down
47 changes: 47 additions & 0 deletions contribs/gnodev/pkg/emitter/middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package emitter

import (
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestMiddlewareUsesHTMLTemplate(t *testing.T) {
tests := []struct {
name string
remote string
want string
}{
{"normal remote", "localhost:9999", "const ws = new WebSocket('ws://localhost:9999');"},
{"xss'd remote", `localhost:9999');alert('pwned`, "const ws = new WebSocket('ws://localhost:9999');alert('pwned');"},
}

// As the code revolves, add more search patterns here.
var reWebsocket = regexp.MustCompile("const ws = new WebSocket[^\n]+")

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rec := httptest.NewRecorder()
mdw := NewMiddleware(tt.remote, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "text/html")
fmt.Fprintf(rw, "<body></body>")
}))
rec.Header().Set("Content-Type", "text/html")
req := httptest.NewRequest("GET", "https://gno.land/example", nil)
mdw.ServeHTTP(rec, req)

targets := reWebsocket.FindAllString(rec.Body.String(), -1)
if len(targets) == 0 {
t.Fatal("Could not find the desired targets")
}
body := targets[0]
if diff := cmp.Diff(string(body), tt.want); diff != "" {

Check failure on line 42 in contribs/gnodev/pkg/emitter/middleware_test.go

View workflow job for this annotation

GitHub Actions / Run Main (gnodev) / Go Lint / lint

unnecessary conversion (unconvert)
t.Fatalf("Response mismatch: got - want +\n%s", diff)
}
})
}
}

0 comments on commit c014723

Please sign in to comment.