Skip to content

Commit bf6a214

Browse files
authored
Move from '{}' to 'Object.create(null)' in more places (#2877)
2 parents c4dafd4 + d07b68e commit bf6a214

File tree

15 files changed

+85
-54
lines changed

15 files changed

+85
-54
lines changed

.changeset/cozy-coats-serve.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"graphile-build-pg": patch
3+
"graphile-build": patch
4+
"graphile-utils": patch
5+
"postgraphile": patch
6+
"ruru-components": patch
7+
"graphile-export": patch
8+
"@dataplan/pg": patch
9+
"grafast": patch
10+
---
11+
12+
Safety - use null prototype objects in more places.

grafast/bench/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export async function bench(
6565
);
6666
const variableValues = variableValuesMatch
6767
? JSON5.parse(variableValuesMatch[1])
68-
: {};
68+
: Object.create(null);
6969
const checkForErrors = (
7070
result: ExecutionResult | ExecutionPatchResult,
7171
) => {

grafast/grafast/src/makeGrafastSchema.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export function makeGrafastSchema(details: GrafastSchemaConfig): GraphQLSchema {
299299
} else {
300300
// Hackily convert the new format into the old format. We'll do away with
301301
// this in future, but for now it's the easiest way to ensure compatibility
302-
plans = {};
302+
plans = Object.create(null);
303303

304304
const assertLocation = <
305305
TExpected extends
@@ -357,7 +357,7 @@ export function makeGrafastSchema(details: GrafastSchemaConfig): GraphQLSchema {
357357
};
358358
for (const [typeName, spec] of Object.entries(objects ?? {})) {
359359
const t = assertLocation(typeName, "objects");
360-
const o = {} as Record<string, any>;
360+
const o = Object.create(null) as Record<string, any>;
361361
plans[typeName] = o as any;
362362

363363
const { plans: planResolvers = {}, ...rest } = spec;
@@ -374,7 +374,7 @@ export function makeGrafastSchema(details: GrafastSchemaConfig): GraphQLSchema {
374374

375375
for (const [typeName, spec] of Object.entries(inputObjects ?? {})) {
376376
const t = assertLocation(typeName, "inputObjects");
377-
const o = {} as Record<string, any>;
377+
const o = Object.create(null) as Record<string, any>;
378378
plans[typeName] = o as any;
379379

380380
const { plans: planResolvers = {}, ...rest } = spec;
@@ -393,7 +393,7 @@ export function makeGrafastSchema(details: GrafastSchemaConfig): GraphQLSchema {
393393

394394
for (const [typeName, spec] of Object.entries(unions ?? {})) {
395395
assertLocation(typeName, "unions");
396-
const o = {} as Record<string, any>;
396+
const o = Object.create(null) as Record<string, any>;
397397
plans[typeName] = o as any;
398398

399399
for (const [key, val] of Object.entries(spec)) {
@@ -403,7 +403,7 @@ export function makeGrafastSchema(details: GrafastSchemaConfig): GraphQLSchema {
403403

404404
for (const [typeName, spec] of Object.entries(interfaces ?? {})) {
405405
assertLocation(typeName, "interfaces");
406-
const o = {} as Record<string, any>;
406+
const o = Object.create(null) as Record<string, any>;
407407
plans[typeName] = o as any;
408408

409409
for (const [key, val] of Object.entries(spec)) {
@@ -418,7 +418,7 @@ export function makeGrafastSchema(details: GrafastSchemaConfig): GraphQLSchema {
418418

419419
for (const [typeName, spec] of Object.entries(enums ?? {})) {
420420
const t = assertLocation(typeName, "enums");
421-
const o = {} as Record<string, any>;
421+
const o = Object.create(null) as Record<string, any>;
422422
plans[typeName] = o as any;
423423

424424
const { values = {}, ...rest } = spec;

grafast/grafast/src/steps/object.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,13 @@ export class ObjectStep<
151151
this.keys,
152152
tuple,
153153
);
154-
const newObj = this.keys.reduce((memo, key, i) => {
155-
memo[key] = tuple[i];
156-
return memo;
157-
}, {} as Partial<DataFromObjectSteps<TPlans>>) as DataFromObjectSteps<TPlans>;
154+
const newObj = this.keys.reduce(
155+
(memo, key, i) => {
156+
memo[key] = tuple[i];
157+
return memo;
158+
},
159+
Object.create(null) as Partial<DataFromObjectSteps<TPlans>>,
160+
) as DataFromObjectSteps<TPlans>;
158161
159162
// Cache newObj so the same tuple values result in the exact same object.
160163
meta.results.push([tuple, newObj]);

grafast/grafast/src/utils.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ export function objectSpec<
498498
);
499499
return o;
500500
},
501-
{} as GraphQLFieldConfigMap<any, Grafast.Context>,
501+
Object.create(null) as GraphQLFieldConfigMap<any, Grafast.Context>,
502502
);
503503
return modifiedFields;
504504
},
@@ -585,7 +585,7 @@ export function objectFieldSpec<
585585
};
586586
return memo;
587587
}, Object.create(null))
588-
: {};
588+
: Object.create(null);
589589

590590
return {
591591
...spec,
@@ -640,10 +640,13 @@ function inputObjectSpec<TParent>(
640640
fields: () => {
641641
const fields =
642642
typeof spec.fields === "function" ? spec.fields() : spec.fields;
643-
const modifiedFields = Object.keys(fields).reduce((o, key) => {
644-
o[key] = inputObjectFieldSpec(fields[key], `${spec.name}.${key}`);
645-
return o;
646-
}, {} as GraphQLInputFieldConfigMap);
643+
const modifiedFields = Object.keys(fields).reduce(
644+
(o, key) => {
645+
o[key] = inputObjectFieldSpec(fields[key], `${spec.name}.${key}`);
646+
return o;
647+
},
648+
Object.create(null) as GraphQLInputFieldConfigMap,
649+
);
647650
return modifiedFields;
648651
},
649652
};

grafast/ruru-components/src/hooks/useFetcher.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,11 @@ export const useFetcher = (
151151
}, [url]);
152152

153153
const fetcherOptions = useMemo<CreateFetcherOptions>(() => {
154-
const headers: Record<string, string> = explain
155-
? { "X-PostGraphile-Explain": "on", "X-GraphQL-Explain": "plan,sql" }
156-
: {};
154+
const headers: Record<string, string> = Object.create(null);
155+
if (explain) {
156+
headers["X-PostGraphile-Explain"] = "on";
157+
headers["X-GraphQL-Explain"] = "plan,sql";
158+
}
157159
return {
158160
url,
159161
headers,

graphile-build/graphile-build-pg/src/plugins/PgJWTPlugin.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,19 @@ export const PgJWTPlugin: GraphileConfig.Plugin = {
162162
serialize: EXPORTABLE(
163163
(attributeNames, pgJwtSecret, pgJwtSignOptions, signJwt) =>
164164
function serialize(value: any) {
165-
const token = attributeNames.reduce((memo, attributeName) => {
166-
if (attributeName === "exp") {
167-
memo[attributeName] = value[attributeName]
168-
? parseFloat(value[attributeName])
169-
: undefined;
170-
} else {
171-
memo[attributeName] = value[attributeName];
172-
}
173-
return memo;
174-
}, {} as any);
165+
const token = attributeNames.reduce(
166+
(memo, attributeName) => {
167+
if (attributeName === "exp") {
168+
memo[attributeName] = value[attributeName]
169+
? parseFloat(value[attributeName])
170+
: undefined;
171+
} else {
172+
memo[attributeName] = value[attributeName];
173+
}
174+
return memo;
175+
},
176+
Object.create(null) as any,
177+
);
175178
const options = Object.assign(
176179
Object.create(null),
177180
pgJwtSignOptions,

graphile-build/graphile-build-pg/src/plugins/PgOrderAllAttributesPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export const PgOrderAllAttributesPlugin: GraphileConfig.Plugin = {
253253
);
254254
return memo;
255255
},
256-
{} as GraphQLEnumValueConfigMap,
256+
Object.create(null) as GraphQLEnumValueConfigMap,
257257
),
258258
`Adding order values from table '${pgCodec.name}'`,
259259
);

graphile-build/graphile-build/src/behavior.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export class Behavior {
189189
const behaviorString = key as keyof GraphileBuild.BehaviorStrings;
190190
if (!this.behaviorRegistry[behaviorString]) {
191191
this.behaviorRegistry[behaviorString] = {
192-
entities: {},
192+
entities: Object.create(null),
193193
};
194194
}
195195
const { description } = spec;

graphile-build/graphile-build/src/extend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default function extend<
2626
if (isDev && (Array.isArray(base) || Array.isArray(extra))) {
2727
throw new Error(`Do not extend arrays!`);
2828
}
29-
const hints = base[$$hints] || {};
29+
const hints = base[$$hints] || Object.create(null);
3030

3131
const keysB = Object.keys(extra);
3232
const extraHints = extra[$$hints] || {};

0 commit comments

Comments
 (0)