Skip to content

Commit

Permalink
Ensure UTF-8 encoding used throughout zenmap
Browse files Browse the repository at this point in the history
  • Loading branch information
bonsaiviking committed Apr 26, 2024
1 parent 480803e commit c840e23
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 36 deletions.
2 changes: 1 addition & 1 deletion zenmap/install_scripts/macosx/make-bundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions zenmap/install_scripts/utils/version_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,22 @@
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()


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()

Expand Down
2 changes: 1 addition & 1 deletion zenmap/radialnet/core/XMLHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,5 @@ def characters(self, text):

root = reader.get_root()

writer = XMLWriter(open("test.xml", 'w'), root)
writer = XMLWriter(open("test.xml", 'wb'), root)
writer.write()
4 changes: 2 additions & 2 deletions zenmap/zenmapCore/NetworkInventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def save_to_file(self, path, index, format="xml"):
"""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
Expand Down Expand Up @@ -352,7 +352,7 @@ def save_to_dir(self, path):
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()

Expand Down
8 changes: 4 additions & 4 deletions zenmap/zenmapCore/NmapParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ def parse(self, f):

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

Expand Down Expand Up @@ -1002,12 +1002,12 @@ def write_text(self, f):
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(
Expand All @@ -1033,7 +1033,7 @@ def get_xml(self):
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()

Expand Down
2 changes: 1 addition & 1 deletion zenmap/zenmapCore/Paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions zenmap/zenmapCore/RecentScans.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,21 @@ def __init__(self):
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()

Expand Down
22 changes: 13 additions & 9 deletions zenmap/zenmapCore/ScriptMetadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __init__(self, script_db_path=None):

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):
Expand Down Expand Up @@ -296,20 +296,20 @@ def get_metadata(self, filename):
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

Expand Down Expand Up @@ -343,7 +343,7 @@ def get_list_variable(self, filename, varname):

@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

Expand All @@ -359,7 +359,7 @@ def get_requires_from_file(f):

@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

Expand Down Expand Up @@ -414,8 +414,11 @@ def construct_library_arguments(self):
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):
Expand All @@ -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():
Expand Down
16 changes: 10 additions & 6 deletions zenmap/zenmapCore/TargetList.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,21 @@ def __init__(self):
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()

Expand Down
2 changes: 1 addition & 1 deletion zenmap/zenmapCore/UmitConfigParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def save_changes(self):
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
Expand Down
2 changes: 1 addition & 1 deletion zenmap/zenmapCore/Version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "7.95"
VERSION = "7.95+SVN"
2 changes: 1 addition & 1 deletion zenmap/zenmapCore/data/locale/xgettext-profile_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def startElement(self, name, attrs):
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)
Expand Down

0 comments on commit c840e23

Please sign in to comment.