Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vendor ipaddress and email.utils; renorm ipv4 and ipv6 types based on updated ipaddress corrections (SYN-5490, SYN-7937) #3902

Merged
merged 22 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ef6ab1a
Preliminary updates based on CPython 3.11.10 changes (SYN-5490, SYN-7…
vEpiphyte Sep 9, 2024
2a5c099
Merge branch 'master' into feat_python311_compat
vEpiphyte Sep 9, 2024
08c2926
Fix pycodestyle issues with ipaddress
vEpiphyte Sep 9, 2024
d414420
Merge branch 'feat_python311_compat' of github.com:vertexproject/syna…
vEpiphyte Sep 9, 2024
7843c76
Merge branch 'master' into feat_python311_compat
vEpiphyte Sep 13, 2024
ae36db5
Vendor in email.utils and email._parseaddr; account for syntax errors…
vEpiphyte Sep 13, 2024
ab04f8a
Add model migration
vEpiphyte Sep 14, 2024
91ff632
Cleanup imports / comments
vEpiphyte Sep 14, 2024
d413350
Add changelogs
vEpiphyte Sep 14, 2024
e85b9e0
move ipv4 test
vEpiphyte Sep 14, 2024
8a37de9
Merge branch 'master' into feat_python311_compat
vEpiphyte Sep 20, 2024
ed18677
Fix maxvers
vEpiphyte Sep 20, 2024
1a1ccdd
Add unit tests for ipaddress and email.utils
vEpiphyte Sep 20, 2024
c785347
Apply suggestions from code review
vEpiphyte Sep 20, 2024
78826f1
Apply suggestions from code review
vEpiphyte Sep 20, 2024
4e90dd7
Vendor part of test/test_email/test_email.py
vEpiphyte Sep 20, 2024
3293b5d
Move lib.test to lib.tests for pytest help.
vEpiphyte Sep 22, 2024
02169a1
Revert "Move lib.test to lib.tests for pytest help."
vEpiphyte Sep 22, 2024
1481b46
change the circleci test globbing for vendor test files.
vEpiphyte Sep 22, 2024
37c0768
Remove old OSX from cpython vendored tests, mark unittest entrypoints…
vEpiphyte Sep 23, 2024
07344cc
Merge branch 'master' into feat_python311_compat
vEpiphyte Sep 24, 2024
752e875
Comment out unused functionality from email.utils
vEpiphyte Sep 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions synapse/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import builtins
import tempfile
import warnings
import ipaddress
import functools
import itertools
import threading
Expand Down Expand Up @@ -68,6 +69,12 @@
logger.warning('* See PyYAML docs (https://pyyaml.org/wiki/PyYAMLDocumentation) for tips on resolving this issue. *')
logger.warning('*****************************************************************************************************')

if version < (3, 11, 10):
# FIXME REMOTE PRINT
print(f'FALLLING BACK TO VENDORED IPADDRESS LIBRARY: {version}')

Check warning on line 74 in synapse/common.py

View check run for this annotation

Codecov / codecov/patch

synapse/common.py#L74

Added line #L74 was not covered by tests
# Fallback to using the vendored IPaddress library
import synapse.vendor.cpython.ipaddress as ipaddress

Check warning on line 76 in synapse/common.py

View check run for this annotation

Codecov / codecov/patch

synapse/common.py#L76

Added line #L76 was not covered by tests

def now():
'''
Get the current epoch time in milliseconds.
Expand Down
1 change: 0 additions & 1 deletion synapse/lib/chop.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import binascii
import ipaddress

import regex

Expand Down
4 changes: 3 additions & 1 deletion synapse/lib/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import struct
import asyncio
import logging
import ipaddress
import contextlib
import collections

Expand All @@ -87,6 +86,9 @@

from synapse.lib.msgpack import deepcopy

# Use the ipaddress module we import from s_common
ipaddress = s_common.ipaddress

logger = logging.getLogger(__name__)

import synapse.lib.msgpack as s_msgpack
Expand Down
3 changes: 2 additions & 1 deletion synapse/lib/scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import idna
import regex
import ipaddress
import unicodedata

import synapse.exc as s_exc
Expand All @@ -21,6 +20,8 @@

import synapse.lib.crypto.coin as s_coin

# Use the ipaddress module we import from s_common
ipaddress = s_common.ipaddress

logger = logging.getLogger(__name__)

Expand Down
5 changes: 3 additions & 2 deletions synapse/lib/stormlib/ipv6.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import logging
import ipaddress


import synapse.exc as s_exc
import synapse.common as s_common

import synapse.lib.stormtypes as s_stormtypes

# Use the ipaddress module we import from s_common
ipaddress = s_common.ipaddress
logger = logging.getLogger(__name__)

@s_stormtypes.registry.registerLib
Expand Down
14 changes: 12 additions & 2 deletions synapse/models/inet.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import socket
import asyncio
import hashlib
import inspect
import logging
import ipaddress
import functools
import email.utils
import urllib.parse

Expand All @@ -21,6 +22,10 @@
import synapse.lookup.iana as s_l_iana

logger = logging.getLogger(__name__)

# Use the ipaddress module we import from s_common
ipaddress = s_common.ipaddress

drivre = regex.compile(r'^\w[:|]')
fqdnre = regex.compile(r'^[\w._-]+$', regex.U)
srv6re = regex.compile(r'^\[([a-f0-9\.:]+)\](?::(\d+))?$', regex.IGNORECASE)
Expand Down Expand Up @@ -803,6 +808,11 @@ def postTypeInit(self):
s_types.Str.postTypeInit(self)
self.setNormFunc(str, self._normPyStr)

self._parseaddr = email.utils.parseaddr
argspec = inspect.getfullargspec(self._parseaddr)
if 'strict' in argspec.kwonlyargs:
self._parseaddr = functools.partial(self._parseaddr, strict=False)

def _normPyStr(self, valu):

# remove quotes for normalized version
Expand All @@ -811,7 +821,7 @@ def _normPyStr(self, valu):
valu = ' '.join(valu.split())

try:
name, addr = email.utils.parseaddr(valu)
name, addr = self._parseaddr(valu)
except Exception as e: # pragma: no cover
# not sure we can ever really trigger this with a string as input
mesg = f'email.utils.parsaddr failed: {str(e)}'
Expand Down
21 changes: 21 additions & 0 deletions synapse/tests/test_model_inet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,15 @@ async def test_rfc2822_addr(self):
self.len(2, await core.nodes('inet:rfc2822:addr^=unittest1'))
self.len(1, await core.nodes('inet:rfc2822:addr^=unittest12'))

# CVE-2023-27043 related behavior
# DISCUSS Do we want to retain the OLD behavior or use the new strict behavior?
nodes = await core.nodes('[inet:rfc2822:addr?="[email protected]]<[email protected]>"]')
self.len(1, nodes)
# This is the python <=3.11.9 behavior or the behavior with strict=False
self.eq(nodes[0].ndef[1], '[email protected]')
# This is the 3.11.10+ behavior or the behavior with strict=True
# self.eq(nodes[0].ndef[1], '[email protected]]<[email protected]>')

async def test_server(self):
formname = 'inet:server'
data = (
Expand Down Expand Up @@ -3364,3 +3373,15 @@ async def test_model_inet_service(self):
:channel -> inet:service:channel
+:name="/r/synapse"
'''))

async def test_model_ipaddress_regression(self):
# TODO DATA MODEL MIGRATION
# TODO IPV6 VARIANT
async with self.getTestCore() as core:
q = '''
init { $l = () }
[inet:ipv4=192.0.0.9 inet:ipv4=192.0.0.0 inet:ipv4=192.0.0.255] $l.append(:type)
fini { return ( $l ) }
'''
resp = await core.callStorm(q)
self.eq(resp, ['unicast', 'private', 'private'])
vEpiphyte marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading