Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.

Commit bbc85e0

Browse files
authored
Pull CSRF token from metadata (#897)
1 parent 95b2055 commit bbc85e0

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

cmd/server/assets/header.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<meta name="msapplication-TileColor" content="#ff0554">
1313
<meta name="msapplication-config" content="/static/browserconfig.xml">
1414
<meta name="theme-color" content="#ffffff">
15+
{{.csrfMeta}}
1516

1617
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
1718
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">

cmd/server/assets/static/js/application.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ $(function () {
3434
}
3535
}
3636

37-
let $csrfField = $("{{.csrfField}}");
37+
let csrfToken = $("meta[name=csrf-token]").attr("content");
38+
let $csrfField = $("<input>")
39+
.attr("type", "hidden")
40+
.attr("name", "gorilla.csrf.Token")
41+
.attr("value", csrfToken);
3842

3943
let $inputField = $("<input>")
4044
.attr("type", "hidden")

pkg/controller/middleware/csrf.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package middleware
1616

1717
import (
1818
"context"
19+
"html/template"
1920
"net/http"
2021

2122
"github.com/google/exposure-notifications-verification-server/pkg/config"
@@ -44,12 +45,10 @@ func ConfigureCSRF(ctx context.Context, config *config.ServerConfig, h *render.R
4445

4546
// Save csrf configuration on the template map.
4647
m := controller.TemplateMapFromContext(ctx)
47-
if _, ok := m["csrfField"]; !ok {
48-
m["csrfField"] = csrf.TemplateField(r)
49-
}
50-
if _, ok := m["csrfToken"]; !ok {
51-
m["csrfToken"] = csrf.Token(r)
52-
}
48+
m["csrfField"] = csrf.TemplateField(r)
49+
m["csrfToken"] = csrf.Token(r)
50+
m["csrfMeta"] = template.HTML(
51+
`<meta name="csrf-token" content="` + csrf.Token(r) + `">`)
5352

5453
// Save the template map on the context.
5554
ctx = controller.WithTemplateMap(ctx, m)

0 commit comments

Comments
 (0)