FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-40121: Fixes audit events raised on creating a new socket by zooba · Pull Request #19238 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (2) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
22 changes: 22 additions & 0 deletions Lib/test/audit-tests.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,28 @@ def hook(event, args):
CloseKey(kv)


def test_socket():
import socket

def hook(event, args):
if event.startswith("socket."):
print(event, *args)

sys.addaudithook(hook)

socket.gethostname()

# Don't care if this fails, we just want the audit message
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Don't care if this fails, we just want the audit message
sock.bind(('127.0.0.1', 8080))
except error:
pass
finally:
sock.close()


if __name__ == "__main__":
from test.libregrtest.setup import suppress_msvcrt_asserts

Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_audit.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ def test_winreg(self):
self.assertSequenceEqual(["winreg.EnumKey", " ", f"{expected} 10000"], events[3])
self.assertSequenceEqual(["winreg.PyHKEY.Detach", " ", expected], events[4])

def test_socket(self):
support.import_module("socket")
returncode, events, stderr = self.run_python("test_socket")
if returncode:
self.fail(stderr)

if support.verbose:
print(*events, sep='\n')
self.assertEqual(events[0][0], "socket.gethostname")
self.assertEqual(events[1][0], "socket.__new__")
self.assertEqual(events[2][0], "socket.bind")
self.assertTrue(events[2][2].endswith("('127.0.0.1', 8080)"))

if __name__ == "__main__":
unittest.main()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes audit events raised on creating a new socket.
7 changes: 4 additions & 3 deletions Modules/socketmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5099,7 +5099,7 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)

#ifdef MS_WINDOWS
/* In this case, we don't use the family, type and proto args */
if (fdobj != NULL && fdobj != Py_None)
if (fdobj == NULL || fdobj == Py_None)
#endif
{
if (PySys_Audit("socket.__new__", "Oiii",
Expand All @@ -5121,8 +5121,9 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
}
memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info));

if (PySys_Audit("socket()", "iii", info.iAddressFamily,
info.iSocketType, info.iProtocol) < 0) {
if (PySys_Audit("socket.__new__", "Oiii", s,
info.iAddressFamily, info.iSocketType,
info.iProtocol) < 0) {
return -1;
}

Expand Down

Back | FazBrowse Home | New Git URL