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

Decrypting Messages fails when Relationship is not active anymore #279

Merged
merged 11 commits into from
Sep 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export class ChallengeController extends TransportController {
private async validateChallengeLocally(challenge: Challenge, signedChallenge: ChallengeSigned): Promise<{ isValid: boolean; correspondingRelationship?: Relationship }> {
if (!challenge.createdBy) return { isValid: false };

const relationship = await this.parent.relationships.getActiveRelationshipToIdentity(challenge.createdBy);
const relationship = await this.parent.relationships.getOnceActiveRelationshipToIdentity(challenge.createdBy);
if (!relationship) {
throw TransportCoreErrors.general.recordNotFound(Relationship, challenge.createdBy.toString());
throw TransportCoreErrors.general.recordNotFound("Once active Relationship to Identity", challenge.createdBy.toString());
jkoenig134 marked this conversation as resolved.
Show resolved Hide resolved
}
const challengeBuffer = CoreBuffer.fromUtf8(signedChallenge.challenge);
let isValid = false;
Expand Down
4 changes: 2 additions & 2 deletions packages/transport/src/modules/messages/MessageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,10 @@ export class MessageController extends TransportController {
);
}
} else {
relationship = await this.relationships.getActiveRelationshipToIdentity(envelope.createdBy);
relationship = await this.relationships.getOnceActiveRelationshipToIdentity(envelope.createdBy);
Magnus-Kuhn marked this conversation as resolved.
Show resolved Hide resolved

if (!relationship) {
throw TransportCoreErrors.messages.missingOrInactiveRelationship(envelope.createdBy.toString());
throw TransportCoreErrors.general.recordNotFound("Once active Relationship to Identity", envelope.createdBy.toString());
Magnus-Kuhn marked this conversation as resolved.
Show resolved Hide resolved
}

const [peerMessage, peerKey] = await this.decryptPeerEnvelope(envelope, relationship);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ export class RelationshipsController extends TransportController {
return await this.getRelationshipToIdentity(address, RelationshipStatus.Active);
}

public async getOnceActiveRelationshipToIdentity(address: CoreAddress): Promise<Relationship | undefined> {
const relationship = await this.getRelationshipToIdentity(address);
if (!relationship || !this.relationshipWasOnceActive(relationship)) return;

return relationship;
}

public relationshipWasOnceActive(relationship: Relationship): boolean {
return !!relationship.cache?.auditLog.find((entry) => entry.newStatus === RelationshipStatus.Active);
}

Magnus-Kuhn marked this conversation as resolved.
Show resolved Hide resolved
public async getRelationship(id: CoreId): Promise<Relationship | undefined> {
const relationshipDoc = await this.relationships.read(id.toString());
if (!relationshipDoc) {
Expand Down
30 changes: 23 additions & 7 deletions packages/transport/test/modules/challenges/Challenges.test.ts
jkoenig134 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IDatabaseConnection } from "@js-soft/docdb-access-abstractions";
import { Serializable } from "@js-soft/ts-serval";
import { CoreAddress, CoreDate, CoreId } from "@nmshd/core-types";
import { CryptoSignature } from "@nmshd/crypto";
import { AccountController, Challenge, ChallengeSigned, ChallengeType, Transport } from "../../../src";
import { AccountController, Challenge, ChallengeSigned, ChallengeType, Relationship, Transport } from "../../../src";
import { TestUtil } from "../../testHelpers/TestUtil";

describe("ChallengeTest", function () {
Expand All @@ -12,6 +12,7 @@ describe("ChallengeTest", function () {

let recipient: AccountController;
let sender: AccountController;
let relationship: Relationship;

beforeAll(async function () {
connection = await TestUtil.createDatabaseConnection();
Expand All @@ -24,7 +25,7 @@ describe("ChallengeTest", function () {
sender = accounts[0];
recipient = accounts[1];

await TestUtil.addRelationship(recipient, sender);
relationship = (await TestUtil.addRelationship(recipient, sender)).acceptedRelationshipPeer;
});

afterAll(async function () {
Expand Down Expand Up @@ -56,10 +57,25 @@ describe("ChallengeTest", function () {
expect(validationResult.correspondingRelationship).toBeDefined();
});

test("should not create a relationship challenge on terminated relationship", async function () {
const terminatedRelationship = (await TestUtil.terminateRelationship(recipient, sender)).terminatedRelationshipPeer;
await expect(sender.challenges.createChallenge(ChallengeType.Relationship, terminatedRelationship)).rejects.toThrow(
"error.transport.challenges.challengeTypeRequiresActiveRelationship"
);
describe("on terminated relationship", function () {
let terminatedRelationship: Relationship;
let challenge: ChallengeSigned;
beforeAll(async function () {
challenge = await sender.challenges.createChallenge(ChallengeType.Relationship, relationship);
terminatedRelationship = (await TestUtil.terminateRelationship(recipient, sender)).terminatedRelationshipPeer;
});

test("should not create a relationship challenge on terminated relationship", async function () {
await expect(sender.challenges.createChallenge(ChallengeType.Relationship, terminatedRelationship)).rejects.toThrow(
"error.transport.challenges.challengeTypeRequiresActiveRelationship"
);
});

test("should still validate the challenge on a terminated relationship", async function () {
const validationResult = await recipient.challenges.validateChallenge(challenge);
expect(validationResult).toBeDefined();
expect(validationResult.isValid).toBe(true);
expect(validationResult.correspondingRelationship).toBeDefined();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,19 @@ describe("MessageController", function () {
});

describe("Relationship Termination", function () {
let messageId: CoreId;
beforeAll(async function () {
Magnus-Kuhn marked this conversation as resolved.
Show resolved Hide resolved
messageId = (await TestUtil.sendMessage(sender, recipient)).id;
await TestUtil.terminateRelationship(sender, recipient);
});

test("should not send a message on a terminated relationship", async function () {
await expect(TestUtil.sendMessage(sender, recipient)).rejects.toThrow("error.transport.messages.missingOrInactiveRelationship");
});

test("should still decrypt the message", async function () {
await expect(sender.messages.fetchCaches([messageId])).resolves.not.toThrow();
await expect(recipient.messages.fetchCaches([messageId])).resolves.not.toThrow();
});
});
});