changeset 37139:aa55c5354b8f

alias: reject non-ascii characters in user help/doc strings Since command doc/help texts are passed to i18n.gettext(), they must be ASCII. Otherwise, UnicodeError would be raised.
author Yuya Nishihara <yuya@tcha.org>
date Wed, 28 Mar 2018 22:04:45 +0900
parents 4d63f3bc1e1a
children 1d56c539794e
files mercurial/dispatch.py tests/test-alias.t
diffstat 2 files changed, 33 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/dispatch.py	Mon Mar 26 22:18:36 2018 -0400
+++ b/mercurial/dispatch.py	Wed Mar 28 22:04:45 2018 +0900
@@ -534,12 +534,24 @@
                              % (self.name, cmd))
 
     def _populatehelp(self, ui, name, cmd, fn, defaulthelp=None):
-        self.help = ui.config('alias', '%s:help' % name, defaulthelp or '')
+        # confine strings to be passed to i18n.gettext()
+        cfg = {}
+        for k in ('doc', 'help'):
+            v = ui.config('alias', '%s:%s' % (name, k), None)
+            if v is None:
+                continue
+            if not encoding.isasciistr(v):
+                self.badalias = (_("non-ASCII character in alias definition "
+                                   "'%s:%s'") % (name, k))
+                return
+            cfg[k] = v
+
+        self.help = cfg.get('help', defaulthelp or '')
         if self.help and self.help.startswith("hg " + cmd):
             # drop prefix in old-style help lines so hg shows the alias
             self.help = self.help[4 + len(cmd):]
 
-        self.__doc__ = ui.config('alias', '%s:doc' % name, fn.__doc__)
+        self.__doc__ = cfg.get('doc', fn.__doc__)
 
     @property
     def args(self):
--- a/tests/test-alias.t	Mon Mar 26 22:18:36 2018 -0400
+++ b/tests/test-alias.t	Wed Mar 28 22:04:45 2018 +0900
@@ -616,6 +616,25 @@
   
   (some details hidden, use --verbose to show complete help)
 
+invalid character in user-specified help
+
+  >>> with open('.hg/hgrc', 'ab') as f:
+  ...     f.write(b'[alias]\n'
+  ...             b'invaliddoc = log\n'
+  ...             b'invaliddoc:doc = \xc0\n'
+  ...             b'invalidhelp = log\n'
+  ...             b'invalidhelp:help = \xc0\n') and None
+  $ hg help invaliddoc
+  non-ASCII character in alias definition 'invaliddoc:doc'
+  $ hg help invalidhelp
+  non-ASCII character in alias definition 'invalidhelp:help'
+  $ hg invaliddoc
+  abort: non-ASCII character in alias definition 'invaliddoc:doc'
+  [255]
+  $ hg invalidhelp
+  abort: non-ASCII character in alias definition 'invalidhelp:help'
+  [255]
+
 invalid arguments
 
   $ hg rt foo