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

feat(core): validate outbound messages #526

Merged
Merged
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
15 changes: 15 additions & 0 deletions packages/core/src/agent/MessageSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/agent/__tests__/AgentMessage.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/agent/__tests__/MessageSender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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,
})
Expand All @@ -237,7 +237,7 @@ describe('MessageSender', () => {
const sendMessageSpy = jest.spyOn(outboundTransport, 'sendMessage')

await messageSender.sendMessageToService({
message: new AgentMessage(),
message: new TestMessage(),
senderKey,
service,
})
Expand All @@ -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({
Expand Down Expand Up @@ -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 = {
Expand Down
11 changes: 11 additions & 0 deletions packages/core/tests/TestMessage.ts
Original file line number Diff line number Diff line change
@@ -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'
}