1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-09-28 19:31:59 +02:00
This commit is contained in:
Philipp Heckel 2022-07-15 20:35:23 -04:00
parent 9514e97219
commit 09cb1482b4
2 changed files with 49 additions and 12 deletions

View file

@ -96,6 +96,8 @@ const (
defaultAttachmentMessage = "You received a file: %s" // Used if message body is empty, and there is an attachment defaultAttachmentMessage = "You received a file: %s" // Used if message body is empty, and there is an attachment
encodingBase64 = "base64" encodingBase64 = "base64"
encodingJWE = "jwe" encodingJWE = "jwe"
multipartFieldMessage = "message"
multipartFieldAttachment = "attachment"
) )
// WebSocket constants // WebSocket constants
@ -539,8 +541,8 @@ func (s *Server) handlePublishEncrypted(r *http.Request, m *message) (body *util
p, err := mp.NextPart() p, err := mp.NextPart()
if err != nil { if err != nil {
return nil, err return nil, err
} else if p.FormName() != "message" { } else if p.FormName() != multipartFieldMessage {
return nil, errHTTPBadRequestUnexpectedMultipartField return nil, wrapErrHTTP(errHTTPBadRequestUnexpectedMultipartField, "expected '%s', got '%s'", multipartFieldMessage, p.FormName())
} }
messageBody, err := util.PeekLimit(p, s.config.MessageLimit) messageBody, err := util.PeekLimit(p, s.config.MessageLimit)
if err == util.ErrLimitReached { if err == util.ErrLimitReached {
@ -552,11 +554,11 @@ func (s *Server) handlePublishEncrypted(r *http.Request, m *message) (body *util
p, err = mp.NextPart() p, err = mp.NextPart()
if err != nil { if err != nil {
return nil, err return nil, err
} else if p.FormName() != "attachment" { } else if p.FormName() != multipartFieldAttachment {
return nil, errHTTPBadRequestUnexpectedMultipartField return nil, wrapErrHTTP(errHTTPBadRequestUnexpectedMultipartField, "expected '%s', got '%s'", multipartFieldAttachment, p.FormName())
} }
m.Attachment = &attachment{ m.Attachment = &attachment{
Name: "attachment.jwe", // Force handlePublishBody into "attachment" mode Name: "attachment.jwe", // Force handlePublishBody into "attachment" mode; .jwe forces application/jose type
} }
body, err = util.Peek(p, s.config.MessageLimit) body, err = util.Peek(p, s.config.MessageLimit)
if err != nil { if err != nil {

View file

@ -1487,9 +1487,9 @@ func TestServer_PublishEncrypted_Simple_TooLarge(t *testing.T) {
func TestServer_PublishEncrypted_WithAttachment(t *testing.T) { func TestServer_PublishEncrypted_WithAttachment(t *testing.T) {
s := newTestServer(t, newTestConfig(t)) s := newTestServer(t, newTestConfig(t))
parts := map[string]string{ parts := []mpart{
"message": "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..gSRYZeX6eBhlj13w.LOchcxFXwALXE2GqdoSwFJEXdMyEbLfLKV9geXr17WrAN-nH7ya1VQ_Y6ebT1w.2eyLaTUfc_rpKaZr4-5I1Q", {"message", "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..gSRYZeX6eBhlj13w.LOchcxFXwALXE2GqdoSwFJEXdMyEbLfLKV9geXr17WrAN-nH7ya1VQ_Y6ebT1w.2eyLaTUfc_rpKaZr4-5I1Q"},
"attachment": "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..vbe1Qv_-mKYbUgce.EfmOUIUi7lxXZG_o4bqXZ9pmpr1Rzs4Y5QLE2XD2_aw_SQ.y2hadrN5b2LEw7_PJHhbcA", {"attachment", "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..vbe1Qv_-mKYbUgce.EfmOUIUi7lxXZG_o4bqXZ9pmpr1Rzs4Y5QLE2XD2_aw_SQ.y2hadrN5b2LEw7_PJHhbcA"},
} }
response := requestMultipart(t, s, "PUT", "/mytopic", parts, map[string]string{ response := requestMultipart(t, s, "PUT", "/mytopic", parts, map[string]string{
"Encoding": "jwe", "Encoding": "jwe",
@ -1506,6 +1506,37 @@ func TestServer_PublishEncrypted_WithAttachment(t *testing.T) {
require.Equal(t, "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..vbe1Qv_-mKYbUgce.EfmOUIUi7lxXZG_o4bqXZ9pmpr1Rzs4Y5QLE2XD2_aw_SQ.y2hadrN5b2LEw7_PJHhbcA", readFile(t, file)) require.Equal(t, "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..vbe1Qv_-mKYbUgce.EfmOUIUi7lxXZG_o4bqXZ9pmpr1Rzs4Y5QLE2XD2_aw_SQ.y2hadrN5b2LEw7_PJHhbcA", readFile(t, file))
} }
func TestServer_PublishEncrypted_WithAttachment_TooLarge_Attachment(t *testing.T) {
c := newTestConfig(t)
c.AttachmentFileSizeLimit = 5000
s := newTestServer(t, c)
parts := []mpart{
{"message", "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..gSRYZeX6eBhlj13w.LOchcxFXwALXE2GqdoSwFJEXdMyEbLfLKV9geXr17WrAN-nH7ya1VQ_Y6ebT1w.2eyLaTUfc_rpKaZr4-5I1Q"},
{"attachment", strings.Repeat("a", 5001)}, // > 5000
}
response := requestMultipart(t, s, "PUT", "/mytopic", parts, map[string]string{
"Encoding": "jwe",
})
err := toHTTPError(t, response.Body.String())
require.Equal(t, 413, err.HTTPCode)
require.Equal(t, 41301, err.Code)
}
func TestServer_PublishEncrypted_WithAttachment_TooLarge_Message(t *testing.T) {
s := newTestServer(t, newTestConfig(t))
parts := []mpart{
{"message", strings.Repeat("a", 5000)},
{"attachment", "eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..gSRYZeX6eBhlj13w.LOchcxFXwALXE2GqdoSwFJEXdMyEbLfLKV9geXr17WrAN-nH7ya1VQ_Y6ebT1w.2eyLaTUfc_rpKaZr4-5I1Q"},
}
response := requestMultipart(t, s, "PUT", "/mytopic", parts, map[string]string{
"Encoding": "jwe",
})
err := toHTTPError(t, response.Body.String())
log.Printf(err.Error())
require.Equal(t, 413, err.HTTPCode)
require.Equal(t, 41303, err.Code)
}
func newTestConfig(t *testing.T) *Config { func newTestConfig(t *testing.T) *Config {
conf := NewConfig() conf := NewConfig()
conf.BaseURL = "http://127.0.0.1:12345" conf.BaseURL = "http://127.0.0.1:12345"
@ -1536,12 +1567,16 @@ func request(t *testing.T, s *Server, method, url, body string, headers map[stri
return rr return rr
} }
func requestMultipart(t *testing.T, s *Server, method, url string, parts map[string]string, headers map[string]string) *httptest.ResponseRecorder { type mpart struct {
key, value string
}
func requestMultipart(t *testing.T, s *Server, method, url string, parts []mpart, headers map[string]string) *httptest.ResponseRecorder {
var b bytes.Buffer var b bytes.Buffer
w := multipart.NewWriter(&b) w := multipart.NewWriter(&b)
for k, v := range parts { for _, part := range parts {
mw, _ := w.CreateFormField(k) mw, _ := w.CreateFormField(part.key)
_, err := io.Copy(mw, strings.NewReader(v)) _, err := io.Copy(mw, strings.NewReader(part.value))
require.Nil(t, err) require.Nil(t, err)
} }
require.Nil(t, w.Close()) require.Nil(t, w.Close())