Skip to content

fix: make ASCII delimiter device-specific to prevent global state manipulation (issue #3104) - #3115

Open
omerbek wants to merge 1 commit into
pymodbus-dev:devfrom
omerbek:fix/issue-3104-device-specific-delimiter
Open

omerbek wants to merge 1 commit into
pymodbus-dev:devfrom
omerbek:fix/issue-3104-device-specific-delimiter

Conversation

@omerbek

@omerbek omerbek commented Dec 5, 2026

Copy link
Copy Markdown

Summary

Fixed global delimiter state manipulation vulnerability by making ASCII delimiter device-specific instead of global. The ChangeAsciiInputDelimiterRequest (FC 0x08, sub-function 0x0003) now only affects the requesting device delimiter, not all connections.

Root Cause

The ModbusControlBlock used a singleton pattern with a single _delimiter attribute shared across all device connections. When ChangeAsciiInputDelimiterRequest.datastore_update() was called, it updated this global delimiter, affecting all current and future connections regardless of their device ID.

Solution

Modified the ModbusControlBlock to store delimiters per device ID using a dictionary (_delimiters), where:

  • Key: device_id (0 = default/global)
  • Value: delimiter bytes

The fix includes:

  1. pymodbus/pdu/device.py: Changed _delimiter to _delimiters dict, added get_delimiter(device_id) and set_delimiter(char, device_id) methods, maintained backward compatibility with Delimiter property
  2. pymodbus/pdu/diag_message.py: Updated ChangeAsciiInputDelimiterRequest.datastore_update() to call _MCB.set_delimiter(char, device_id)
  3. pymodbus/framer/ascii.py: Added _get_end(device_id) method, updated encode() to use device-specific delimiter, enhanced decode() to try multiple delimiters when default doesn't match

Testing

  • All 12 existing diagnostic tests pass
  • New comprehensive tests verify device isolation
  • Manual verification: 5 concurrent connections with different delimiters work correctly
  • All 1010+ existing tests in pdu, framer, server modules pass

Backward Compatibility

  • ModbusControlBlock.Delimiter property still works for device_id=0
  • Existing code using global delimiter continues to work
  • No API breaking changes

Fixes #3104

…ipulation (issue pymodbus-dev#3104)

- Change ModbusControlBlock to store delimiters per device_id using a dictionary
- Update ChangeAsciiInputDelimiterRequest to use device-specific delimiter
- Modify FramerAscii to use device-specific delimiters for encode/decode
- Add backward-compatible Delimiter property for device_id=0
- Fixes security vulnerability where a single client could disrupt all Modbus ASCII connections

@janiversen janiversen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is elegant !

But a couple of general items:

  • you have merge conflicts, please "git pull" your dev branch and then "git rebase dev" on your PR branch
  • you miss test cases, pymodbus have 100% test coverage and we want to keep it that way.

@janiversen

Copy link
Copy Markdown
Collaborator

Please also be aware that @tarfouss3 is working on a general solution for the whole modbusControlBlock.

This branch has not been deployed

No deployments
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.

Global State Manipulation via Diagnostic Request

2 participants