From 9d80f7fd68c5a1c243f1d90a05d8fec028f1c9e8 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 22 Jan 2024 15:33:01 +0100 Subject: [feature] Allow "charset=utf8" in incoming AP POST requests (#2564) * [feature] Allow "charset=utf8" in incoming AP POST requests * changed my mind * document POSTing to a GtS inbox * correct link --- internal/federation/federatingactor.go | 17 +++++++++++------ internal/federation/federatingactor_test.go | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) (limited to 'internal') diff --git a/internal/federation/federatingactor.go b/internal/federation/federatingactor.go index 8fc47462d..b91165bb1 100644 --- a/internal/federation/federatingactor.go +++ b/internal/federation/federatingactor.go @@ -40,7 +40,7 @@ import ( // - application/activity+json // - application/ld+json;profile=https://w3.org/ns/activitystreams // -// Where for the above we are leniant with whitespace and quotes. +// Where for the above we are leniant with whitespace, quotes, and charset. func IsASMediaType(ct string) bool { var ( // First content-type part, @@ -48,7 +48,8 @@ func IsASMediaType(ct string) bool { p1 string = ct //nolint:revive // Second content-type part, - // contains AS IRI if provided + // contains AS IRI or charset + // if provided. p2 string ) @@ -56,7 +57,11 @@ func IsASMediaType(ct string) bool { sep := strings.IndexByte(ct, ';') if sep >= 0 { p1 = ct[:sep] + + // Trim all start/end + // space of second part. p2 = ct[sep+1:] + p2 = strings.Trim(p2, " ") } // Trim any ending space from the @@ -65,12 +70,12 @@ func IsASMediaType(ct string) bool { switch p1 { case "application/activity+json": - return p2 == "" + // Accept with or without charset. + // This should be case insensitive. + // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type#charset + return p2 == "" || strings.EqualFold(p2, "charset=utf-8") case "application/ld+json": - // Trim all start/end space. - p2 = strings.Trim(p2, " ") - // Drop any quotes around the URI str. p2 = strings.ReplaceAll(p2, "\"", "") diff --git a/internal/federation/federatingactor_test.go b/internal/federation/federatingactor_test.go index 348d501d6..d07b56537 100644 --- a/internal/federation/federatingactor_test.go +++ b/internal/federation/federatingactor_test.go @@ -164,6 +164,22 @@ func TestIsASMediaType(t *testing.T) { Input: "application/activity+json", Expect: true, }, + { + Input: "application/activity+json; charset=utf-8", + Expect: true, + }, + { + Input: "application/activity+json;charset=utf-8", + Expect: true, + }, + { + Input: "application/activity+json ;charset=utf-8", + Expect: true, + }, + { + Input: "application/activity+json ; charset=utf-8", + Expect: true, + }, { Input: "application/ld+json;profile=https://www.w3.org/ns/activitystreams", Expect: true, @@ -196,6 +212,10 @@ func TestIsASMediaType(t *testing.T) { Input: "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", Expect: true, }, + { + Input: "application/ld+json", + Expect: false, + }, } { if federation.IsASMediaType(test.Input) != test.Expect { t.Errorf("did not get expected result %v for input: %s", test.Expect, test.Input) -- cgit v1.2.3