Skip to content

Commit

Permalink
protection against non-ASCII in args
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaeno committed Jul 10, 2023
1 parent f18de5d commit 758fb25
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 11 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
** Release Notes

1.5.62
* protection against non-ASCII args

1.5.61
* fixed changes in 1.5.60

Expand Down
6 changes: 4 additions & 2 deletions pandaclient/Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1997,7 +1997,8 @@ def get_events_status(ids, verbose=False):
def update_events(events, verbose=False):
"""Update events
args:
events: a list of {'eventRangeID': ..., 'eventStatus': ..., 'errorCode': <optional>}
events: a list of {'eventRangeID': ..., 'eventStatus': ...,
'errorCode': <optional>, 'errorDiag': <optional, < 500chars>}
verbose: True to see verbose message
returns:
status code
Expand All @@ -2012,7 +2013,8 @@ def update_events(events, verbose=False):
curl.verbose = verbose
# execute
url = baseURLSSL + '/updateEventRanges'
data = {'eventRanges': json.dumps(events)}
data = {'eventRanges': json.dumps(events),
'version': 2}
status, output = curl.post(url, data, is_json=True)
if status != 0:
return EC_Failed, output
Expand Down
23 changes: 23 additions & 0 deletions pandaclient/Group_argparse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import argparse
import sys
from collections import OrderedDict
try:
unicode
except Exception:
unicode = str


# check string args if they can be encoded with utf-8
class ActionWithUnicodeCheck(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
if isinstance(values, (unicode, str)):
try:
values.encode()
except Exception:
parser.exit(1, message="ERROR: argument --{0} cannot be encoded with utf-8: '{1}'\n".format(self.dest, values))
setattr(namespace, self.dest, values)


class GroupArgParser(argparse.ArgumentParser):
def __init__(self, usage, conflict_handler):
Expand Down Expand Up @@ -86,3 +102,10 @@ def __call__(self, parser, namespace, values, option_string=None):
else:
raise Exception("!!!ERROR!!! Unknown group name=%s" % values)
sys.exit(0)


# factory method
def get_parser(usage, conflict_handler):
p = GroupArgParser(usage, conflict_handler)
p.register('action', 'store', ActionWithUnicodeCheck)
return p
2 changes: 1 addition & 1 deletion pandaclient/PandaToolsPkgInfo.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
release_version = "1.5.61"
release_version = "1.5.62"
4 changes: 2 additions & 2 deletions pandaclient/PathenaScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import shutil
import atexit
import argparse
from pandaclient.Group_argparse import GroupArgParser
from pandaclient.Group_argparse import get_parser
import random
import pickle
import json
Expand Down Expand Up @@ -114,7 +114,7 @@
"-l"
]

optP = GroupArgParser(usage=usage, conflict_handler="resolve")
optP = get_parser(usage=usage, conflict_handler="resolve")
optP.set_examples(examples)

# define option groups
Expand Down
4 changes: 2 additions & 2 deletions pandaclient/PchainScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import shlex
import atexit

from pandaclient.Group_argparse import GroupArgParser
from pandaclient.Group_argparse import get_parser
from pandaclient import PLogger
from pandaclient import PandaToolsPkgInfo
from pandaclient import MiscUtils
Expand Down Expand Up @@ -33,7 +33,7 @@ def main():
usage = """pchain [options]
"""

optP = GroupArgParser(usage=usage, conflict_handler="resolve")
optP = get_parser(usage=usage, conflict_handler="resolve")

group_output = optP.add_group('output', 'output dataset/files')
group_config = optP.add_group('config', 'workflow configuration')
Expand Down
4 changes: 2 additions & 2 deletions pandaclient/PhpoScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import copy
import atexit

from pandaclient.Group_argparse import GroupArgParser
from pandaclient.Group_argparse import get_parser
from pandaclient import PLogger
from pandaclient import PandaToolsPkgInfo
from pandaclient import MiscUtils
Expand Down Expand Up @@ -33,7 +33,7 @@ def main(get_taskparams=False, ext_args=None, dry_mode=False):
usage = """phpo [options]
"""

optP = GroupArgParser(usage=usage, conflict_handler="resolve")
optP = get_parser(usage=usage, conflict_handler="resolve")

group_input = optP.add_group('input', 'input dataset(s)/files/format')
group_output = optP.add_group('output', 'output dataset/files')
Expand Down
4 changes: 2 additions & 2 deletions pandaclient/PrunScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import argparse
import time

from pandaclient.Group_argparse import GroupArgParser
from pandaclient.Group_argparse import get_parser
try:
from urllib import quote
except ImportError:
Expand Down Expand Up @@ -83,7 +83,7 @@ def main(get_taskparams=False, ext_args=None, dry_mode=False):
"--useSiteGroup"
]

optP = GroupArgParser(usage=usage, conflict_handler="resolve")
optP = get_parser(usage=usage, conflict_handler="resolve")
optP.set_examples(examples)

# command-line parameters
Expand Down

0 comments on commit 758fb25

Please sign in to comment.