binwiederhier 2 лет назад
Родитель
Сommit
35eac5b9ad
3 измененных файлов с 10 добавлено и 14 удалено
  1. 0 1
      server/errors.go
  2. 2 13
      server/server.go
  3. 8 0
      server/util.go

+ 0 - 1
server/errors.go

@@ -106,7 +106,6 @@ var (
 	errHTTPBadRequestNotAPaidUser                    = &errHTTP{40027, http.StatusBadRequest, "invalid request: not a paid user", "", nil}
 	errHTTPBadRequestNotAPaidUser                    = &errHTTP{40027, http.StatusBadRequest, "invalid request: not a paid user", "", nil}
 	errHTTPBadRequestBillingRequestInvalid           = &errHTTP{40028, http.StatusBadRequest, "invalid request: not a valid billing request", "", nil}
 	errHTTPBadRequestBillingRequestInvalid           = &errHTTP{40028, http.StatusBadRequest, "invalid request: not a valid billing request", "", nil}
 	errHTTPBadRequestBillingSubscriptionExists       = &errHTTP{40029, http.StatusBadRequest, "invalid request: billing subscription already exists", "", nil}
 	errHTTPBadRequestBillingSubscriptionExists       = &errHTTP{40029, http.StatusBadRequest, "invalid request: billing subscription already exists", "", nil}
-	errHTTPBadRequestInvalidMimeHeader               = &errHTTP{40030, http.StatusBadRequest, "invalid request: invalid MIME encoding of header", "", nil}
 	errHTTPNotFound                                  = &errHTTP{40401, http.StatusNotFound, "page not found", "", nil}
 	errHTTPNotFound                                  = &errHTTP{40401, http.StatusNotFound, "page not found", "", nil}
 	errHTTPUnauthorized                              = &errHTTP{40101, http.StatusUnauthorized, "unauthorized", "https://ntfy.sh/docs/publish/#authentication", nil}
 	errHTTPUnauthorized                              = &errHTTP{40101, http.StatusUnauthorized, "unauthorized", "https://ntfy.sh/docs/publish/#authentication", nil}
 	errHTTPForbidden                                 = &errHTTP{40301, http.StatusForbidden, "forbidden", "https://ntfy.sh/docs/publish/#authentication", nil}
 	errHTTPForbidden                                 = &errHTTP{40301, http.StatusForbidden, "forbidden", "https://ntfy.sh/docs/publish/#authentication", nil}

+ 2 - 13
server/server.go

@@ -843,17 +843,10 @@ func (s *Server) forwardPollRequest(v *visitor, m *message) {
 func (s *Server) parsePublishParams(r *http.Request, m *message) (cache bool, firebase bool, email string, unifiedpush bool, err *errHTTP) {
 func (s *Server) parsePublishParams(r *http.Request, m *message) (cache bool, firebase bool, email string, unifiedpush bool, err *errHTTP) {
 	cache = readBoolParam(r, true, "x-cache", "cache")
 	cache = readBoolParam(r, true, "x-cache", "cache")
 	firebase = readBoolParam(r, true, "x-firebase", "firebase")
 	firebase = readBoolParam(r, true, "x-firebase", "firebase")
+	m.Title = maybeDecodeHeader(readParam(r, "x-title", "title", "t"))
 	m.Click = readParam(r, "x-click", "click")
 	m.Click = readParam(r, "x-click", "click")
 	icon := readParam(r, "x-icon", "icon")
 	icon := readParam(r, "x-icon", "icon")
 	filename := readParam(r, "x-filename", "filename", "file", "f")
 	filename := readParam(r, "x-filename", "filename", "file", "f")
-	title := readParam(r, "x-title", "title", "t")
-	if title != "" {
-		title, err := mimeDecoder.DecodeHeader(title)
-		if err != nil {
-			return false, false, "", false, errHTTPBadRequestInvalidMimeHeader.Wrap("invalid X-Title header: %s", err.Error())
-		}
-		m.Title = title
-	}
 	attach := readParam(r, "x-attach", "attach", "a")
 	attach := readParam(r, "x-attach", "attach", "a")
 	if attach != "" || filename != "" {
 	if attach != "" || filename != "" {
 		m.Attachment = &attachment{}
 		m.Attachment = &attachment{}
@@ -891,11 +884,7 @@ func (s *Server) parsePublishParams(r *http.Request, m *message) (cache bool, fi
 	}
 	}
 	messageStr := strings.ReplaceAll(readParam(r, "x-message", "message", "m"), "\\n", "\n")
 	messageStr := strings.ReplaceAll(readParam(r, "x-message", "message", "m"), "\\n", "\n")
 	if messageStr != "" {
 	if messageStr != "" {
-		messageStr, err := mimeDecoder.DecodeHeader(messageStr)
-		if err != nil {
-			return false, false, "", false, errHTTPBadRequestInvalidMimeHeader.Wrap("invalid X-Message header: %s", err.Error())
-		}
-		m.Message = messageStr
+		m.Message = maybeDecodeHeader(messageStr)
 	}
 	}
 	var e error
 	var e error
 	m.Priority, e = util.ParsePriority(readParam(r, "x-priority", "priority", "prio", "p"))
 	m.Priority, e = util.ParsePriority(readParam(r, "x-priority", "priority", "prio", "p"))

+ 8 - 0
server/util.go

@@ -117,3 +117,11 @@ func fromContext[T any](r *http.Request, key contextKey) (T, error) {
 	}
 	}
 	return t, nil
 	return t, nil
 }
 }
+
+func maybeDecodeHeader(header string) string {
+	decoded, err := mimeDecoder.DecodeHeader(header)
+	if err != nil {
+		return header
+	}
+	return decoded
+}