Skip to content

Support for inserting headers at arbitrary position #13

Description

@bc547

Your current implementation only has support for adding a header at the end of the headers. Some message signing techniques require that no additional headers are added at the end because it messes up the signature verification. Also, multiple signatures issued by different MTA's are a problem with append-only headers.

Milter protocol also supports inserting a header at an arbitrary position. See e.g. page 63 of http://dannykim.me/danny/?module=file&act=procFileDownload&file_srl=685&sid=48ab5391338c15e9a860cb9a02cae406 and the value for SMFIR_INSHEADER on https://opensource.apple.com/source/postfix/postfix-174/postfix/src/milter/milter8.c

Since it is very similar to the change header command, adding support for this is rather easy. It is sufficient to add the next piece of code to the modifier.go file. It's the ChangeHeader code copied and the command byte was changed from 'm' to 'i'.

// InsertHeader inserts a new email message header at the specified position
func (m *Modifier) InsertHeader(index int, name, value string) error {
	buffer := new(bytes.Buffer)
	// encode header index in the beginning
	if err := binary.Write(buffer, binary.BigEndian, uint32(index)); err != nil {
		return err
	}
	// add header name and value to buffer
	data := []byte(name + null + value + null)
	if _, err := buffer.Write(data); err != nil {
		return err
	}
	// prepare and send response packet
	return m.writePacket(NewResponse('i', buffer.Bytes()).Response())
}

I tested it with postfix and it works perfectly. The header is inserted exactly where it should be (right before the Received: line of the MTA that connects to the milter service.

Would it be possible to add this feature?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions