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

fix(desktop): correct handling of '#' wildcard in topic filtering #1742

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 38 additions & 21 deletions src/database/services/MessageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,42 @@ export default class MessageService {
} as MessageModel
}

public handleTopicQuery(query: $TSFixed, topic?: string) {
if (topic && topic !== '#') {
// Escape special characters for SQL LIKE
topic = topic.replace(/[\\%_]/g, '\\$&')

// Remove $share prefix if present
if (topic.startsWith('$share/')) {
topic = topic.split('/').slice(2).join('/')
}

/*
Handle `+` wildcard
Known Issue: '+' wildcard handling in MQTT topics is incorrect.
'+' is replaced with '%' for SQL LIKE, causing multi-level match.
- Incorrect: 'testtopic/+/test' matches 'testtopic/1/2/test'
- Incorrect: 'testtopic/+/hello/+' can not matches 'testtopic/hello/hello/hello'
TODO: FIX this issue.
*/
if (topic.includes('+')) {
topic = topic.replace('+', '%')
}

// Handle '#' wildcard
if (topic.endsWith('/#')) {
const baseTopic = topic.slice(0, -2) // Remove '/#'
query.andWhere('(msg.topic = :baseTopic OR msg.topic LIKE :topic ESCAPE "\\")', {
baseTopic,
topic: baseTopic + '/%',
})
} else {
query.andWhere('msg.topic LIKE :topic ESCAPE "\\"', { topic })
}
}
return query
}

public async get(
connectionId: string,
options: {
Expand All @@ -60,20 +96,7 @@ export default class MessageService {

msgType !== 'all' && query.andWhere('msg.out = :out', { out: msgType === 'publish' })

if (topic && topic !== '#') {
topic = topic.replace(/[\\%_]/g, '\\$&')
if (topic.startsWith('$share/')) topic = topic.split('/').slice(2).join('/')
if (topic.includes('#') && topic.endsWith('/#')) topic = topic.replace('#', '%')
/*
Known Issue: '+' wildcard handling in MQTT topics is incorrect.
'+' is replaced with '%' for SQL LIKE, causing multi-level match.
- Incorrect: 'testtopic/+/test' matches 'testtopic/1/2/test'
- Incorrect: 'testtopic/+/hello/+' can not matches 'testtopic/hello/hello/hello'
TODO: FIX this issue.
*/
if (topic.includes('+')) topic = topic.replace('+', '%')
query.andWhere('msg.topic LIKE :topic ESCAPE "\\"', { topic })
}
query = this.handleTopicQuery(query, topic)

if (options.searchParams) {
const { topic, payload } = options.searchParams
Expand Down Expand Up @@ -124,13 +147,7 @@ export default class MessageService {

msgType !== 'all' && query.andWhere('msg.out = :out', { out: msgType === 'publish' })

if (topic && topic !== '#') {
topic = topic.replace(/[\\%_]/g, '\\$&')
if (topic.startsWith('$share/')) topic = topic.split('/').slice(2).join('/')
if (topic.includes('#') && topic.endsWith('/#')) topic = topic.replace('#', '%')
if (topic.includes('+')) topic = topic.replace('+', '%')
query.andWhere('msg.topic LIKE :topic ESCAPE "\\"', { topic })
}
query = this.handleTopicQuery(query, topic)

if (options.searchParams) {
const { topic, payload } = options.searchParams
Expand Down