Skip to content

Commit

Permalink
Add check for max number of allowed characters
Browse files Browse the repository at this point in the history
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
#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.
  • Loading branch information
Claire Adams committed Jun 22, 2022
1 parent 468d696 commit 1c4d871
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions chkcrontab_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions tests/test_crontab
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ OK_SPACE=" "
@monthly root Good @ Command
# FAIL 1 for bad time spec.
@m0nthly root Bad @ Command
# FAIL 1 for line too long
0 21 * * wed root /bin/bash/myscript "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"

0 comments on commit 1c4d871

Please sign in to comment.