-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gnodev/pkg/emitter): use html/template not text/template for HTML…
… generation (#3545) 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
Showing
3 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package emitter | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
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. | ||
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) | ||
require.True(t, len(targets) > 0) | ||
body := targets[0] | ||
require.Equal(t, body, tt.want) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters