Forms: the script asks for the right form's token #27

Merged
jcoffey-dev merged 1 commits from fix/form-token-name into main 2026-10-14 20:09:57 +00:00
2 changed files with 21 additions and 1 deletions

No files matched your search

+20
View File
@@ -3,6 +3,7 @@ package forms
import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"
)
@@ -100,6 +101,25 @@ func TestRenderEscapes(t *testing.T) {
}
}
// The script finds each form by the data attribute the markup sets, and asks
// for that form's token by the name in it. A rename once left them apart, and
// every form asked for the token of a form called "undefined".
func TestScriptReadsTheFormsAttribute(t *testing.T) {
f := load(t, contact)
h, err := f.Render("/f.js", "/f.css")
if err != nil {
t.Fatal(err)
}
m := regexp.MustCompile(`data-([a-z]+)-form="contact"`).FindStringSubmatch(string(h))
if m == nil {
t.Fatalf("no data-…-form attribute in:\n%s", h)
}
js := string(Script)
if !strings.Contains(js, "form[data-"+m[1]+"-form]") || !strings.Contains(js, "form.dataset."+m[1]+"Form") {
t.Errorf("the script doesn't read data-%s-form, the attribute the form has", m[1])
}
}
func TestSubjectIsPlain(t *testing.T) {
for _, bad := range []string{`{{ range 100000000 }}x{{ end }}`, `{{ printf "%099999999d" 1 }}`, `{{ if .a }}x{{ end }}`, `{{ $x := .a }}`, `{{ .a.b }}`, `{{ template "x" }}`} {
if _, err := Parse("contact", []byte("to: [email protected]\nsubject: '"+bad+"'\nfields:\n - name: a\n")); err == nil {
+1 -1
View File
@@ -10,7 +10,7 @@
form.querySelectorAll('[aria-invalid]').forEach(function (el) { el.removeAttribute('aria-invalid'); });
}
function token(form) {
return fetch('/_hotdog/forms/' + form.dataset.clForm + '/token', { credentials: 'omit' })
return fetch('/_hotdog/forms/' + form.dataset.hotdogForm + '/token', { credentials: 'omit' })
.then(function (r) { if (!r.ok) throw new Error(r.status); return r.json(); })
.then(function (d) { form.querySelector('input[name="_t"]').value = d.token; });
}