1111from flask import Flask , jsonify
1212from flask import request as flask_request
1313from flask import render_template , abort , Response , make_response
14+ from werkzeug .utils import secure_filename
1415
1516from nettacker import logger
1617from nettacker .api .core import (
4748app = Flask (__name__ , template_folder = str (Config .path .web_static_dir ))
4849app .config .from_object (__name__ )
4950
51+ nettacker_path_config = Config .path
5052nettacker_application_config = Config .settings .as_dict ()
5153nettacker_application_config .update (Config .api .as_dict ())
5254del nettacker_application_config ["api_access_key" ]
@@ -191,6 +193,33 @@ def index():
191193 )
192194
193195
196+ def sanitize_report_path_filename (report_path_filename ):
197+ """
198+ sanitize the report_path_filename
199+
200+ Args:
201+ report_path_filename: the report path filename
202+
203+ Returns:
204+ the sanitized report path filename
205+ """
206+ filename = secure_filename (os .path .basename (report_path_filename ))
207+ if not filename :
208+ return False
209+ # Define a list or tuple of valid extensions
210+ VALID_EXTENSIONS = (".html" , ".htm" , ".txt" , ".json" , ".csv" )
211+ if "." in filename :
212+ if filename .endswith (VALID_EXTENSIONS ):
213+ safe_report_path = nettacker_path_config .results_dir / filename
214+ else :
215+ return False
216+ else :
217+ safe_report_path = nettacker_path_config .results_dir / filename
218+ if not safe_report_path .is_relative_to (nettacker_path_config .results_dir ):
219+ return False
220+ return safe_report_path
221+
222+
194223@app .route ("/new/scan" , methods = ["GET" , "POST" ])
195224def new_scan ():
196225 """
@@ -201,6 +230,11 @@ def new_scan():
201230 """
202231 api_key_is_valid (app , flask_request )
203232 form_values = dict (flask_request .form )
233+ raw_report_path_filename = form_values .get ("report_path_filename" )
234+ report_path_filename = sanitize_report_path_filename (raw_report_path_filename )
235+ if not report_path_filename :
236+ return jsonify (structure (status = "error" , msg = "Invalid report filename" )), 400
237+ form_values ["report_path_filename" ] = str (report_path_filename )
204238 for key in nettacker_application_config :
205239 if key not in form_values :
206240 form_values [key ] = nettacker_application_config [key ]
@@ -273,7 +307,13 @@ def session_set():
273307 """
274308 api_key_is_valid (app , flask_request )
275309 res = make_response (jsonify (structure (status = "ok" , msg = _ ("browser_session_valid" ))))
276- res .set_cookie ("key" , value = app .config ["OWASP_NETTACKER_CONFIG" ]["api_access_key" ])
310+ res .set_cookie (
311+ "key" ,
312+ value = app .config ["OWASP_NETTACKER_CONFIG" ]["api_access_key" ],
313+ httponly = True ,
314+ samesite = "Lax" ,
315+ secure = True ,
316+ )
277317 return res
278318
279319
0 commit comments