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:
parent
822f4394fe
commit
db774c18d9
@ -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,
|
||||
|
||||
@ -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),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user