Skip to content

I think we're getting somewhere with generics. #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions examples/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ func main() {
addr := fmt.Sprintf("localhost:%d", port)
m := web.NewMuxConfigDev(port).Mux()

m.Handle("/echo", safehttp.MethodGet, safehttp.HandlerFunc(echo))
m.Handle("/uptime", safehttp.MethodGet, safehttp.HandlerFunc(uptime))
m.Handle("/echo", safehttp.MethodGet, web.HandlerFunc(echo))
m.Handle("/uptime", safehttp.MethodGet, web.HandlerFunc(uptime))

start = time.Now()
log.Printf("Visit http://%s\n", addr)
log.Printf("Listening on %s...\n", addr)
log.Fatal(http.ListenAndServe(addr, m))
}

func echo(w safehttp.ResponseWriter, req *safehttp.IncomingRequest) safehttp.Result {
func echo(w web.ResponseWriter, req *safehttp.IncomingRequest) safehttp.Result {
q, err := req.URL().Query()
if err != nil {
return w.WriteError(safehttp.StatusBadRequest)
Expand All @@ -64,7 +64,7 @@ var uptimeTmpl *template.Template = template.Must(template.New("uptime").Parse(
`<h1>Uptime: {{ .Uptime }}</h1>
{{- if .EasterEgg }}<h1>You've found an easter egg using "{{ .EasterEgg }}". Congrats!</h1>{{ end -}}`))

func uptime(w safehttp.ResponseWriter, req *safehttp.IncomingRequest) safehttp.Result {
func uptime(w web.ResponseWriter, req *safehttp.IncomingRequest) safehttp.Result {
var x struct {
Uptime time.Duration
EasterEgg string
Expand Down
20 changes: 20 additions & 0 deletions examples/echo/security/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/google/go-safeweb/safehttp/plugins/hostcheck"
"github.com/google/go-safeweb/safehttp/plugins/hsts"
"github.com/google/go-safeweb/safehttp/plugins/staticheaders"
"github.com/google/safehtml"

"github.com/google/go-safeweb/safehttp"
)
Expand Down Expand Up @@ -88,3 +89,22 @@ func NewMuxConfigDev(port int) *safehttp.ServeMuxConfig {

return c
}

type ResponseWriter struct {
safehttp.ResponseWriter
}

func (rw ResponseWriter) WriteHTML(html safehtml.HTML) safehttp.Result {
return rw.Write(html)
}



func HandlerFunc(f func(ResponseWriter, *safehttp.IncomingRequest) safehttp.Result) safehttp.HandlerFunc {
ha := safehttp.HandlerAdapter[ResponseWriter]{
CustomResponseWriterFactory: func(w safehttp.ResponseWriter) ResponseWriter {
return ResponseWriter{w}
},
}
return ha.AdaptFunc(f)
}
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
module github.com/google/go-safeweb

go 1.16
go 1.18

require (
github.com/google/go-cmp v0.5.0
github.com/google/safehtml v0.0.2
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/net v0.0.0-20210510120150-4163338589ed
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
golang.org/x/tools v0.1.1
)

require (
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/safehtml v0.0.2 h1:ZOt2VXg4x24bW0m2jtzAOkhoXV0iM8vNKc0paByCZqM=
github.com/google/safehtml v0.0.2/go.mod h1:L4KWwDsUJdECRAEpZoBn3O64bQaywRscowZjJAzjHnU=
github.com/yuin/goldmark v1.3.5 h1:dPmz1Snjq0kmkz159iL7S6WzdahUTHnHB5M56WFVifs=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w=
Expand All @@ -13,21 +11,14 @@ golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210510120150-4163338589ed h1:p9UgmWI9wKpfYmgaV/IZKGdXc5qEK45tDwwwDyjS26I=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
28 changes: 28 additions & 0 deletions safehttp/typed_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package safehttp

type TypedHandler[CustomResponseWriter ResponseWriter] interface {
ServeHTTP(CustomResponseWriter, *IncomingRequest) Result
}

type HandlerAdapter[CustomResponseWriter ResponseWriter] struct {
CustomResponseWriterFactory func(ResponseWriter) CustomResponseWriter
}

func (ha *HandlerAdapter[CustomResponseWriter]) Adapt(h TypedHandler[CustomResponseWriter]) Handler {
return &adaptedHandler[CustomResponseWriter]{rwFactory: ha.CustomResponseWriterFactory, h: h}
}

func (ha *HandlerAdapter[CustomResponseWriter]) AdaptFunc(f func(CustomResponseWriter, *IncomingRequest) Result) HandlerFunc {
return HandlerFunc(func(w ResponseWriter, req *IncomingRequest) Result {
return f(ha.CustomResponseWriterFactory(w), req)
})
}

type adaptedHandler[CustomResponseWriter ResponseWriter] struct {
rwFactory func(ResponseWriter) CustomResponseWriter
h TypedHandler[CustomResponseWriter]
}

func (ah *adaptedHandler[CustomResponseWriter]) ServeHTTP(rw ResponseWriter, req *IncomingRequest) Result {
return ah.h.ServeHTTP(ah.rwFactory(rw), req)
}