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

Restrict ObjectCanon prototypes to {Array,Object}.prototype and null. #8260

Merged
merged 1 commit into from
May 18, 2021
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
4 changes: 3 additions & 1 deletion src/cache/inmemory/__tests__/object-canon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ describe("ObjectCanon", () => {
expect(canon.admit(c2)).toBe(c2);
});

it("preserves custom prototypes", () => {
// TODO Reenable this when ObjectCanon allows enabling canonization for
// arbitrary prototypes (not just {Array,Object}.prototype and null).
it.skip("preserves custom prototypes", () => {
const canon = new ObjectCanon;

class Custom {
Expand Down
12 changes: 6 additions & 6 deletions src/cache/inmemory/__tests__/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2044,11 +2044,11 @@ describe('reading from the store', () => {
}

const nonCanonicalQueryResult0 = readQuery(false);
expect(canon.isCanonical(nonCanonicalQueryResult0)).toBe(false);
expect(canon.isKnown(nonCanonicalQueryResult0)).toBe(false);
expect(nonCanonicalQueryResult0).toEqual({ count: 0 });

const canonicalQueryResult0 = readQuery(true);
expect(canon.isCanonical(canonicalQueryResult0)).toBe(true);
expect(canon.isKnown(canonicalQueryResult0)).toBe(true);
// The preservation of { count: 0 } proves the result didn't have to be
// recomputed, but merely canonized.
expect(canonicalQueryResult0).toEqual({ count: 0 });
Expand All @@ -2058,7 +2058,7 @@ describe('reading from the store', () => {
});

const canonicalQueryResult1 = readQuery(true);
expect(canon.isCanonical(canonicalQueryResult1)).toBe(true);
expect(canon.isKnown(canonicalQueryResult1)).toBe(true);
expect(canonicalQueryResult1).toEqual({ count: 1 });

const nonCanonicalQueryResult1 = readQuery(false);
Expand Down Expand Up @@ -2101,7 +2101,7 @@ describe('reading from the store', () => {
}

const canonicalFragmentResult1 = readFragment(true);
expect(canon.isCanonical(canonicalFragmentResult1)).toBe(true);
expect(canon.isKnown(canonicalFragmentResult1)).toBe(true);
expect(canonicalFragmentResult1).toEqual({ count: 0 });

const nonCanonicalFragmentResult1 = readFragment(false);
Expand All @@ -2115,13 +2115,13 @@ describe('reading from the store', () => {

const nonCanonicalFragmentResult2 = readFragment(false);
expect(readFragment(false)).toBe(nonCanonicalFragmentResult2);
expect(canon.isCanonical(nonCanonicalFragmentResult2)).toBe(false);
expect(canon.isKnown(nonCanonicalFragmentResult2)).toBe(false);
expect(nonCanonicalFragmentResult2).toEqual({ count: 1 });
expect(readFragment(false)).toBe(nonCanonicalFragmentResult2);

const canonicalFragmentResult2 = readFragment(true);
expect(readFragment(true)).toBe(canonicalFragmentResult2);
expect(canon.isCanonical(canonicalFragmentResult2)).toBe(true);
expect(canon.isKnown(canonicalFragmentResult2)).toBe(true);
expect(canonicalFragmentResult2).toEqual({ count: 1 });
});
});
1 change: 0 additions & 1 deletion src/cache/inmemory/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {

export const {
hasOwnProperty: hasOwn,
toString: objToStr,
} = Object.prototype;

export function getTypenameFromStoreObject(
Expand Down
11 changes: 6 additions & 5 deletions src/cache/inmemory/object-canon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Trie } from "@wry/trie";
import { canUseWeakMap } from "../../utilities";
import { objToStr } from "./helpers";

function isObjectOrArray(value: any): value is object {
return !!value && typeof value === "object";
Expand Down Expand Up @@ -82,7 +81,7 @@ export class ObjectCanon {
keys?: SortedKeysInfo;
}>(canUseWeakMap);

public isCanonical(value: any): boolean {
public isKnown(value: any): boolean {
return isObjectOrArray(value) && this.known.has(value);
}

Expand All @@ -106,8 +105,9 @@ export class ObjectCanon {
const original = this.passes.get(value);
if (original) return original;

switch (objToStr.call(value)) {
case "[object Array]": {
const proto = Object.getPrototypeOf(value);
switch (proto) {
case Array.prototype: {
if (this.known.has(value)) return value;
const array: any[] = (value as any[]).map(this.admit, this);
// Arrays are looked up in the Trie using their recursively
Expand All @@ -126,7 +126,8 @@ export class ObjectCanon {
return node.array;
}

case "[object Object]": {
case null:
case Object.prototype: {
Comment on lines -129 to +130
Copy link
Member Author

Choose a reason for hiding this comment

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

The case "[object Object]" from before would have canonized most user-defined classes (the ones that don't redefine Symbol.toStringTag), which feels dangerous to me, because not every class can be canonized (canonization requires the ability to copy the object safely, for example). A more complete solution will allow defining appropriate canonization logic for specific object types/prototypes, in addition to the defaults.

if (this.known.has(value)) return value;
const proto = Object.getPrototypeOf(value);
const array = [proto];
Expand Down
2 changes: 1 addition & 1 deletion src/cache/inmemory/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class StoreReader {
// If result is canonical, then it could only have been previously
// cached by the canonizing version of executeSelectionSet, so we can
// avoid checking both possibilities here.
this.canon.isCanonical(result),
this.canon.isKnown(result),
);
if (latest && result === latest.result) {
return true;
Expand Down