11import json
22import mimetypes
33import os
4- import subprocess
4+ import subprocess # nosec
55from datetime import datetime
66from tempfile import TemporaryDirectory
77from typing import Any , Dict , List , Optional
@@ -42,9 +42,9 @@ def c2patool_inject(
4242 env = env_vars ,
4343 check = True ,
4444 stderr = subprocess .PIPE ,
45- )
45+ ) # nosec
4646 except subprocess .CalledProcessError as e :
47- raise UnknownError (e .stderr )
47+ raise UnknownError (e .stderr ) from e
4848
4949
5050def create_c2pa_manifest (
@@ -219,7 +219,7 @@ def inject_file(
219219 with TemporaryDirectory () as temp_dir :
220220 if thumbnail_url :
221221 thumbnail_file_path = os .path .join (temp_dir , 'thumbnail.jpg' )
222- response = requests .get (thumbnail_url , stream = True )
222+ response = requests .get (thumbnail_url , stream = True , timeout = 120 )
223223 response .raise_for_status ()
224224 with open (thumbnail_file_path , 'wb' ) as thumbnail_file :
225225 for chunk in response .iter_content (chunk_size = 8192 ):
@@ -232,7 +232,7 @@ def inject_file(
232232
233233 # Save the manifest to a temporary file
234234 manifest_file_path = os .path .join (temp_dir , 'manifest.json' )
235- with open (manifest_file_path , 'w' ) as manifest_file :
235+ with open (manifest_file_path , 'w' , ) as manifest_file :
236236 json .dump (manifest , manifest_file )
237237 manifest_file .flush ()
238238
@@ -254,25 +254,25 @@ def read_c2pa(asset_c2pa_bytes: bytes, asset_mime_type: str):
254254 f .write (asset_c2pa_bytes )
255255
256256 command = ['c2patool' , asset_c2pa_file ]
257- process = subprocess .run (command , text = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE , check = False )
257+ process = subprocess .run (
258+ command , text = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE , check = False
259+ ) # nosec
258260 if process .returncode != 0 :
259261 if 'No claim found' in process .stderr :
260262 raise NoClaimFound
261- else :
262- raise UnknownError (process .stderr )
263+ raise UnknownError (process .stderr )
263264
264265 json_output = json .loads (process .stdout )
265266 return json_output
266267
267268
268269def read_c2pa_file (c2pa_file : str ):
269270 command = ['c2patool' , c2pa_file ]
270- process = subprocess .run (command , text = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE , check = False )
271+ process = subprocess .run (command , text = True , stdout = subprocess .PIPE , stderr = subprocess .PIPE , check = False ) # nosec
271272 if process .returncode != 0 :
272273 if 'No claim found' in process .stderr :
273274 raise NoClaimFound
274- else :
275- raise UnknownError (process .stderr )
275+ raise UnknownError (process .stderr )
276276
277277 json_output = json .loads (process .stdout )
278278 return json_output
0 commit comments