diff options
Diffstat (limited to 'yamn/encoder/encoder.go')
| -rw-r--r-- | yamn/encoder/encoder.go | 57 |
1 files changed, 48 insertions, 9 deletions
diff --git a/yamn/encoder/encoder.go b/yamn/encoder/encoder.go index 3b595b1..6c730c9 100644 --- a/yamn/encoder/encoder.go +++ b/yamn/encoder/encoder.go @@ -24,13 +24,15 @@ import ( ) const ( - maxChainLength = 10 - headerBytes = 256 - encHeadBytes = 160 - bodyBytes = 17920 - maxPlainBytes = 17910 - messageBytes = maxChainLength*headerBytes + bodyBytes - armorVersion = "0.2.7" + maxChainLength = 10 + headerBytes = 256 + encHeadBytes = 160 + bodyBytes = 17920 + maxPlainBytes = 17910 + maxReferencesBytes = 900 + maxReferenceIDs = 20 + messageBytes = maxChainLength*headerBytes + bodyBytes + armorVersion = "0.2.7" ) var ( @@ -137,6 +139,14 @@ func Validate(r Request) error { return fmt.Errorf("%w: invalid header value", ErrInvalidRequest) } } + if strings.TrimSpace(r.ReplyTo) != "" { + if _, err := mail.ParseAddress(r.ReplyTo); err != nil { + return fmt.Errorf("%w: invalid Reply-To address", ErrInvalidRequest) + } + } + if !validReferences(r.References) { + return fmt.Errorf("%w: invalid References message ID", ErrInvalidRequest) + } return nil } @@ -158,8 +168,9 @@ func composeMessage(r Request) ([]byte, error) { if r.Newsgroup != "" { b.WriteString("Newsgroups: " + r.Newsgroup + "\n") } - if r.References != "" { - b.WriteString("References: " + r.References + "\n") + if references := strings.Fields(r.References); len(references) > 0 { + b.WriteString("References: " + strings.Join(references, " ") + "\n") + b.WriteString("In-Reply-To: " + references[len(references)-1] + "\n") } b.WriteString("\n") b.WriteString(r.Body) @@ -167,6 +178,34 @@ func composeMessage(r Request) ([]byte, error) { } func validHeaderValue(value string) bool { return !strings.ContainsAny(value, "\r\n\x00") } +func validReferences(value string) bool { + if value == "" { + return true + } + if len(value) > maxReferencesBytes { + return false + } + references := strings.Fields(value) + if len(references) == 0 || len(references) > maxReferenceIDs { + return false + } + for _, reference := range references { + if len(reference) < 5 || reference[0] != '<' || reference[len(reference)-1] != '>' { + return false + } + messageID := reference[1 : len(reference)-1] + if strings.Count(messageID, "@") != 1 || strings.HasPrefix(messageID, "@") || strings.HasSuffix(messageID, "@") { + return false + } + for _, character := range messageID { + if character < 33 || character > 126 || character == '<' || character == '>' { + return false + } + } + } + return true +} + func isRemailerName(s string) bool { if s == "" { return false |
