Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[conf] Add new coerce_list configuration type #4029

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions desktop/core/src/desktop/lib/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

import os
import re
import ast
import sys
import json
import logging
Expand Down Expand Up @@ -230,6 +231,10 @@ def __init__(self, key=_ANONYMOUS, default=None, dynamic_default=None, required=
LOG.warning("%s is of type bool. Resetting it as type 'coerce_bool'." " Please fix it permanently" % (key,))
type = coerce_bool

if type is list:
LOG.warning("%s is of type list. Resetting it as type 'coerce_list'." " Please fix it permanently" % (key,))
type = coerce_list

self.key = key
self.default_value = default
self.dynamic_default = dynamic_default
Expand Down Expand Up @@ -661,6 +666,19 @@ def coerce_csv(value):
raise Exception("Could not coerce %r to csv array." % value)


def coerce_list(value) -> list:
if isinstance(value, str):
try:
result = ast.literal_eval(value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sreenaths
There is still risk of memory exhaustion. What is the size of input expected? Limiting the size?
Take a look this: https://stackoverflow.com/questions/4710247/python-3-are-there-any-known-security-holes-in-ast-literal-evalnode-or-string

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had similar thought, but as this is an admin configuration felt its fine. Will see if there are any safer way to implement this.

if isinstance(result, list):
return result
except (SyntaxError, ValueError):
return [value]
elif isinstance(value, list):
return value
raise Exception("Could not coerce %r to list." % value)


def coerce_json_dict(value):
if isinstance(value, string_types):
return json.loads(value)
Expand Down
17 changes: 17 additions & 0 deletions desktop/core/src/desktop/lib/conf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import configobj

from desktop.lib.conf import *
from desktop.lib.conf import coerce_bool, coerce_list


def my_dynamic_default():
Expand Down Expand Up @@ -187,6 +188,22 @@ def test_coerce_bool(self):
with pytest.raises(Exception):
coerce_bool(tuple("foo"))

def test_coerce_list(self):
assert coerce_list("[]") == []
assert coerce_list("Test val") == ["Test val"]
assert coerce_list("['Test val 1', 'Test val 2']") == ["Test val 1", "Test val 2"]
assert coerce_list("['Test val 1', 'Test val 2, 3']") == ["Test val 1", "Test val 2, 3"]
assert coerce_list("['Test val 1', 'Test val \\'2\\'']") == ["Test val 1", "Test val '2'"]

assert coerce_list(['Test val 1', 'Test val 2']) == ["Test val 1", "Test val 2"]

with pytest.raises(Exception):
coerce_list(tuple("foo"))
with pytest.raises(Exception):
coerce_list(1)
with pytest.raises(Exception):
coerce_list("{1:1, 2:2}")

def test_print_help(self):
out = string_io()
self.conf.print_help(out=out, skip_header=True)
Expand Down
Loading