Skip to content

Commit

Permalink
fix(modify): allow adding multiple entries for existing headers
Browse files Browse the repository at this point in the history
  • Loading branch information
msessa committed Mar 3, 2025
1 parent 877e9a0 commit 495e264
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
4 changes: 1 addition & 3 deletions docs/reference/modifiers/header.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

`add_header` module modifies the message by adding an header.

Note: the header must be present in the message prior to the execution of the modifier.
If the header already exist it will result in an error.

Note: Adding a header with an existing key will create multiple entries for that key in the message.

Definition:

Expand Down
7 changes: 1 addition & 6 deletions internal/modify/add_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package modify
import (
"context"
"errors"
"fmt"

"github.com/emersion/go-message/textproto"
"github.com/foxcpp/maddy/framework/buffer"
Expand Down Expand Up @@ -77,11 +76,7 @@ func (r addHeader) RewriteRcpt(ctx context.Context, rcptTo string) ([]string, er
}

func (r addHeader) RewriteBody(ctx context.Context, h *textproto.Header, body buffer.Buffer) error {
if h.Has(r.headerName) {
return fmt.Errorf("cannot add header `%s` as it is already present", r.headerName)
}

h.Set(r.headerName, r.headerValue)
h.Add(r.headerName, r.headerValue)
return nil
}

Expand Down
25 changes: 10 additions & 15 deletions internal/modify/add_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

func TestAddHeader(t *testing.T) {
test := func(headerName string, headerValue string, duplicateHeader bool) {
test := func(headerName string, headerValue string, expectedValues []string) {
t.Helper()

mod, err := NewAddHeader("modify.add_header", "")
Expand Down Expand Up @@ -60,15 +60,8 @@ func TestAddHeader(t *testing.T) {

err = state.RewriteBody(context.Background(), &testHdr, buffer.MemoryBuffer{Slice: body})
if err != nil {
if duplicateHeader && strings.Contains(err.Error(), "already present") {
return
}
t.Fatal(err)
}
if duplicateHeader && err == nil {
t.Fatalf("expected error on duplicate header")
}

var fullBody bytes.Buffer
if err := textproto.WriteHeader(&fullBody, testHdr); err != nil {
t.Fatal(err)
Expand All @@ -77,14 +70,16 @@ func TestAddHeader(t *testing.T) {
t.Fatal(err)
}

if !strings.Contains(fmt.Sprintf("%s", &fullBody), fmt.Sprintf("%s: %s", headerName, headerValue)) {
t.Fatalf("new header not found in message")
for _, wantedValue := range expectedValues {
if !strings.Contains(fullBody.String(), fmt.Sprintf("%s: %s", headerName, wantedValue)) {
t.Fatalf("new header not found in message")
}
}

}

test("Something", "Somevalue", false)
test("X-Testing", "Somevalue", false)
// Test setting a header that is already present
test("To", "Somevalue", true)
test("To", "Somevalue", true)
test("Something", "Somevalue", []string{"Somevalue"})
test("X-Testing", "Somevalue", []string{"Somevalue"})
// Test setting a header that is already present should result two entries
test("To", "Somevalue", []string{"<heya@heya>", "Somevalue"})
}

0 comments on commit 495e264

Please sign in to comment.