Generated plugins (acceptance.json at the root) fail on: HTTP routes not registered through pluginsdk route or a public route reading personal columns; HTML built from unescaped non-constant strings; public POSTs outside route.Form; personal data names in log calls; outbound requests carrying personal data or values the spec does not send to that host. First-party plugins get the same findings as warnings. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.dev.alexdunmow.com/block/pluginsdk/plugin"
|
|
"git.dev.alexdunmow.com/block/pluginsdk/route"
|
|
"git.dev.alexdunmow.com/private/quote-requests-a1a5c0/db"
|
|
)
|
|
|
|
func newHTTPHandler(deps plugin.CoreServices) http.Handler {
|
|
setServices(deps)
|
|
r := route.New(pluginName)
|
|
r.Form("/submit", handleSubmit)
|
|
r.Handle(http.MethodGet, "/requests", route.Admin, handleList)
|
|
return r
|
|
}
|
|
|
|
func handleSubmit(w http.ResponseWriter, r *http.Request) {
|
|
dbtx, ok := current.Pool.(db.DBTX)
|
|
if !ok {
|
|
http.Error(w, "service unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, "invalid request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
params := db.InsertQuoteRequestParams{
|
|
Name: strings.TrimSpace(r.PostForm.Get("name")),
|
|
Email: strings.TrimSpace(r.PostForm.Get("email")),
|
|
Phone: strings.TrimSpace(r.PostForm.Get("phone")),
|
|
PropertyAddress: strings.TrimSpace(r.PostForm.Get("property_address")),
|
|
RoofIssue: strings.TrimSpace(r.PostForm.Get("roof_issue")),
|
|
}
|
|
if err := db.New(dbtx).InsertQuoteRequest(r.Context(), params); err != nil {
|
|
http.Error(w, "could not save your request", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
slog.Info("quote request stored", "issue_length", len(params.RoofIssue))
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
_, _ = fmt.Fprintf(w, "<!doctype html><p>Thank you. We received %d characters.</p>", len(params.RoofIssue))
|
|
}
|
|
|
|
func handleList(w http.ResponseWriter, r *http.Request) {
|
|
dbtx, ok := current.Pool.(db.DBTX)
|
|
if !ok {
|
|
http.Error(w, "service unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
rows, err := db.New(dbtx).ListQuoteRequests(r.Context())
|
|
if err != nil {
|
|
http.Error(w, "could not load requests", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(rows)
|
|
}
|