Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pymilter-suspicious-from
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
misc
pymilter-suspicious-from
Commits
358ec62a
Commit
358ec62a
authored
5 years ago
by
Jan Philipp Timme
Browse files
Options
Downloads
Patches
Plain Diff
Use builtin python tools to do address/header parsing
parent
e343be18
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.py
+37
-41
37 additions, 41 deletions
main.py
with
37 additions
and
41 deletions
main.py
+
37
−
41
View file @
358ec62a
...
...
@@ -6,6 +6,10 @@ import Milter
import
re
from
email.header
import
decode_header
from
email.utils
import
getaddresses
# Basic logger that also logs to stdout
# TODO: Improve this a lot.
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -18,36 +22,27 @@ handler.setFormatter(formatter)
logger
.
addHandler
(
handler
)
split_from_regex
=
re
.
compile
(
'
(?P<from_label>.*)<(?P<from_address>.*)>$
'
)
address_domain_regex
=
re
.
compile
(
'
.*@(?P<domain>[\.\w-]+)
'
)
def
normalizeRawFromHeader
(
value
):
return
value
.
replace
(
'
\n
'
,
''
).
replace
(
'
\r
'
,
''
).
strip
()
def
get_decoded_header
(
value
):
decoded_header_items
=
decode_header
(
value
)
decoded_header_value
=
''
for
item
in
decoded_header_items
:
decoded_item
=
item
[
0
].
decode
(
item
[
1
])
if
item
[
1
]
is
not
None
else
item
[
0
]
if
isinstance
(
decoded_item
,
bytes
):
decoded_item
=
decoded_item
.
decode
(
'
ascii
'
)
decoded_header_value
+=
decoded_item
return
getaddresses
([
decoded_header_value
])[
0
]
def
parseFromHeader
(
value
):
"""
Split
'
From:
'
-header into label and address values.
"""
match
=
split_from_regex
.
match
(
value
)
if
match
is
None
:
return
None
result
=
{
'
label
'
:
match
.
group
(
'
from_label
'
).
strip
(),
'
address
'
:
match
.
group
(
'
from_address
'
).
strip
()
}
result
[
'
label_domain
'
]
=
getDomainFromLabel
(
result
[
'
label
'
])
result
[
'
address_domain
'
]
=
getDomainFromAddress
(
result
[
'
address
'
])
return
result
def
normalizeRawFromHeader
(
value
):
return
value
.
replace
(
'
\n
'
,
''
).
replace
(
'
\r
'
,
''
).
strip
()
def
getDomainFrom
Label
(
label
):
def
getDomainFrom
Value
(
value
):
"""
Check whether given
'
From:
'
header label contains something that looks like an email address.
"""
match
=
address_domain_regex
.
match
(
label
)
return
match
.
group
(
'
domain
'
).
strip
()
if
match
is
not
None
else
None
def
getDomainFromAddress
(
address
):
match
=
address_domain_regex
.
match
(
address
)
match
=
address_domain_regex
.
match
(
value
)
return
match
.
group
(
'
domain
'
).
strip
()
if
match
is
not
None
else
None
...
...
@@ -67,27 +62,28 @@ class SuspiciousFrom(Milter.Base):
logger
.
debug
(
f
"
(
{
self
.
id
}
) Got
\"
From:
\"
header raw value:
'
{
value
}
'"
)
value
=
normalizeRawFromHeader
(
value
)
if
value
==
''
:
logger
.
info
(
f
"
Got empty from header value! WTF! Skipping.
"
)
logger
.
warn
(
f
"
Got empty from header value! WTF! Skipping.
"
)
return
Milter
.
CONTINUE
data
=
parseFromHeader
(
value
)
if
data
is
None
:
logger
.
info
(
f
"
Failed to parse given from header value! Skipping.
"
)
return
Milter
.
CONTINUE
logger
.
info
(
f
"
(
{
self
.
id
}
) Label:
'
{
data
[
'
label
'
]
}
'
, Address:
'
{
data
[
'
address
'
]
}
'"
)
if
data
[
'
label_domain
'
]
is
not
None
:
logger
.
debug
(
f
"
(
{
self
.
id
}
) Label
'
{
data
[
'
label
'
]
}
'
contains an address with domain
'
{
data
[
'
label_domain
'
]
}
'
.
"
)
if
data
[
'
label_domain
'
].
lower
()
==
data
[
'
address_domain
'
].
lower
():
logger
.
info
(
f
"
(
{
self
.
id
}
) Label domain
'
{
data
[
'
label_domain
'
]
}
'
matches address domain
'
{
data
[
'
address_domain
'
]
}
'
. Good!
"
)
data
=
get_decoded_header
(
value
)
logger
.
info
(
f
"
(
{
self
.
id
}
) Label:
'
{
data
[
0
]
}
'
, Address:
'
{
data
[
1
]
}
'"
)
if
data
[
0
]
==
''
:
logger
.
info
(
f
"
(
{
self
.
id
}
) No label in from header, OK!
"
)
self
.
new_headers
.
append
({
'
name
'
:
'
X-From-Checked
'
,
'
value
'
:
'
OK - No label specified
'
})
else
:
label_domain
=
getDomainFromValue
(
data
[
0
])
address_domain
=
getDomainFromValue
(
data
[
1
])
logger
.
info
(
f
"
(
{
self
.
id
}
)Extracted label_domain
'
{
label_domain
}
'
and address_domain
'
{
address_domain
}
'"
)
if
label_domain
is
not
None
:
logger
.
debug
(
f
"
(
{
self
.
id
}
) Label
'
{
data
[
0
]
}
'
contains an address with domain
'
{
label_domain
}
'
.
"
)
if
label_domain
.
lower
()
==
address_domain
.
lower
():
logger
.
info
(
f
"
(
{
self
.
id
}
) Label domain
'
{
label_domain
}
'
matches address domain
'
{
address_domain
}
'
. Good!
"
)
self
.
new_headers
.
append
({
'
name
'
:
'
X-From-Checked
'
,
'
value
'
:
'
OK - Label domain matches address domain
'
})
else
:
logger
.
info
(
f
"
(
{
self
.
id
}
) Label domain
'
{
data
[
'
label_domain
'
]
}
'
did NOT match address domain
'
{
data
[
'
address_domain
'
]
}
'
. BAD!
"
)
logger
.
info
(
f
"
(
{
self
.
id
}
) Label domain
'
{
label_domain
}
'
did NOT match address domain
'
{
address_domain
}
'
. BAD!
"
)
self
.
new_headers
.
append
({
'
name
'
:
'
X-From-Checked
'
,
'
value
'
:
'
FAIL - Label domain does NOT match address domain
'
})
else
:
# Supposedly no additional address in the label, accept it for now
# TODO: Also decode utf-8 weirdness and check in there
logger
.
info
(
f
"
(
{
self
.
id
}
) Label
'
{
data
[
'
label
'
]
}
'
probably did not contain an address. Everything is fine.
"
)
self
.
new_headers
.
append
({
'
name
'
:
'
X-From-Checked
'
,
'
value
'
:
'
OK - No address found in label
'
})
self
.
final_result
=
Milter
.
ACCEPT
logger
.
info
(
f
"
(
{
self
.
id
}
) No domain found in label. Good!
"
)
self
.
new_headers
.
append
({
'
name
'
:
'
X-From-Checked
'
,
'
value
'
:
'
OK - No domain found in label.
'
})
# Use continue here, so we can reach eom hook.
# TODO: Log and react if multiple From-headers are found?
return
Milter
.
CONTINUE
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment