Skip to content

fix: block Junos command abbreviations from bypassing block.cmd - #33

Closed
Venkata-Sai-Vishwanath-robo wants to merge 1 commit into
Juniper:mainfrom
Venkata-Sai-Vishwanath-robo:fix
Closed

Venkata-Sai-Vishwanath-robo wants to merge 1 commit into
Juniper:mainfrom
Venkata-Sai-Vishwanath-robo:fix

Conversation

@Venkata-Sai-Vishwanath-robo

Copy link
Copy Markdown
Contributor

check_command_blocklist matched the literal spelling of block.cmd patterns via a whole-string regex, but Junos accepts any unambiguous prefix abbreviation of a CLI keyword (and is case-insensitive), so e.g. "req sys reboot" reached the device unblocked while "request system reboot" was rejected. Matching is now token-wise, treating each pattern token as a case-insensitive Junos-style prefix abbreviation.

check_command_blocklist matched the literal spelling of block.cmd
patterns via a whole-string regex, but Junos accepts any unambiguous
prefix abbreviation of a CLI keyword (and is case-insensitive), so
e.g. "req sys reboot" reached the device unblocked while "request
system reboot" was rejected. Matching is now token-wise, treating
each pattern token as a case-insensitive Junos-style prefix
abbreviation.
@Venkata-Sai-Vishwanath-robo
Venkata-Sai-Vishwanath-robo marked this pull request as draft August 10, 2026 21:12
@Venkata-Sai-Vishwanath-robo
Venkata-Sai-Vishwanath-robo marked this pull request as ready for review August 11, 2026 05:10
@nileshsimaria

Copy link
Copy Markdown
Collaborator

Thanks for the PR, and for catching the abbreviation bypass in block.cmd — it was a real gap.

I'm closing this as superseded. While this was open, 75fe90a landed on main and fixed the same issue with a shared _matches_blocklist_tokens helper used by both check_command_blocklist and check_config_blocklist, plus unit tests (test_blocks_abbreviated_literal_tokens). That is also why this branch now shows conflicts in jmcp.py.

A few reasons we went with the shared helper rather than merging this version:

  • Tokens containing regex metacharacters fall back to re.fullmatch only, so abbreviation handling is skipped for regex-style patterns the README documents (e.g. request system (reboot|halt) still lets req sys reb through).
  • The fix only covers the operational-command path. Configuration mode accepts the same abbreviations, so block.cfg was still bypassable (set sys root-auth ... vs set system root-authentication).
  • pattern.split() drops the existing escape-aware / character-class-aware tokenizer, so patterns like request system[ ]reboot break into invalid regex fragments.
  • Moving from re.match to per-token startswith changed the semantics of existing patterns whose last token is a partial keyword (e.g. request system power no longer blocks request system power-off).
  • No unit test accompanied the change, even though tests/test_blocklist_guardrails.py already exercises this function.

If you spot anything the version on main still misses, a fresh PR against current main with a failing test would be very welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants