mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-17 14:37:02 +00:00
courier: Make procmail's sanitize be more unicode friendly
Sanitize only lets some ascii characters go through, which is very unfriendly to non-ascii usernames. This patch changes it to be more inclusive, and filter out only selected characters that may be problematic for the subprocesses, specially considering UNIX shell environments. It's not meant to catch all possible problems, just help prevent some common ones.
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"blitiri.com.ar/go/chasquid/internal/trace"
|
"blitiri.com.ar/go/chasquid/internal/trace"
|
||||||
)
|
)
|
||||||
@@ -79,14 +80,19 @@ func (p *Procmail) Deliver(from string, to string, data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// sanitizeForProcmail cleans the string, leaving only [a-zA-Z-.].
|
// sanitizeForProcmail cleans the string, removing characters that could be
|
||||||
|
// problematic considering we will run an external command.
|
||||||
|
//
|
||||||
|
// The server does not rely on this to do substitution or proper filtering,
|
||||||
|
// that's done at a different layer; this is just for defense in depth.
|
||||||
func sanitizeForProcmail(s string) string {
|
func sanitizeForProcmail(s string) string {
|
||||||
valid := func(r rune) rune {
|
valid := func(r rune) rune {
|
||||||
switch {
|
switch {
|
||||||
case r >= 'A' && r <= 'Z', r >= 'a' && r <= 'z', r == '-', r == '.':
|
case unicode.IsSpace(r), unicode.IsControl(r),
|
||||||
return r
|
strings.ContainsRune("/;\"'\\|*&$%()[]{}`!", r):
|
||||||
default:
|
|
||||||
return rune(-1)
|
return rune(-1)
|
||||||
|
default:
|
||||||
|
return r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return strings.Map(valid, s)
|
return strings.Map(valid, s)
|
||||||
|
|||||||
@@ -63,3 +63,32 @@ func TestProcmailBadCommandLine(t *testing.T) {
|
|||||||
t.Errorf("Unexpected success: %q %v", procmailBin, procmailArgs)
|
t.Errorf("Unexpected success: %q %v", procmailBin, procmailArgs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSanitize(t *testing.T) {
|
||||||
|
cases := []struct{ v, expected string }{
|
||||||
|
// These are the same.
|
||||||
|
{"thisisfine", "thisisfine"},
|
||||||
|
{"ñaca", "ñaca"},
|
||||||
|
{"123-456_789", "123-456_789"},
|
||||||
|
{"123+456~789", "123+456~789"},
|
||||||
|
|
||||||
|
// These have problematic characters that get dropped.
|
||||||
|
{"with spaces", "withspaces"},
|
||||||
|
{"with/slash", "withslash"},
|
||||||
|
{"quote';andsemicolon", "quoteandsemicolon"},
|
||||||
|
{"a;b", "ab"},
|
||||||
|
{`"test"`, "test"},
|
||||||
|
|
||||||
|
// Interesting cases taken from
|
||||||
|
// http://www.user.uni-hannover.de/nhtcapri/bidirectional-text.html
|
||||||
|
// We allow them, they're the same on both sides.
|
||||||
|
{"١٩٩٩–١٢–٣١", "١٩٩٩–١٢–٣١"},
|
||||||
|
{"موزهها", "موزه\u200cها"},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
out := sanitizeForProcmail(c.v)
|
||||||
|
if out != c.expected {
|
||||||
|
t.Errorf("%q: expected %q, got %q", c.v, c.expected, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user