Skip to content

Commit fba25be

Browse files
Marek DenisMarek Denis
authored andcommitted
Prevent sigve from leaking file descriptors.
After using open() function it is important to always close the file handler. There are 3 spots in sigver.py where files were opened and never closed. Following commit introduces new function 'read_file' that utilized 'with' keyword, hence ensuring the file will always be closed.
1 parent 4e90463 commit fba25be

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/saml2/sigver.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ class CertificateError(SigverError):
106106
pass
107107

108108

109+
def read_file(*args, **kwargs):
110+
with open(*args, **kwargs) as handler:
111+
return handler.read()
112+
113+
109114
def rm_xmltag(statement):
110115
try:
111116
_t = statement.startswith(XMLTAG)
@@ -540,7 +545,7 @@ def pem_format(key):
540545

541546

542547
def import_rsa_key_from_file(filename):
543-
return RSA.importKey(open(filename, 'r').read())
548+
return RSA.importKey(read_file(filename, 'r'))
544549

545550

546551
def parse_xmlsec_output(output):
@@ -648,11 +653,13 @@ def read_cert_from_file(cert_file, cert_type):
648653
:param cert_type: The certificate type
649654
:return: A base64 encoded certificate as a string or the empty string
650655
"""
656+
657+
651658
if not cert_file:
652659
return ""
653660

654661
if cert_type == "pem":
655-
line = open(cert_file).read().split("\n")
662+
line = read_file(cert_file).split("\n")
656663
if line[0] == "-----BEGIN CERTIFICATE-----":
657664
line = line[1:]
658665
elif line[0] == "-----BEGIN PUBLIC KEY-----":
@@ -672,7 +679,7 @@ def read_cert_from_file(cert_file, cert_type):
672679
return "".join(line)
673680

674681
if cert_type in ["der", "cer", "crt"]:
675-
data = open(cert_file).read()
682+
data = read_file(cert_file)
676683
return base64.b64encode(str(data))
677684

678685

0 commit comments

Comments
 (0)