# HG changeset patch # User Thomas De Schampheleire # Date 2019-04-29 21:33:45 # Node ID 7aff9a999536632b9e112d69a0e3b22d2c82e36e # Parent e0626084e9c575024ed7d93ddd5e562eb3741bd0 templates, controllers: replace webhelpers.html.literal() with webhelpers.html.HTML() where possible Usage of webhelpers.literal (h.literal) can be a problem when variables are not correctly escaped. Luckily, this function can be avoided in several cases. Several users of the construct: h.literal(_('..A..') % (..B..)) can be simplified if (..B..) just contains a call to h.link_to. In this case, there is actually no need to use h.literal, because the object returned by link_to is already a literal. It is sufficient to use webhelpers.html.HTML() like so: h.HTML(_('..A..')) % (..B..) which is better because it will escape the '..A..' part instead of passing it literally. The need to wrap the '..A..' part in HTML() is to make sure the (escaped) end result is not a plain string but a 'literal' to avoid double escaping later. See also the documentation: https://docs.pylonsproject.org/projects/webhelpers/en/latest/modules/html/builder.html " When literal is used in a mixed expression containing both literals and ordinary strings, it tries hard to escape the strings and return a literal. However, this depends on which value has “control” of the expression. literal seems to be able to take control with all combinations of the + operator, but with % and join it must be on the left side of the expression. So these all work: "A" + literal("B") literal(", ").join(["A", literal("B")]) literal("%s %s") % (16, literal("kg")) But these return an ordinary string which is prone to double-escaping later: "\n".join([literal('Foo!'), literal('Bar!')]) "%s %s" % (literal("16"), literal("<em>kg</em>")) " This same escaping with 'HTML()' was already done by default in mako templates for constructs like ${_("something")} that do not contain format specifiers. When the translated string _does_ contain format specifiers, we want to use the same escaping, but we have to do it explicit and earlier so the escaping happens already when strings are inserted into the template string. diff --git a/kallithea/controllers/admin/repos.py b/kallithea/controllers/admin/repos.py --- a/kallithea/controllers/admin/repos.py +++ b/kallithea/controllers/admin/repos.py @@ -201,10 +201,10 @@ class ReposController(BaseRepoController fork = repo.fork if fork is not None: fork_name = fork.repo_name - h.flash(h.literal(_('Forked repository %s as %s') - % (fork_name, repo_url)), category='success') + h.flash(h.HTML(_('Forked repository %s as %s')) + % (fork_name, repo_url), category='success') else: - h.flash(h.literal(_('Created repository %s') % repo_url), + h.flash(h.HTML(_('Created repository %s')) % repo_url, category='success') return {'result': True} return {'result': False} diff --git a/kallithea/controllers/admin/settings.py b/kallithea/controllers/admin/settings.py --- a/kallithea/controllers/admin/settings.py +++ b/kallithea/controllers/admin/settings.py @@ -171,14 +171,14 @@ class SettingsController(BaseController) install_git_hooks=install_git_hooks, user=request.authuser.username, overwrite_git_hooks=overwrite_git_hooks) - added_msg = ', '.join( + added_msg = h.HTML(', ').join( h.link_to(safe_unicode(repo_name), h.url('summary_home', repo_name=repo_name)) for repo_name in added ) or '-' - removed_msg = ', '.join( - h.escape(safe_unicode(repo_name)) for repo_name in removed + removed_msg = h.HTML(', ').join( + safe_unicode(repo_name) for repo_name in removed ) or '-' - h.flash(h.literal(_('Repositories successfully rescanned. Added: %s. Removed: %s.') % - (added_msg, removed_msg)), category='success') + h.flash(h.HTML(_('Repositories successfully rescanned. Added: %s. Removed: %s.')) % + (added_msg, removed_msg), category='success') if invalidate_cache: log.debug('invalidating all repositories cache') diff --git a/kallithea/controllers/admin/user_groups.py b/kallithea/controllers/admin/user_groups.py --- a/kallithea/controllers/admin/user_groups.py +++ b/kallithea/controllers/admin/user_groups.py @@ -140,7 +140,7 @@ class UserGroupsController(BaseControlle action_logger(request.authuser, 'admin_created_users_group:%s' % gr, None, request.ip_addr) - h.flash(h.literal(_('Created user group %s') % h.link_to(h.escape(gr), url('edit_users_group', id=ug.users_group_id))), + h.flash(h.HTML(_('Created user group %s')) % h.link_to(gr, url('edit_users_group', id=ug.users_group_id)), category='success') Session().commit() except formencode.Invalid as errors: diff --git a/kallithea/templates/admin/gists/edit.html b/kallithea/templates/admin/gists/edit.html --- a/kallithea/templates/admin/gists/edit.html +++ b/kallithea/templates/admin/gists/edit.html @@ -32,7 +32,7 @@