Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.
Ruby agplai-assistantdockerdsgvogdprhotwireissue-trackerjira-alternativeknowledge-basellmmcp-servermeeting-notesopen-sourceproject-managementredmine-alternativeruby-on-railsself-hostedticketingtime-tracking
fix: review round 1 of #84, let the store refuse a key it cannot hold
Guido Boyke00ec34d94614
diff --git a/CLAUDE.md b/CLAUDE.mdindex 39c9b18..403b497 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -811,7 +811,8 @@ feature by feature):crosses surfaces. Runs inside authentication and the read-only gate. A key is at most 255bytes of valid UTF-8 without NUL (else: 422 `invalid_idempotency_key`, nothing written).One rule for both surfaces, `Mcp::IdempotencyStore.client_key` (strip, blank is no key) and- `.storable_key?`: since MCP 0.13.1 a write tool refuses such an `idempotency_key` as+ `.storable_key?` (`IdempotencyStore.new` raises on any other key, before the write, so a+ third caller cannot skip it): since MCP 0.13.1 a write tool refuses such an `idempotency_key` as`invalid_input` before it runs, after the project and read-only gates (guard`spec/requests/api/mcp_idempotency_key_bound_spec.rb`; it used to write, fail to rememberthe key with -32603 and write again on every retry). A replay checks no rightsdiff --git a/app/services/mcp/dispatcher.rb b/app/services/mcp/dispatcher.rbindex ade73b8..490a102 100644--- a/app/services/mcp/dispatcher.rb+++ b/app/services/mcp/dispatcher.rb@@ -48,6 +48,9 @@ module Mcp# for a token that reaches every project of the organisation named).# 0.13.1 = a key the store cannot hold (more than 255 bytes, no valid UTF-8,# a NUL) is refused as invalid_input before the write, as REST does since R0.+ # It narrows an existing argument on purpose, released as a fix: a key of 256+ # bytes up to about 2.6 KB used to be stored and replayed (one stored before+ # is refused now, not replayed), a longer one wrote again on every retry.SERVER_VERSION = '0.13.1'# MCP surface — sourced from the single tool registry (D6): the :mcp set is thediff --git a/app/services/mcp/idempotency_store.rb b/app/services/mcp/idempotency_store.rbindex 932f4b8..1c5ec6f 100644--- a/app/services/mcp/idempotency_store.rb+++ b/app/services/mcp/idempotency_store.rb@@ -59,11 +59,13 @@ module Mcpend# Within the index (bytes) and storable in a PostgreSQL text column: valid- # UTF-8 without NUL, which the database refuses with an error otherwise.- # Takes a key from .client_key; a binary one is read as the UTF-8 it claims.+ # UTF-8 without NUL, which the database refuses with an error otherwise; and+ # not blank, which the model refuses (blank? in the key's own encoding, the+ # one the model sees). Takes a key from .client_key, which is never blank; a+ # binary one is read as the UTF-8 it claims.def self.storable_key?(key)utf8 = key.dup.force_encoding(Encoding::UTF_8)- key.bytesize <= MAX_KEY_LENGTH && utf8.valid_encoding? && !utf8.include?("\0")+ key.bytesize <= MAX_KEY_LENGTH && utf8.valid_encoding? && !utf8.include?("\0") && key.present?end# The refusal of a key outside .storable_key?, naming the field the surface@@ -79,6 +81,11 @@ module Mcp# @param args [Hash] the request args the key dedupes (already minus the key);# its fingerprint detects a key reused with a DIFFERENT payload.def initialize(token:, tool_name:, key:, args:)+ # Both surfaces refuse such a key before the write. A caller that forgot+ # would fail only in #remember, after the write, and its retry would write+ # again: refused here, before anything runs.+ raise ArgumentError, 'the store cannot hold this idempotency key' unless self.class.storable_key?(key)+@token = token@tool_name = tool_name@key = keydiff --git a/spec/requests/api/v1/idempotency_spec.rb b/spec/requests/api/v1/idempotency_spec.rbindex def3f6c..eee6441 100644--- a/spec/requests/api/v1/idempotency_spec.rb+++ b/spec/requests/api/v1/idempotency_spec.rb@@ -194,6 +194,25 @@ RSpec.describe 'API V1 idempotency', :ci_guard, type: :request dokey = 'k' * Mcp::IdempotencyStore::MAX_KEY_LENGTHexpect { 2.times { comment('Hallo', key: key) } }.to change { posts_count }.by(1)end++ # The header is read by the rule the MCP argument is read by+ # (Mcp::IdempotencyStore.client_key): surrounding whitespace goes before the+ # key is counted or stored, so the bare key replays the first answer.+ it 'drops surrounding whitespace before it counts, as MCP does' do+ key = 'k' * Mcp::IdempotencyStore::MAX_KEY_LENGTH+ expect { comment('Hallo', key: " #{key}\t") }.to change { posts_count }.by(1)+ first = snapshot+ expect(first.first).to eq(201)++ expect { comment('Hallo', key: key) }.not_to(change { posts_count })+ expect(snapshot).to eq(first)+ end++ it 'names the header and the bound in its refusal' do+ comment('Hallo', key: 'k' * (Mcp::IdempotencyStore::MAX_KEY_LENGTH + 1))+ expect(response.parsed_body).to eq('error' => 'invalid_idempotency_key', 'code' => 'invalid_input',+ 'message' => 'Idempotency-Key must be valid UTF-8 without NUL, at most 255 bytes')+ endend# A malformed JSON body answers like any bad request, with a code, whetherdiff --git a/spec/services/mcp/idempotency_store_spec.rb b/spec/services/mcp/idempotency_store_spec.rbindex f633314..b0d2861 100644--- a/spec/services/mcp/idempotency_store_spec.rb+++ b/spec/services/mcp/idempotency_store_spec.rb@@ -99,6 +99,16 @@ RSpec.describe Mcp::IdempotencyStore, :ci_guard doit 'strips a binary header like any other key' doexpect(described_class.client_key(' k-1 '.b)).to eq('k-1'.b)end++ # Blank is Rails' blank? in the key's own encoding, as before on both+ # surfaces: in UTF-8 (an MCP argument) a Unicode space is blank, so the call+ # has no key; in binary (a REST header from Puma) only an ASCII space is, so+ # the same bytes are a key there, and the model, which sees the same binary+ # string, stores it.+ it 'reads Unicode spaces alone as no key in UTF-8 and as a key in binary' do+ expect(described_class.client_key("\u00A0")).to be_nil+ expect(described_class.client_key("\xC2\xA0".b)).to eq("\xC2\xA0".b)+ endenddescribe '.storable_key?' do@@ -126,6 +136,24 @@ RSpec.describe Mcp::IdempotencyStore, :ci_guard doit 'holds a binary key whose bytes are valid UTF-8' doexpect(described_class.storable_key?('schlüssel'.b)).to be(true)end++ # The model refuses a blank key (presence), after the write: blank in the+ # key's own encoding, as the model sees it.+ it 'refuses a blank key, holding Unicode spaces only in binary' do+ expect(['', " \t", "\u00A0"].map { |key| described_class.storable_key?(key) }).to all(be(false))+ expect(described_class.storable_key?("\xC2\xA0".b)).to be(true)+ end+ end++ describe '.new' do+ # Both surfaces refuse such a key before the write. One that forgot would+ # otherwise fail only in #remember, after the write, and its retry would+ # write again: the store refuses it before anything runs.+ it 'refuses a key the store cannot hold' do+ ['k' * 256, "k\0x", "k\xFFx".b, ''].each do |key|+ expect { store(key: key, args: { a: 1 }) }.to raise_error(ArgumentError, /cannot hold/)+ end+ endendit 'names the field and the bound in its refusal' do