Skip to content
Open
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
9 changes: 9 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ Version 2.0.25

To be released.

### @fedify/fedify

- Standalone key documents whose `id` differs from the requested key URL are
now rejected instead of being cached under the wrong URL.
([#963], [#980] by Junseok Oh)

[#963]: https://github.com/fedify-dev/fedify/issues/963
[#980]: https://github.com/fedify-dev/fedify/issues/980


Version 2.0.24
--------------
Expand Down
6 changes: 6 additions & 0 deletions changes.d/fedify/mismatched-key-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- Standalone key documents whose `id` differs from the requested key URL are
now rejected instead of being cached under the wrong URL.
([#963], [#980] by Junseok Oh)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use square brackets instead of parentheses:

Suggested change
([#963], [#980] by Junseok Oh)
[[#963], [#980] by Junseok Oh]


[#963]: https://github.com/fedify-dev/fedify/issues/963
[#980]: https://github.com/fedify-dev/fedify/issues/980
Comment on lines +5 to +6

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to declare these link definitions; instead you should add resolved URLs in the front matter; use the sacho resolve-links command.

59 changes: 59 additions & 0 deletions packages/fedify/src/sig/key.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,65 @@ test("fetchKey()", async () => {
);
});

test("fetchKey() rejects standalone keys with a mismatched id", async () => {
for (
const { keyId, standaloneKey, fetch } of [
{
keyId: "https://example.com/key",
standaloneKey: rsaPublicKey1,
fetch: (keyId: string, options: FetchKeyOptions) =>
fetchKey(keyId, CryptographicKey, options),
},
{
keyId: "https://example.com/multikey",
standaloneKey: ed25519Multikey,
fetch: (keyId: string, options: FetchKeyOptions) =>
fetchKey(keyId, Multikey, options),
},
]
) {
const cache: Record<string, CryptographicKey | Multikey | null> = {};
const options: FetchKeyOptions = {
async documentLoader(resource) {
if (resource === keyId) {
const document = await standaloneKey.toJsonLd({
contextLoader: mockDocumentLoader,
});
return {
contextUrl: null,
documentUrl: resource,
document: {
...document as Record<string, unknown>,
id: "https://example.com/different-key",
},
};
}
return await mockDocumentLoader(resource);
},
contextLoader: mockDocumentLoader,
keyCache: {
get(keyId) {
return Promise.resolve(cache[keyId.href]);
},
set(keyId, key) {
cache[keyId.href] = key;
return Promise.resolve();
},
} satisfies KeyCache,
};

assertEquals(await fetch(keyId, options), {
key: null,
cached: false,
});
assertEquals(cache, { [keyId]: null });
assertEquals(await fetch(keyId, options), {
key: null,
cached: true,
});
}
});

test("fetchKey() returns null for a malformed actor publicKey", async () => {
const actorId = "https://example.com/malformed-public-key";
const keyId = "https://example.com/malformed-public-key#main-key";
Expand Down
5 changes: 4 additions & 1 deletion packages/fedify/src/sig/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,10 @@ async function fetchKeyInternal<T extends CryptographicKey | Multikey>(
}
}
let key: T | null = null;
if (object instanceof cls) key = object;
if (
object instanceof cls &&
(object.id == null || object.id.href === keyId)
) key = object;
else if (isActor(object)) {
// Treat malformed remote actor keys as missing keys.
// @ts-ignore: cls is either CryptographicKey or Multikey
Expand Down
Loading