Skip to content

Commit

Permalink
restrict MTA-STS policy server response size to avoid DoS
Browse files Browse the repository at this point in the history
  • Loading branch information
Snawoot committed Feb 21, 2019
1 parent 04cdd40 commit 4383449
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion postfix_mta_sts_resolver/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
import aiodns
import aiohttp
import enum
from io import BytesIO
from . import defaults
from .utils import parse_mta_sts_record, parse_mta_sts_policy, is_plaintext


HARD_RESP_LIMIT = 64 * 1024
CHUNK = 4096


class BadSTSPolicy(Exception):
pass

Expand Down Expand Up @@ -92,7 +97,20 @@ async def resolve(self, domain, last_known_id=None):
raise BadSTSPolicy()
if not is_plaintext(resp.headers.get('Content-Type', '')):
raise BadSTSPolicy()
policy_text = await resp.text()
if (int(resp.headers.get('Content-Length', '0')) >
HARD_RESP_LIMIT):
raise BadSTSPolicy()
policy_file = BytesIO()
while policy_file.tell() <= HARD_RESP_LIMIT:
chunk = await resp.content.read(CHUNK)
if not chunk:
break
policy_file.write(chunk)
else:
raise BadSTSPolicy()
charset = (resp.charset if resp.charset is not None
else 'ascii')
policy_text = policy_file.getvalue().decode(charset)
except:
return STSFetchResult.FETCH_ERROR, None

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
long_description = f.read()

setup(name='postfix_mta_sts_resolver',
version='0.2.4',
version='0.2.5',
description='Daemon which provides TLS client policy for Postfix via socketmap, according to domain MTA-STS policy',
url='https://github.com/Snawoot/postfix-mta-sts-resolver',
author='Vladislav Yarmak',
Expand Down

0 comments on commit 4383449

Please sign in to comment.