From 94c2f59f6fe4cd313b221831ca2e68de7ecacee9 Mon Sep 17 00:00:00 2001 From: Claire Adams Date: Tue, 21 Jun 2022 17:03:53 -0700 Subject: [PATCH] Add check for max number of allowed characters According to the crontab man pages, the maximum number of characters in the command field is 998 https://manpages.debian.org/jessie/cron/crontab.5.en.html. This PR adds a check to catch lines that are longer than 998. It updates the test file as well. This change was requested in this issue https://github.com/lyda/chkcrontab/issues/22 and I have run into issues with crontab failing because of lines being too long, so having the linter catch this issue will be a big help. --- chkcrontab_lib.py | 7 +++++++ tests/test_crontab | 2 ++ 2 files changed, 9 insertions(+) diff --git a/chkcrontab_lib.py b/chkcrontab_lib.py index 344dbd7..5178cb6 100755 --- a/chkcrontab_lib.py +++ b/chkcrontab_lib.py @@ -83,6 +83,9 @@ FILE_RE_WHITELIST = [re.compile(x) for x in (r'\.in$', r'\.cron$', r'\.disabled$', r'^(\S+\.)?cron\.d$')] +# Per https://manpages.debian.org/jessie/cron/crontab.5.en.html: +# "The maximum permitted length for the command field is 998 characters." +MAX_COMMAND_LENGTH = 998 class FSM(object): """Finite State Machine. @@ -738,6 +741,9 @@ def ValidateAndLog(self, log): log.LineWarn(log.MSG_BARE_PERCENT, 'A bare % is a line break in' ' crontab and is commonly not intended.') + if len(self.command) > MAX_COMMAND_LENGTH: + log.LineError(log.MSG_COMMAND_LENGTH_ERROR, 'Command length is %d which is greater' + ' than %d (max allowed characters)' % (len(self.command), MAX_COMMAND_LENGTH)) class CronLineAt(CronLineTimeAction): """For cron lines specified with @ time specs.""" @@ -885,6 +891,7 @@ class LogCounter(object): _msg_kinds = set(('BARE_PERCENT', 'CHKCRONTAB_ERROR', + 'COMMAND_LENGTH_ERROR', 'FIELD_PARSE_ERROR', 'FIELD_VALUE_ERROR', 'INVALID_AT', diff --git a/tests/test_crontab b/tests/test_crontab index 9d02d71..42f6f6d 100644 --- a/tests/test_crontab +++ b/tests/test_crontab @@ -69,3 +69,5 @@ OK_SPACE=" " @monthly root Good @ Command # FAIL 1 for bad time spec. @m0nthly root Bad @ Command +# FAIL 1 for command too long +0 21 * * wed root /bin/bash/myscript "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"