From b3b6061086138ade2ee6c9ef416184776bc3a55a Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Sun, 12 Jan 2020 18:55:28 -0500 Subject: [PATCH] #10 better error handling for OS errors --- jetforce.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/jetforce.py b/jetforce.py index b80165f..12e01b1 100755 --- a/jetforce.py +++ b/jetforce.py @@ -253,6 +253,14 @@ class StaticDirectoryApplication(JetforceApplication): filesystem_path = self.root / filename + try: + if not os.access(filesystem_path, os.R_OK): + # File not readable + return Response(Status.NOT_FOUND, "Not Found") + except OSError: + # Filename too large, etc. + return Response(Status.NOT_FOUND, "Not Found") + if filesystem_path.is_file(): is_cgi = str(filename).startswith(self.cgi_directory) is_exe = os.access(filesystem_path, os.X_OK) @@ -432,8 +440,8 @@ class GeminiRequestHandler: app = self.app(environ, self.write_status) for data in app: await self.write_body(data) - except Exception as e: - self.write_status(Status.CGI_ERROR, str(e)) + except Exception: + self.write_status(Status.CGI_ERROR, "An unexpected error occurred") raise finally: await self.close_connection()