fix(captain): add operation whitelist to prevent method injection

- Add ALLOWED_OPERATIONS whitelist to validate operation parameter
- Prevent arbitrary method invocation via send()
- Refactor tone methods using metaprogramming to reduce duplication
- Add specs for invalid operations and injection attempts
This commit is contained in:
Shivam Mishra
2026-01-13 18:22:35 +05:30
parent f63cef20bf
commit 7fba7b946e
2 changed files with 37 additions and 21 deletions
+24
View File
@@ -139,4 +139,28 @@ RSpec.describe Captain::RewriteService do
expect(result[:message]).to eq('Rewritten text')
end
end
describe '#perform with invalid operation' do
it 'raises ArgumentError for unknown operation' do
invalid_service = described_class.new(
account: account,
content: content,
operation: 'invalid_operation',
conversation_display_id: conversation.display_id
)
expect { invalid_service.perform }.to raise_error(ArgumentError, /Invalid operation/)
end
it 'prevents method injection attacks' do
dangerous_service = described_class.new(
account: account,
content: content,
operation: 'perform',
conversation_display_id: conversation.display_id
)
expect { dangerous_service.perform }.to raise_error(ArgumentError, /Invalid operation/)
end
end
end