From c840e236cb43cfa57d2542a3fc3688807cc90387 Mon Sep 17 00:00:00 2001 From: dmiller Date: Fri, 26 Apr 2024 17:25:40 +0000 Subject: [PATCH] Ensure UTF-8 encoding used throughout zenmap --- zenmap/install_scripts/macosx/make-bundle.sh | 2 +- .../install_scripts/utils/version_update.py | 6 ++--- zenmap/radialnet/core/XMLHandler.py | 2 +- zenmap/zenmapCore/NetworkInventory.py | 4 ++-- zenmap/zenmapCore/NmapParser.py | 8 +++---- zenmap/zenmapCore/Paths.py | 2 +- zenmap/zenmapCore/RecentScans.py | 16 +++++++++----- zenmap/zenmapCore/ScriptMetadata.py | 22 +++++++++++-------- zenmap/zenmapCore/TargetList.py | 16 +++++++++----- zenmap/zenmapCore/UmitConfigParser.py | 2 +- zenmap/zenmapCore/Version.py | 2 +- .../data/locale/xgettext-profile_editor.py | 2 +- 12 files changed, 48 insertions(+), 36 deletions(-) diff --git a/zenmap/install_scripts/macosx/make-bundle.sh b/zenmap/install_scripts/macosx/make-bundle.sh index 37d7b91e1..84ad2f932 100755 --- a/zenmap/install_scripts/macosx/make-bundle.sh +++ b/zenmap/install_scripts/macosx/make-bundle.sh @@ -93,7 +93,7 @@ import sys from string import Template from zenmapCore.Version import * from zenmapCore.Name import * -with open(sys.argv[1],"r") as f: +with open(sys.argv[1],"r",encoding="utf-8") as f: sys.stdout.write(Template(f.read()).substitute( VERSION=VERSION, APP_WEB_SITE=APP_WEB_SITE, diff --git a/zenmap/install_scripts/utils/version_update.py b/zenmap/install_scripts/utils/version_update.py index d39c6ebdd..87f9ef992 100644 --- a/zenmap/install_scripts/utils/version_update.py +++ b/zenmap/install_scripts/utils/version_update.py @@ -73,14 +73,14 @@ NAME_PY = os.path.join("zenmapCore", "Name.py") def update_date(base_dir): name_file = os.path.join(base_dir, NAME_PY) print(">>> Updating %s" % name_file) - nf = open(name_file, "r") + nf = open(name_file, "r", encoding="utf-8") ncontent = nf.read() nf.close() ncontent = re.sub(r'APP_COPYRIGHT *= *"Copyright 2005-....', 'APP_COPYRIGHT = "Copyright 2005-%d' % (datetime.today().year), ncontent) # Write the modified file. - nf = open(name_file, "w") + nf = open(name_file, "w", encoding="utf-8") nf.write(ncontent) nf.close() @@ -88,7 +88,7 @@ def update_date(base_dir): def update_version(base_dir, version): version = re.sub(r'(?=[^0-9.])', '+', version, 1) print(">>> Updating %s" % os.path.join(base_dir, VERSION_PY)) - vf = open(os.path.join(base_dir, VERSION_PY), "w") + vf = open(os.path.join(base_dir, VERSION_PY), "w", encoding="utf-8") print("VERSION = \"%s\"" % version, file=vf) vf.close() diff --git a/zenmap/radialnet/core/XMLHandler.py b/zenmap/radialnet/core/XMLHandler.py index 9cd4a3b6f..8a8941687 100644 --- a/zenmap/radialnet/core/XMLHandler.py +++ b/zenmap/radialnet/core/XMLHandler.py @@ -321,5 +321,5 @@ if __name__ == "__main__": root = reader.get_root() - writer = XMLWriter(open("test.xml", 'w'), root) + writer = XMLWriter(open("test.xml", 'wb'), root) writer.write() diff --git a/zenmap/zenmapCore/NetworkInventory.py b/zenmap/zenmapCore/NetworkInventory.py index 781b4e253..661aa8ec9 100644 --- a/zenmap/zenmapCore/NetworkInventory.py +++ b/zenmap/zenmapCore/NetworkInventory.py @@ -286,7 +286,7 @@ class NetworkInventory(object): """Saves the scan with the given list index into a file with a given path. With format = "xml", saves Nmap XML; otherwise saves plain text output.""" - f = open(path, 'w') + f = open(path, 'wb') if format == "xml": self.get_scans()[index].write_xml(f) self.filenames[self.get_scans()[index]] = f @@ -352,7 +352,7 @@ class NetworkInventory(object): self._generate_filenames(path) for scan, filename in self.filenames.items(): - f = open(os.path.join(path, filename), "w") + f = open(os.path.join(path, filename), "wb") scan.write_xml(f) f.close() diff --git a/zenmap/zenmapCore/NmapParser.py b/zenmap/zenmapCore/NmapParser.py index 8d17dcaef..d4bd4504e 100644 --- a/zenmap/zenmapCore/NmapParser.py +++ b/zenmap/zenmapCore/NmapParser.py @@ -739,7 +739,7 @@ class NmapParserSAX(ParserBasics, ContentHandler): def parse_file(self, filename): """Parse an Nmap XML file from the named file.""" - with open(filename, "r") as f: + with open(filename, "rb") as f: self.parse(f) self.filename = filename @@ -1002,12 +1002,12 @@ class NmapParserSAX(ParserBasics, ContentHandler): f.""" if self.nmap_output == "": return - f.write(self.nmap_output) + f.write(self.nmap_output.encode('utf-8','xmlcharrefreplace')) def write_xml(self, f): """Write the XML representation of this object to the file-like object f.""" - writer = XMLGenerator(f) + writer = XMLGenerator(f, encoding='utf-8') writer.startDocument() if self.xml_stylesheet_data is not None: writer.processingInstruction( @@ -1033,7 +1033,7 @@ class NmapParserSAX(ParserBasics, ContentHandler): def write_xml_to_file(self, filename): """Write the XML representation of this scan to the file whose name is given.""" - fd = open(filename, "w") + fd = open(filename, "wb") self.write_xml(fd) fd.close() diff --git a/zenmap/zenmapCore/Paths.py b/zenmap/zenmapCore/Paths.py index 2cebfed5a..23da3af74 100644 --- a/zenmap/zenmapCore/Paths.py +++ b/zenmap/zenmapCore/Paths.py @@ -199,7 +199,7 @@ def return_if_exists(path, create=False): if os.path.exists(path): return path elif create: - f = open(path, "w") + f = open(path, "wb") f.close() return path raise Exception("File '%s' does not exist or could not be found!" % path) diff --git a/zenmap/zenmapCore/RecentScans.py b/zenmap/zenmapCore/RecentScans.py index 3d4cdc3cd..ee5c9bfda 100644 --- a/zenmap/zenmapCore/RecentScans.py +++ b/zenmap/zenmapCore/RecentScans.py @@ -76,17 +76,21 @@ class RecentScans(object): self.using_file = True # Recovering saved targets - recent_file = open(self.recent_scans_file, "r") - self.temp_list = [ - t for t in recent_file.read().split(";") - if t != "" and t != "\n"] - recent_file.close() + for enc in ('utf-8', None): + try: + with open(self.recent_scans_file, "r", encoding=enc) as recent_file: + self.temp_list = [ + t for t in recent_file.read().split(";") + if t != "" and t != "\n"] + except UnicodeDecodeError: + continue + break else: self.using_file = False def save(self): if self.using_file: - recent_file = open(self.recent_scans_file, "w") + recent_file = open(self.recent_scans_file, "w", encoding="utf-8") recent_file.write(";".join(self.temp_list)) recent_file.close() diff --git a/zenmap/zenmapCore/ScriptMetadata.py b/zenmap/zenmapCore/ScriptMetadata.py index aa1c7cba2..127061d3b 100644 --- a/zenmap/zenmapCore/ScriptMetadata.py +++ b/zenmap/zenmapCore/ScriptMetadata.py @@ -84,7 +84,7 @@ class ScriptDB (object): self.lineno = 1 self.line = "" - with open(script_db_path, "r") as self.f: + with open(script_db_path, "r", encoding="utf-8") as self.f: self.entries_list = self.parse() def syntax_error(self, message): @@ -296,20 +296,20 @@ class ScriptMetadata (object): self.get_string_variable(filename, "author")] filepath = os.path.join(self.scripts_dir, filename) - with open(filepath, "r") as f: + with open(filepath, "r", encoding="utf-8") as f: for tag_name, tag_text in nsedoc_tags_iter(f): if tag_name == "output" and not entry.output: entry.output = tag_text elif tag_name == "usage" and not entry.usage: entry.usage = tag_text - except IOError as e: + except (IOError, UnicodeError) as e: entry.description = "Error getting metadata: {}".format(e) return entry @staticmethod def get_file_contents(filename): - with open(filename, "r") as f: + with open(filename, "r", encoding="utf-8") as f: contents = f.read() return contents @@ -343,7 +343,7 @@ class ScriptMetadata (object): @staticmethod def get_requires(filename): - with open(filename, "r") as f: + with open(filename, "r", encoding="utf-8") as f: requires = ScriptMetadata.get_requires_from_file(f) return requires @@ -359,7 +359,7 @@ class ScriptMetadata (object): @staticmethod def get_script_args(filename): - with open(filename, "r") as f: + with open(filename, "r", encoding="utf-8") as f: args = ScriptMetadata.get_script_args_from_file(f) return args @@ -414,8 +414,11 @@ class ScriptMetadata (object): else: libname = filename - self.library_arguments[libname] = self.get_script_args(filepath) - self.library_requires[libname] = self.get_requires(filepath) + try: + self.library_arguments[libname] = self.get_script_args(filepath) + self.library_requires[libname] = self.get_requires(filepath) + except (IOError, UnicodeError) as e: + log.debug("Unable to process {}: {}".format(libname, e)) def get_script_entries(scripts_dir, nselib_dir): @@ -424,7 +427,8 @@ def get_script_entries(scripts_dir, nselib_dir): metadata = ScriptMetadata(scripts_dir, nselib_dir) try: scriptdb = ScriptDB(os.path.join(scripts_dir, "script.db")) - except IOError: + except (IOError, UnicodeError) as e: + log.debug("Unable to process script.db: {}".format(e)) return [] entries = [] for dbentry in scriptdb.get_entries_list(): diff --git a/zenmap/zenmapCore/TargetList.py b/zenmap/zenmapCore/TargetList.py index 7712871b4..e4d25372b 100644 --- a/zenmap/zenmapCore/TargetList.py +++ b/zenmap/zenmapCore/TargetList.py @@ -77,17 +77,21 @@ class TargetList(object): self.using_file = True # Recovering saved targets - target_file = open(self.target_list_file, "r") - self.temp_list = [ - t for t in target_file.read().split(";") - if t != "" and t != "\n"] - target_file.close() + for enc in ('utf-8', None): + try: + with open(self.target_list_file, "r", encoding=enc) as target_file: + self.temp_list = [ + t for t in target_file.read().split(";") + if t != "" and t != "\n"] + except UnicodeDecodeError: + continue + break else: self.using_file = False def save(self): if self.using_file: - target_file = open(self.target_list_file, "w") + target_file = open(self.target_list_file, "w", encoding="utf-8") target_file.write(";".join(self.temp_list)) target_file.close() diff --git a/zenmap/zenmapCore/UmitConfigParser.py b/zenmap/zenmapCore/UmitConfigParser.py index b035838d9..fd51a4a5a 100644 --- a/zenmap/zenmapCore/UmitConfigParser.py +++ b/zenmap/zenmapCore/UmitConfigParser.py @@ -87,7 +87,7 @@ class UmitConfigParser(ConfigParser): if self.filenames: log.debug("saving to %s" % self.filenames) try: - with open(self.filenames, 'w') as fp: + with open(self.filenames, 'w', encoding="utf-8") as fp: self.write(fp) except Exception as e: self.failed = e diff --git a/zenmap/zenmapCore/Version.py b/zenmap/zenmapCore/Version.py index b3dd8943b..5abeb6d2b 100644 --- a/zenmap/zenmapCore/Version.py +++ b/zenmap/zenmapCore/Version.py @@ -1 +1 @@ -VERSION = "7.95" +VERSION = "7.95+SVN" diff --git a/zenmap/zenmapCore/data/locale/xgettext-profile_editor.py b/zenmap/zenmapCore/data/locale/xgettext-profile_editor.py index 2ce09a69f..66d5aab3e 100755 --- a/zenmap/zenmapCore/data/locale/xgettext-profile_editor.py +++ b/zenmap/zenmapCore/data/locale/xgettext-profile_editor.py @@ -48,7 +48,7 @@ if directory is not None: os.chdir(directory) for fn in filenames: - with open(fn, "r") as f: + with open(fn, "rb") as f: parser = xml.sax.make_parser() parser.setContentHandler(Handler()) parser.parse(f)