oficina / estilo Public

busy: the shim guard hears submit in any quote, and onsubmit

Fable's P3.8 on estilo #34: the guard counted only a double-quoted
addEventListener("submit", so a re-vendored shim hearing submit another
way would pass it. submitHooks counts addEventListener with "submit" in
double quotes, single quotes or backticks (spaces allowed after the
parenthesis) and any onsubmit. TestTheSubmitGuardSeesEverySpelling adds
each spelling to the real shim and expects two; the old count fails it
for five of the six.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev b9ba6dcc9ed23a8b3c3918412266aca9afdb1fd6 parent 2f9e0de
1 file changed, +29 −1
  • busy/busy_test.go +29 −1
diff --git a/busy/busy_test.go b/busy/busy_test.go
index 44cbb73..ab53fb6 100644
--- a/busy/busy_test.go
+++ b/busy/busy_test.go
@@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"os"
+ "regexp"
"strings"
"testing"
)
@@ -49,7 +50,7 @@ func TestTheShimLeftTheRuleToBusyJS(t *testing.T) {
// ([rst-scheme]), which it applies in place; any other form it leaves
// to the browser and busy.js. (The shim at rastrillo 0547879, which
// Docs pins, added this; the switch drag came with it.)
- if n := strings.Count(shim, `addEventListener("submit"`); n != 1 {
+ if n := submitHooks(shim); n != 1 {
t.Fatalf("the shim listens for submit %d times; only the scheme switch should", n)
}
scheme := shim[strings.Index(shim, `addEventListener("submit"`):]
@@ -71,6 +72,33 @@ func TestTheShimLeftTheRuleToBusyJS(t *testing.T) {
}
}
+// submitHooks counts the ways a script can hear a submit:
+// addEventListener with the type in any of JavaScript's three quotes, and
+// an onsubmit handler, assigned or as an attribute.
+func submitHooks(js string) int {
+ return len(regexp.MustCompile("addEventListener\\(\\s*[\"'`]submit[\"'`]|\\bonsubmit\\b").FindAllString(js, -1))
+}
+
+// The guard above must see a second submit listener however it is
+// spelled, or a re-vendored shim with a busy rule written another way
+// would pass it (Fable, estilo #34: it caught only "submit" in double
+// quotes).
+func TestTheSubmitGuardSeesEverySpelling(t *testing.T) {
+ shim := string(ShimJS())
+ for _, extra := range []string{
+ `document.addEventListener("submit", busy, true)`,
+ `document.addEventListener('submit', busy, true)`,
+ "document.addEventListener(`submit`, busy, true)",
+ `document.addEventListener( "submit", busy)`,
+ `form.onsubmit = busy`,
+ `document.body.setAttribute("onsubmit", "busy()")`,
+ } {
+ if n := submitHooks(shim + "\n" + extra + "\n"); n != 2 {
+ t.Errorf("the guard counts %d submit listeners with %s added; want 2", n, extra)
+ }
+ }
+}
+
func TestHandlerServesOnlyItsTwoFiles(t *testing.T) {
h := Handler()
for path, want := range map[string]int{