[PATCH 0/3] Issue #727 - Improve repo url check

This patch aims to improve the repo url check. It expands variables used to compose the url and handles url redirection if the server returns that. Jose Ricardo Ziviani (3): Implement a method to expand variables in a url Expands all possible variables in a repo url to validate it Handles http redirection when checking url path src/kimchi/repositories.py | 16 ++++++++-------- src/kimchi/utils.py | 16 +++++++++++++--- src/kimchi/yumparser.py | 24 ++++++++++++++++++------ 3 files changed, 39 insertions(+), 17 deletions(-) -- 1.9.1

- creates a generic function to expand variables from a given string and makes specialized methods to get the name to display and the repo url. Signed-off-by: Jose Ricardo Ziviani <joserz@linux.vnet.ibm.com> --- src/kimchi/yumparser.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/kimchi/yumparser.py b/src/kimchi/yumparser.py index 0c9de77..a481ac2 100644 --- a/src/kimchi/yumparser.py +++ b/src/kimchi/yumparser.py @@ -249,21 +249,33 @@ def _get_all_yum_vars(): return variables -def get_display_name(name): - if not name: - return '' - +def _expand_variables(stringvar, split_char=' '): yum_variables = _get_all_yum_vars() yum_variables['releasever'] = _get_releasever() yum_variables['basearch'] = _get_basearch() - name_vars = [var for var in name.split() + name_vars = [var for var in stringvar.split(split_char) if var.startswith('$') and var.strip('$') in yum_variables] return reduce(lambda nm, var: nm.replace(var, yum_variables[var.strip('$')]), name_vars, - name) + stringvar) + + +def get_display_name(name): + if not name or '$' not in name: + return name + + return _expand_variables(name) + + +def get_expanded_url(url): + url_path = url.split('://') + if len(url_path) != 2 or '$' not in url: + return url + + return _expand_variables(url, '/') class YumUpdatePackageObject(object): -- 1.9.1

- the url will be validated even if it has variables in it. Signed-off-by: Jose Ricardo Ziviani <joserz@linux.vnet.ibm.com> --- src/kimchi/repositories.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/kimchi/repositories.py b/src/kimchi/repositories.py index 8f99a88..7689151 100644 --- a/src/kimchi/repositories.py +++ b/src/kimchi/repositories.py @@ -30,7 +30,7 @@ from kimchi.exception import InvalidOperation, InvalidParameter from kimchi.exception import OperationFailed, NotFoundError, MissingParameter from kimchi.utils import validate_repo_url from kimchi.yumparser import get_yum_repositories, write_repo_to_file -from kimchi.yumparser import get_display_name +from kimchi.yumparser import get_display_name, get_expanded_url class Repositories(object): @@ -178,13 +178,13 @@ class YumRepo(object): raise MissingParameter("KCHREPOS0013E") if baseurl: - validate_repo_url(baseurl) + validate_repo_url(get_expanded_url(baseurl)) if mirrorlist: - validate_repo_url(mirrorlist) + validate_repo_url(get_expanded_url(mirrorlist)) if metalink: - validate_repo_url(metalink) + validate_repo_url(get_expanded_url(metalink)) if mirrorlist and metalink: raise InvalidOperation('KCHREPOS0030E') @@ -278,15 +278,15 @@ class YumRepo(object): raise MissingParameter("KCHREPOS0013E") if baseurl is not None: - validate_repo_url(baseurl) + validate_repo_url(get_expanded_url(baseurl)) entry.baseurl = baseurl if mirrorlist is not None: - validate_repo_url(mirrorlist) + validate_repo_url(get_expanded_url(mirrorlist)) entry.mirrorlist = mirrorlist if metalink is not None: - validate_repo_url(metalink) + validate_repo_url(get_expanded_url(metalink)) entry.metalink = metalink if mirrorlist and metalink: @@ -437,7 +437,7 @@ class AptRepo(object): dist = config['dist'] comps = config.get('comps', []) - validate_repo_url(uri) + validate_repo_url(get_expanded_url(uri)) kimchiLock.acquire() try: -- 1.9.1

Signed-off-by: Jose Ricardo Ziviani <joserz@linux.vnet.ibm.com> --- src/kimchi/utils.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/kimchi/utils.py b/src/kimchi/utils.py index baf2d62..0906903 100644 --- a/src/kimchi/utils.py +++ b/src/kimchi/utils.py @@ -40,6 +40,7 @@ from kimchi.config import paths, PluginPaths from kimchi.exception import InvalidParameter, TimeoutExpired +MAX_REDIRECTION_ALLOWED = 5 kimchi_log = cherrypy.log.error_log task_id = 0 @@ -140,7 +141,9 @@ def import_module(module_name): return __import__(module_name, globals(), locals(), ['']) -def check_url_path(path): +def check_url_path(path, redirected=0): + if redirected > MAX_REDIRECTION_ALLOWED: + return False try: code = '' parse_result = urlparse(path) @@ -156,9 +159,16 @@ def check_url_path(path): conn = HTTPConnection(server_name, timeout=15) # Don't try to get the whole file: conn.request('HEAD', path) - code = conn.getresponse().status + response = conn.getresponse() + code = response.status conn.close() - if code != 200: + if code == 200: + return True + elif code == 301 or code == 302: + for header in response.getheaders(): + if header[0] == 'location': + return check_url_path(header[1], redirected+1) + else: return False except (urllib2.URLError, HTTPException, IOError, ValueError): return False -- 1.9.1
participants (2)
-
Aline Manera
-
Jose Ricardo Ziviani