Skip to content

Commit 1aed0e8

Browse files
authored
Defend against non-serializable params in invariantWrappers (#11861)
1 parent 29755da commit 1aed0e8

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

.changeset/little-weeks-join.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Defend against non-serializable params in `invariantWrappers`

.size-limits.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"dist/apollo-client.min.cjs": 39581,
3-
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32830
2+
"dist/apollo-client.min.cjs": 39604,
3+
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32852
44
}

src/utilities/globals/__tests__/invariantWrappers.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,25 @@ test("base invariant(false, 6, ...), raises fallback", async () => {
171171
)
172172
);
173173
});
174+
175+
test("base invariant(false, 6, ...) with non-serializable param", async () => {
176+
await using _ = mockErrorMessageHandler();
177+
178+
const obj: any = {};
179+
obj.self = obj;
180+
181+
expect(() => {
182+
invariant(false, 6, obj);
183+
}).toThrow(
184+
new InvariantError(
185+
"An error occurred! For more details, see the full error text at https://go.apollo.dev/c/err#" +
186+
encodeURIComponent(
187+
JSON.stringify({
188+
version: "local",
189+
message: 6,
190+
args: ["<non-serializable>"],
191+
})
192+
)
193+
)
194+
);
195+
});

src/utilities/globals/invariantWrappers.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,15 @@ declare global {
117117
}
118118

119119
function stringify(arg: any) {
120-
return typeof arg == "string" ? arg : (
121-
stringifyForDisplay(arg, 2).slice(0, 1000)
122-
);
120+
if (typeof arg == "string") {
121+
return arg;
122+
}
123+
124+
try {
125+
return stringifyForDisplay(arg, 2).slice(0, 1000);
126+
} catch {
127+
return "<non-serializable>";
128+
}
123129
}
124130

125131
function getHandledErrorMsg(

0 commit comments

Comments
 (0)