Mercurial > kallithea
changeset 7646:7aff9a999536 stable
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('<span class="foo">Foo!</span>'), 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.
author | Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> |
---|---|
date | Mon, 29 Apr 2019 21:33:45 +0200 |
parents | e0626084e9c5 |
children | 4babc6e047d0 |
files | kallithea/controllers/admin/repos.py kallithea/controllers/admin/settings.py kallithea/controllers/admin/user_groups.py kallithea/templates/admin/gists/edit.html kallithea/templates/admin/permissions/permissions_globals.html kallithea/templates/admin/users/user_edit_ips.html kallithea/templates/base/default_perms_box.html |
diffstat | 7 files changed, 14 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/kallithea/controllers/admin/repos.py Thu Apr 18 21:33:23 2019 +0200 +++ b/kallithea/controllers/admin/repos.py Mon Apr 29 21:33:45 2019 +0200 @@ -201,10 +201,10 @@ 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}
--- a/kallithea/controllers/admin/settings.py Thu Apr 18 21:33:23 2019 +0200 +++ b/kallithea/controllers/admin/settings.py Mon Apr 29 21:33:45 2019 +0200 @@ -171,14 +171,14 @@ 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')
--- a/kallithea/controllers/admin/user_groups.py Thu Apr 18 21:33:23 2019 +0200 +++ b/kallithea/controllers/admin/user_groups.py Mon Apr 29 21:33:45 2019 +0200 @@ -140,7 +140,7 @@ 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:
--- a/kallithea/templates/admin/gists/edit.html Thu Apr 18 21:33:23 2019 +0200 +++ b/kallithea/templates/admin/gists/edit.html Mon Apr 29 21:33:45 2019 +0200 @@ -32,7 +32,7 @@ <div id="edit_error" style="display: none" class="flash_msg"> <div class="alert alert-dismissable alert-warning"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true"><i class="icon-cancel-circled"></i></button> - ${h.literal(_('Gist was updated since you started editing. Copy your changes and click %(here)s to reload new version.') + ${(h.HTML(_('Gist was updated since you started editing. Copy your changes and click %(here)s to reload new version.')) % {'here': h.link_to(_('here'),h.url('edit_gist', gist_id=c.gist.gist_access_id))})} </div> <script>
--- a/kallithea/templates/admin/permissions/permissions_globals.html Thu Apr 18 21:33:23 2019 +0200 +++ b/kallithea/templates/admin/permissions/permissions_globals.html Mon Apr 29 21:33:45 2019 +0200 @@ -7,7 +7,7 @@ ${h.checkbox('anonymous',True)} <span>${_('Allow anonymous access')}</span> </label> - <span class="help-block">${h.literal(_('Allow access to Kallithea without needing to log in. Anonymous users use %s user permissions.') % (h.link_to('*default*',h.url('admin_permissions_perms'))))}</span> + <span class="help-block">${h.HTML(_('Allow access to Kallithea without needing to log in. Anonymous users use %s user permissions.')) % (h.link_to('*default*',h.url('admin_permissions_perms')))}</span> </div> </div> <div class="form-group">
--- a/kallithea/templates/admin/users/user_edit_ips.html Thu Apr 18 21:33:23 2019 +0200 +++ b/kallithea/templates/admin/users/user_edit_ips.html Mon Apr 29 21:33:45 2019 +0200 @@ -4,7 +4,7 @@ <tr> <td><div class="ip">${ip.ip_addr}</div></td> <td><div class="ip">${h.ip_range(ip.ip_addr)}</div></td> - <td>${h.literal(_('Inherited from %s') % h.link_to('*default*',h.url('admin_permissions_ips')))}</td> + <td>${h.HTML(_('Inherited from %s')) % h.link_to('*default*',h.url('admin_permissions_ips'))}</td> </tr> %endfor %endif
--- a/kallithea/templates/base/default_perms_box.html Thu Apr 18 21:33:23 2019 +0200 +++ b/kallithea/templates/base/default_perms_box.html Mon Apr 29 21:33:45 2019 +0200 @@ -12,8 +12,8 @@ <div> ${h.checkbox('inherit_default_permissions',value=True)} <span class="help-block"> - ${h.literal(_('Select to inherit global settings, IP whitelist and permissions from the %s.') - % h.link_to(_('default permissions'), url('admin_permissions')))} + ${(h.HTML(_('Select to inherit global settings, IP whitelist and permissions from the %s.')) + % h.link_to(_('default permissions'), url('admin_permissions')))} </span> </div> </div>