Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions src/client/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,16 +413,18 @@ module.exports = function (client, options) {
if (mcData.supportFeature('useChatSessions')) { // 1.19.3+
const { acknowledged, acknowledgements } = getAcknowledgements()
const canSign = client.profileKeys && client._session
const argumentSignatures = canSign ? signaturesForCommand(command, options.timestamp, options.salt, options.preview, acknowledgements) : []
const chatPacket = {
command,
timestamp: options.timestamp,
salt: options.salt,
argumentSignatures: canSign ? signaturesForCommand(command, options.timestamp, options.salt, options.preview, acknowledgements) : [],
argumentSignatures,
messageCount: client._lastSeenMessages.pending,
checksum: computeChatChecksum(client._lastSeenMessages), // 1.21.5+
acknowledged
}
client.write((mcData.supportFeature('seperateSignedChatCommandPacket') && canSign) ? 'chat_command_signed' : 'chat_command', chatPacket)
// A command with nothing to sign goes as the unsigned chat_command whether or not the client can sign.
client.write((mcData.supportFeature('seperateSignedChatCommandPacket') && argumentSignatures.length > 0) ? 'chat_command_signed' : 'chat_command', chatPacket)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Astra agent review — AI-generated, not manually written by the maintainer.

On versions with seperateSignedChatCommandPacket (for example 1.21.8), the newly selected chat_command contains only command, so the acknowledgement fields in chatPacket never reach the server. However, this branch still calls getAcknowledgements() (which marks entries as acknowledged) and then resets _lastSeenMessages.pending. I reproduced receiving one signed message, sending /list with signing enabled, and then sending chat: the command clears pending from 1 to 0 and the following chat_message sends offset 0, although no packet reported that acknowledgement offset. This desynchronizes the last-seen window used for chat validation. Could the unsigned-command branch leave both the pending count and entry flags untouched, with a regression covering an unsigned command followed by signed chat? Older versions whose chat_command carries acknowledgements should retain their current accounting.

client._lastSeenMessages.pending = 0
} else {
client.write('chat_command', {
Expand Down
57 changes: 57 additions & 0 deletions test/declareCommandsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,61 @@ describe('declare_commands handling', () => {
assert.strictEqual(writes[0].name, 'chat_command_signed')
assert.deepStrictEqual(writes[0].data.argumentSignatures.map(sig => sig.argumentName), ['message'])
})

it('sends a command with no signable argument as the unsigned packet', () => {
const client = new EventEmitter()
client.version = '26.1'
client.verifyMessage = () => true
client.profileKeys = true
client._session = { uuid: '00000000-0000-0000-0000-000000000000' }

const writes = []
client.write = (name, data) => writes.push({ name, data })

injectChatPlugin(client, {})
client.signMessage = () => Buffer.from([1])

client.emit('declare_commands', {
rootIndex: 0,
nodes: [
{ children: [1] },
{ children: [2], extraNodeData: { name: 'msg' } },
{ children: [], extraNodeData: { name: 'message', parser: 'minecraft:message' } }
]
})

client._signedChat('/login hunter2', { timestamp: 1n, salt: 1n })

assert.strictEqual(writes.length, 1)
assert.strictEqual(writes[0].name, 'chat_command')
assert.deepStrictEqual(writes[0].data.argumentSignatures, [])
})

it('sends a known command without arguments as the unsigned packet', () => {
const client = new EventEmitter()
client.version = '26.1'
client.verifyMessage = () => true
client.profileKeys = true
client._session = { uuid: '00000000-0000-0000-0000-000000000000' }

const writes = []
client.write = (name, data) => writes.push({ name, data })

injectChatPlugin(client, {})
client.signMessage = () => Buffer.from([1])

client.emit('declare_commands', {
rootIndex: 0,
nodes: [
{ children: [1] },
{ children: [2], extraNodeData: { name: 'msg' } },
{ children: [], extraNodeData: { name: 'message', parser: 'minecraft:message' } }
]
})

client._signedChat('/msg', { timestamp: 1n, salt: 1n })

assert.strictEqual(writes.length, 1)
assert.strictEqual(writes[0].name, 'chat_command')
})
})
Loading