7070)
7171
7272
73+ class _MemfdCreationError (OSError ):
74+ """Raised when Linux in-memory virtual file creation (memfd) fails."""
75+
76+ pass
77+
78+
7379@contextlib .contextmanager
7480def secure_cert_key_paths (
7581 cert : Union [bytes , str , None ],
@@ -114,13 +120,8 @@ def secure_cert_key_paths(
114120 # the bytes to anonymous in-memory files using memfd_create. This yields
115121 # /proc/self/fd/... paths, keeping the private key entirely in memory.
116122 if sys .platform == "linux" and hasattr (os , "memfd_create" ):
117- cm = _memfd_cert_key_paths (cert_bytes , key_bytes )
118123 try :
119- cert_path , key_path = cm .__enter__ ()
120- except OSError :
121- pass # Fallback to Tier 3 on failure.
122- else :
123- try :
124+ with _memfd_cert_key_paths (cert_bytes , key_bytes ) as (cert_path , key_path ):
124125 # Handle cases where path exists but might be restricted.
125126 if (cert_path is None or os .path .exists (cert_path )) and (
126127 key_path is None or os .path .exists (key_path )
@@ -129,9 +130,8 @@ def secure_cert_key_paths(
129130 str , key_path or key
130131 ), passphrase
131132 return
132- finally :
133- cm .__exit__ (* sys .exc_info ())
134- # If verification failed, fall through to Tier 3.
133+ except _MemfdCreationError :
134+ pass # Fallback to Tier 3 on failure.
135135
136136 # Tier 3: Fallback Encrypted Temp Files. If in-memory files are not supported
137137 # (macOS/Windows), we write to disk. To protect the key, we encrypt plaintext
@@ -213,20 +213,24 @@ def _memfd_cert_key_paths(
213213 the active descriptors (e.g., '/proc/self/fd/3').
214214 """
215215 cleanup_fds = []
216- print ("--- in memfd_cert_key_paths" )
217216 paths = []
218217
219218 try :
220- for data , name in [(cert_bytes , "mtls_cert" ), (key_bytes , "mtls_key" )]:
221- if data is not None :
222- # MFD_CLOEXEC prevents FD leaks to spawned subprocesses.
223- fd = os .memfd_create (name , os .MFD_CLOEXEC ) # type: ignore[attr-defined]
224- cleanup_fds .append (fd )
225- with os .fdopen (fd , "wb" , closefd = False ) as f :
226- f .write (data )
227- paths .append (f"/proc/self/fd/{ fd } " )
228- else :
229- paths .append (None )
219+ try :
220+ for data , name in [(cert_bytes , "mtls_cert" ), (key_bytes , "mtls_key" )]:
221+ if data is not None :
222+ # MFD_CLOEXEC prevents FD leaks to spawned subprocesses.
223+ fd = os .memfd_create (name , os .MFD_CLOEXEC ) # type: ignore[attr-defined]
224+ cleanup_fds .append (fd )
225+ with os .fdopen (fd , "wb" , closefd = False ) as f :
226+ f .write (data )
227+ paths .append (f"/proc/self/fd/{ fd } " )
228+ else :
229+ paths .append (None )
230+ except OSError as exc :
231+ raise _MemfdCreationError (
232+ "Failed to create in-memory virtual files"
233+ ) from exc
230234
231235 cert_path , key_path = paths
232236 yield cert_path , key_path
@@ -258,7 +262,6 @@ def _tempfile_cert_key_paths(
258262 else None
259263 )
260264 cleanup_files = []
261- print ("--- in _tempfile_cert_key_paths" )
262265 new_passphrase = passphrase
263266 cert_data = cert_bytes
264267 key_data = None
0 commit comments