Unix: Channel-specific list of files for @fortune.

Also forbid file names starting with a dash, because
arguments to the fortune argument could be used for
DoSing.
This commit is contained in:
Valentin Lorentz 2015-12-27 12:32:09 +01:00
parent 49ff291f61
commit b42437c711
2 changed files with 25 additions and 2 deletions

View File

@ -52,6 +52,20 @@ def configure(advanced):
default=True): default=True):
conf.supybot.commands.disabled().add('Unix.progstats') conf.supybot.commands.disabled().add('Unix.progstats')
class NonOptionString(registry.String):
errormsg = _('Value must be a string not starting with a dash (-), not %r.')
def __init__(self, *args, **kwargs):
self.__parent = super(NonOptionString, self)
self.__parent.__init__(*args, **kwargs)
def setValue(self, v):
if v.startswith('-'):
self.error(v)
else:
self.__parent.setValue(v)
class SpaceSeparatedListOfNonOptionStrings(registry.SpaceSeparatedListOfStrings):
Value = NonOptionString
Unix = conf.registerPlugin('Unix') Unix = conf.registerPlugin('Unix')
conf.registerGroup(Unix, 'fortune') conf.registerGroup(Unix, 'fortune')
@ -70,8 +84,8 @@ conf.registerChannelValue(Unix.fortune, 'offensive',
registry.Boolean(False, _("""Determines whether fortune will retrieve registry.Boolean(False, _("""Determines whether fortune will retrieve
offensive fortunes along with the normal fortunes. This sends the -a offensive fortunes along with the normal fortunes. This sends the -a
option to the fortune program."""))) option to the fortune program.""")))
conf.registerGlobalValue(Unix.fortune, 'files', conf.registerChannelValue(Unix.fortune, 'files',
registry.SpaceSeparatedListOfStrings([], _("""Determines what specific file SpaceSeparatedListOfNonOptionStrings([], _("""Determines what specific file
(if any) will be used with the fortune command; if none is given, the (if any) will be used with the fortune command; if none is given, the
system-wide default will be used. Do note that this fortune file must be system-wide default will be used. Do note that this fortune file must be
placed with the rest of your system's fortune files."""))) placed with the rest of your system's fortune files.""")))

View File

@ -53,6 +53,15 @@ else:
skipUnlessPing6 = skipIf(utils.findBinaryInPath('ping6') is None, skipUnlessPing6 = skipIf(utils.findBinaryInPath('ping6') is None,
'ping6 not available.') 'ping6 not available.')
class UnixConfigTestCase(ChannelPluginTestCase):
plugins = ('Unix',)
def testFortuneFiles(self):
self.assertNotError('config channel plugins.Unix.fortune.files '
'foo bar')
self.assertRegexp('config channel plugins.Unix.fortune.files '
'"-foo bar"',
'Error:.*dash.*not \'-foo\'')
self.assertNotError('config channel plugins.Unix.fortune.files ""')
if os.name == 'posix': if os.name == 'posix':