diff options
author | Evgeny Kuznetsov <evgeny@kuznetsov.md> | 2023-03-23 17:13:37 +0300 |
---|---|---|
committer | Evgeny Kuznetsov <evgeny@kuznetsov.md> | 2023-03-23 17:13:37 +0300 |
commit | 078ebd04c6a3088b8c2c35070490eb34cf4c6708 (patch) | |
tree | f9a879a039124cca57d45566f44b036d472bbaca | |
parent | 4fdc39884f6d083e4266459b550a538c3229cae4 (diff) | |
download | static-wm-078ebd04c6a3088b8c2c35070490eb34cf4c6708.tar.gz static-wm-078ebd04c6a3088b8c2c35070490eb34cf4c6708.zip |
feat: add scheme checks
Per https://www.w3.org/TR/webmention/#request-verification
-rw-r--r-- | main.go | 15 | ||||
-rw-r--r-- | main_test.go | 2 |
2 files changed, 16 insertions, 1 deletions
@@ -10,6 +10,7 @@ import ( const ( errSrcInvalid = "source is not a parsable URL" errTgtNotAccepted = "can not process webmentions for this target" + errInvalidScheme = "URL scheme is not HTTP(S)" ) // endpoint is a webmention receiver. @@ -19,15 +20,27 @@ type endpoint struct { // ServeHTTP is http.Handler implementation. func (ep endpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) { - _, err := url.Parse(r.PostFormValue("source")) + source, err := url.Parse(r.PostFormValue("source")) if err != nil { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(errSrcInvalid)) + return + } + if source.Scheme != "http" && source.Scheme != "https" { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(errInvalidScheme)) + return } target, err := url.Parse(r.PostFormValue("target")) if err != nil || !ep.targetAllowed(target) { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(errTgtNotAccepted)) + return + } + if target.Scheme != "http" && target.Scheme != "https" { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(errInvalidScheme)) + return } } diff --git a/main_test.go b/main_test.go index 0510af8..37fe250 100644 --- a/main_test.go +++ b/main_test.go @@ -20,6 +20,8 @@ func TestSyncRejection(t *testing.T) { }{ {"invalid source", "https||:example.org/somewhere", "my.site/part/target", errSrcInvalid}, {"target no accepted", "https://example.org/somewhere", "wrong.site/tgt", errTgtNotAccepted}, + {"wrong source scheme", "ftp://example.org/somewhere", "http://my.site/part/tgt", errInvalidScheme}, + {"wrong source scheme", "http://example.org/somewhere", "ssh://my.site/part/tgt", errInvalidScheme}, } for _, tc := range tests { |