1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Defend against self-referencing composites

Co-authored-by: Janne Savolainen <janne.savolainen@live.fi>

Signed-off-by: Iku-turso <mikko.aspiala@gmail.com>
This commit is contained in:
Iku-turso 2022-10-11 14:51:04 +03:00 committed by Janne Savolainen
parent 822f4394fe
commit db774c18d9
No known key found for this signature in database
GPG Key ID: 8C6CFB2FFFE8F68A
2 changed files with 35 additions and 1 deletions

View File

@ -235,6 +235,26 @@ Available parent ids are:
});
});
it("given items with same id and parent id, throws", () => {
const someItem = {
id: "some-id",
parentId: "some-id",
};
const someRoot = {
id: "root",
someParentId: undefined,
};
const items = [someItem, someRoot];
expect(() => {
getComposite({
source: items,
});
}).toThrow('Tried to get a composite, but found items with self as parent: "some-id"');
});
it("given undefined ids, throws", () => {
const root = {
someParentId: undefined,

View File

@ -44,12 +44,26 @@ export default <T>({
filter((x) => getId(x) === undefined),
);
if(undefinedIds.length) {
if (undefinedIds.length) {
throw new Error(
`Tried to get a composite but encountered ${undefinedIds.length} undefined ids`,
);
}
const selfReferencingIds = pipeline(
source,
filter((x) => getId(x) === getParentId(x)),
map(getId),
);
if (selfReferencingIds.length) {
throw new Error(
`Tried to get a composite, but found items with self as parent: "${selfReferencingIds.join(
'", ',
)}"`,
);
}
const duplicateIds = pipeline(
source,
countBy(getId),