diff --git a/packages/core/src/agent/MessageSender.ts b/packages/core/src/agent/MessageSender.ts index dc9a7793bb..d0bbdb256e 100644 --- a/packages/core/src/agent/MessageSender.ts +++ b/packages/core/src/agent/MessageSender.ts @@ -12,6 +12,7 @@ import { ReturnRouteTypes } from '../decorators/transport/TransportDecorator' import { AriesFrameworkError } from '../error' import { Logger } from '../logger' import { MessageRepository } from '../storage/MessageRepository' +import { MessageValidator } from '../utils/MessageValidator' import { EnvelopeService } from './EnvelopeService' import { TransportService } from './TransportService' @@ -252,6 +253,20 @@ export class MessageSender { message.setReturnRouting(ReturnRouteTypes.all) } + try { + await MessageValidator.validate(message) + } catch (error) { + this.logger.error( + `Aborting sending outbound message ${message.type} to ${service.serviceEndpoint}. Message validation failed`, + { + errors: error, + message: message.toJSON(), + } + ) + + throw error + } + const outboundPackage = await this.packMessage({ message, keys, endpoint: service.serviceEndpoint }) outboundPackage.endpoint = service.serviceEndpoint outboundPackage.connectionId = connectionId diff --git a/packages/core/src/agent/__tests__/AgentMessage.test.ts b/packages/core/src/agent/__tests__/AgentMessage.test.ts index fc663b6750..da8f7b11a6 100644 --- a/packages/core/src/agent/__tests__/AgentMessage.test.ts +++ b/packages/core/src/agent/__tests__/AgentMessage.test.ts @@ -1,8 +1,4 @@ -import { AgentMessage } from '../AgentMessage' - -class TestMessage extends AgentMessage { - public readonly type = 'https://didcomm.org/connections/1.0/invitation' -} +import { TestMessage } from '../../../tests/TestMessage' describe('AgentMessage', () => { describe('toJSON', () => { diff --git a/packages/core/src/agent/__tests__/MessageSender.test.ts b/packages/core/src/agent/__tests__/MessageSender.test.ts index f58d7f6e95..8f55f9bd5e 100644 --- a/packages/core/src/agent/__tests__/MessageSender.test.ts +++ b/packages/core/src/agent/__tests__/MessageSender.test.ts @@ -3,12 +3,12 @@ import type { MessageRepository } from '../../storage/MessageRepository' import type { OutboundTransport } from '../../transport' import type { OutboundMessage, WireMessage } from '../../types' +import { TestMessage } from '../../../tests/TestMessage' import { getAgentConfig, getMockConnection, mockFunction } from '../../../tests/helpers' import testLogger from '../../../tests/logger' import { ReturnRouteTypes } from '../../decorators/transport/TransportDecorator' import { DidCommService } from '../../modules/connections' import { InMemoryMessageRepository } from '../../storage/InMemoryMessageRepository' -import { AgentMessage } from '../AgentMessage' import { EnvelopeService as EnvelopeServiceImpl } from '../EnvelopeService' import { MessageSender } from '../MessageSender' import { TransportService } from '../TransportService' @@ -51,7 +51,7 @@ describe('MessageSender', () => { const enveloperService = new EnvelopeService() const envelopeServicePackMessageMock = mockFunction(enveloperService.packMessage) - const inboundMessage = new AgentMessage() + const inboundMessage = new TestMessage() inboundMessage.setReturnRouting(ReturnRouteTypes.all) const session = new DummyTransportSession('session-123') @@ -98,7 +98,7 @@ describe('MessageSender', () => { messageSender = new MessageSender(enveloperService, transportService, messageRepository, logger) connection = getMockConnection({ id: 'test-123', theirLabel: 'Test 123' }) - outboundMessage = createOutboundMessage(connection, new AgentMessage()) + outboundMessage = createOutboundMessage(connection, new TestMessage()) envelopeServicePackMessageMock.mockReturnValue(Promise.resolve(wireMessage)) transportServiceFindServicesMock.mockReturnValue([firstDidCommService, secondDidCommService]) @@ -225,7 +225,7 @@ describe('MessageSender', () => { test('throws error when there is no outbound transport', async () => { await expect( messageSender.sendMessageToService({ - message: new AgentMessage(), + message: new TestMessage(), senderKey, service, }) @@ -237,7 +237,7 @@ describe('MessageSender', () => { const sendMessageSpy = jest.spyOn(outboundTransport, 'sendMessage') await messageSender.sendMessageToService({ - message: new AgentMessage(), + message: new TestMessage(), senderKey, service, }) @@ -254,7 +254,7 @@ describe('MessageSender', () => { messageSender.registerOutboundTransport(outboundTransport) const sendMessageSpy = jest.spyOn(outboundTransport, 'sendMessage') - const message = new AgentMessage() + const message = new TestMessage() message.setReturnRouting(ReturnRouteTypes.all) await messageSender.sendMessageToService({ @@ -287,7 +287,7 @@ describe('MessageSender', () => { }) test('return outbound message context with connection, payload and endpoint', async () => { - const message = new AgentMessage() + const message = new TestMessage() const endpoint = 'https://example.com' const keys = { diff --git a/packages/core/tests/TestMessage.ts b/packages/core/tests/TestMessage.ts new file mode 100644 index 0000000000..299f1f6147 --- /dev/null +++ b/packages/core/tests/TestMessage.ts @@ -0,0 +1,11 @@ +import { AgentMessage } from '../src/agent/AgentMessage' + +export class TestMessage extends AgentMessage { + public constructor() { + super() + + this.id = this.generateId() + } + + public readonly type = 'https://didcomm.org/connections/1.0/invitation' +}