Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long ClientId causes unwanted MqttPersistenceException inside MqttDefaultFilePersistence #1061

Open
dafota opened this issue Oct 19, 2024 · 0 comments

Comments

@dafota
Copy link

dafota commented Oct 19, 2024

Description:

I encountered an issue in the open method where, if the combined length of clientId and theConnection exceeds the maximum file name length, the directory creation fails silently. This can cause the restoreBackups function to throw an MqttPersistenceException.

Steps to Reproduce:

  1. Pass a clientId and theConnection whose combined length (after filtering with isSafeChar) results in a key exceeding 255 characters.
  2. When the open method tries to create a directory with this key, the directory creation fails because the file system's file name length limit is 255 characters.

Impact:

This issue imposes an unintended limitation on MQTT's support for long clientId values, which should normally be supported up to 65,535 characters. It affects the persistence mechanism and could disrupt the normal operation of the library.

Suggested Solutions:

1- Validate key Length: Add a check to ensure that the key length does not exceed 255 characters before creating the directory. If it does, consider truncating the key or using a hashed version to fit within the limit.
2- Handle mkdir Failure: Check the return value of mkdir and throw an appropriate exception if directory creation fails, with a clear error message indicating the cause.

Relevant Code:

Here is the part of the open function where the issue occurs:

if (!clientDir.exists()) {
    clientDir.mkdir(); // Fails silently if the key length exceeds 255 characters
}

And in the restoreBackups method:

File[] files = dir.listFiles(new PersistanceFileFilter(MESSAGE_BACKUP_FILE_EXTENSION));

if (files == null) {
    throw new MqttPersistenceException();
}

However, if the length of the key exceeds 255 characters, mkdir fails and does not throw an exception. The directory remains non-existent, causing subsequent calls to listFiles() in the restoreBackups method to return null, which then triggers an MqttPersistenceException.

Environment:

  • Library version: 1.2.5
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

No branches or pull requests

1 participant