[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
This commit is contained in:
tobi 2024-01-22 15:33:01 +01:00 committed by GitHub
commit 9d80f7fd68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 6 deletions

View file

@ -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, "\"", "")