Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
maxStatementRequestBytes int64 = reconciliation.MaxFileBytes + (1 << 20)
maxRazorpayTestRequestBytes int64 = 1 << 20
maxRazorpayLiveRequestBytes int64 = 1 << 20
robotsTagValue = "noindex, nofollow, noarchive, nosnippet, noimageindex"
)

type API struct {
Expand All @@ -60,6 +61,14 @@ func New(cfg config.Config, paymentService *payments.Service, smsService *sms.Se

func (a *API) Register(app core.App) {
app.OnServe().BindFunc(func(e *core.ServeEvent) error {
e.Router.BindFunc(func(event *core.RequestEvent) error {
event.Response.Header().Set("X-Robots-Tag", robotsTagValue)
return event.Next()
})
e.Router.GET("/robots.txt", func(event *core.RequestEvent) error {
event.Response.Header().Set("Cache-Control", "no-store")
return event.String(http.StatusOK, "User-agent: *\nDisallow:\n")
})
e.Router.POST("/api/payments", a.createPayment).Bind(apis.BodyLimit(maxPaymentRequestBytes))
e.Router.GET("/api/payments/{id}", a.getPayment)
e.Router.POST("/api/payments/{id}/cancel", a.cancelPayment)
Expand Down
12 changes: 10 additions & 2 deletions internal/api/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,23 @@ func TestOperatorSPAUsesSecurityHeadersAndRejectsUnknownBrowserRoutes(t *testing
if res.StatusCode != http.StatusOK || !bytes.Contains(data, []byte("PayGate")) {
t.Fatalf("root status=%d body=%s", res.StatusCode, data)
}
for _, name := range []string{"Content-Security-Policy", "Permissions-Policy", "Referrer-Policy", "Strict-Transport-Security"} {
for _, name := range []string{"Content-Security-Policy", "Permissions-Policy", "Referrer-Policy", "Strict-Transport-Security", "X-Robots-Tag"} {
if res.Header.Get(name) == "" {
t.Fatalf("missing %s", name)
}
}
for _, path := range []string{"/contact/", "/robots.txt", "/sitemap.xml"} {
res, robots := fixture.request(t, http.MethodGet, "/robots.txt", nil, "", false)
if res.StatusCode != http.StatusOK || !bytes.Contains(robots, []byte("User-agent: *")) || res.Header.Get("X-Robots-Tag") == "" {
t.Fatalf("robots status=%d header=%q body=%s", res.StatusCode, res.Header.Get("X-Robots-Tag"), robots)
}
for _, path := range []string{"/contact/", "/sitemap.xml"} {
res, _ := fixture.request(t, http.MethodGet, path, nil, "", false)
if res.StatusCode != http.StatusNotFound {
t.Fatalf("%s status=%d", path, res.StatusCode)
}
}
res, _ = fixture.request(t, http.MethodGet, "/api/config", nil, "", true)
if res.Header.Get("X-Robots-Tag") == "" {
t.Fatal("missing X-Robots-Tag on API response")
}
}
1 change: 1 addition & 0 deletions internal/api/razorpay.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@ func (a *API) setOperatorSecurityHeaders(e *core.RequestEvent) {
headers.Set("Strict-Transport-Security", "max-age=31536000")
headers.Set("X-Content-Type-Options", "nosniff")
headers.Set("X-Frame-Options", "DENY")
headers.Set("X-Robots-Tag", robotsTagValue)
}
1 change: 1 addition & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="noindex, nofollow, noarchive, nosnippet, noimageindex" />
<title>PayGate</title>
</head>
<body>
Expand Down