Skip to content

Commit 4093417

Browse files
authored
Merge pull request #913 from jonball4/sketchy-codec-protection
fix(superagent-wrapper): revert avoid mutation by sketchy codecs
2 parents 38b995d + a0dafb2 commit 4093417

File tree

2 files changed

+2
-71
lines changed

2 files changed

+2
-71
lines changed

packages/superagent-wrapper/src/request.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ const patchRequest = <
138138
});
139139
}
140140
return pipe(
141-
// deep copy to prevent modification of the inputs by sketchy codecs
142-
route.response[status].decode(structuredClone(res.body)),
141+
route.response[status].decode(res.body),
143142
E.map((body) =>
144143
decodedResponse<Route>({
145144
status,
@@ -195,8 +194,7 @@ export const requestForRoute =
195194
route: Route,
196195
): BoundRequestFactory<Req, Route> =>
197196
(params: h.RequestType<Route>): PatchedRequest<Req, Route> => {
198-
// deep copy to prevent modification of the inputs by sketchy codecs
199-
const reqProps = route.request.encode(structuredClone(params));
197+
const reqProps = route.request.encode(params);
200198

201199
let path = route.path;
202200
for (const key in reqProps.params) {

packages/superagent-wrapper/test/request.test.ts

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,6 @@ const PostTestRoute = h.httpRoute({
4545
},
4646
});
4747

48-
const PostOptionalTestRoute = h.httpRoute({
49-
path: '/test/optional/{id}',
50-
method: 'POST',
51-
request: h.httpRequest({
52-
query: {
53-
foo: t.string,
54-
},
55-
params: {
56-
id: NumberFromString,
57-
},
58-
body: {
59-
bar: t.number,
60-
optional: h.optionalized({
61-
baz: t.boolean,
62-
qux: h.optional(t.string),
63-
}),
64-
},
65-
}),
66-
response: {
67-
200: t.type({
68-
id: t.number,
69-
foo: t.string,
70-
bar: t.number,
71-
baz: t.boolean,
72-
}),
73-
401: t.type({
74-
message: t.string,
75-
}),
76-
},
77-
});
78-
7948
const HeaderGetTestRoute = h.httpRoute({
8049
path: '/getHeader',
8150
method: 'GET',
@@ -91,9 +60,6 @@ const TestRoutes = h.apiSpec({
9160
'api.v1.test': {
9261
post: PostTestRoute,
9362
},
94-
'api.v1.test.optional': {
95-
post: PostOptionalTestRoute,
96-
},
9763
'api.v1.getheader': {
9864
get: HeaderGetTestRoute,
9965
},
@@ -139,23 +105,6 @@ const createTestServer = (port: number) => {
139105
}
140106
});
141107

142-
testApp.post('/test/optional/:id', (req, res) => {
143-
const filteredReq = {
144-
query: req.query,
145-
params: req.params,
146-
headers: req.headers,
147-
body: req.body,
148-
};
149-
const params = E.getOrElseW((err) => {
150-
throw new Error(JSON.stringify(err));
151-
})(PostTestRoute.request.decode(filteredReq));
152-
const response = PostTestRoute.response[200].encode({
153-
...params,
154-
baz: true,
155-
});
156-
res.send(response);
157-
});
158-
159108
testApp.get(HeaderGetTestRoute.path, (req, res) => {
160109
res.send(
161110
HeaderGetTestRoute.response[200].encode({
@@ -293,22 +242,6 @@ describe('decodeExpecting', () => {
293242
'Could not decode response 200: [{"invalid":"response"}] due to error [Invalid value undefined supplied to : { id: number, foo: string, bar: number, baz: boolean }/id: number\nInvalid value undefined supplied to : { id: number, foo: string, bar: number, baz: boolean }/foo: string\nInvalid value undefined supplied to : { id: number, foo: string, bar: number, baz: boolean }/bar: number\nInvalid value undefined supplied to : { id: number, foo: string, bar: number, baz: boolean }/baz: boolean]',
294243
);
295244
});
296-
297-
it('does not modify inputs by dropping keys with undefined values', async () => {
298-
const body = {
299-
id: 1337,
300-
foo: 'test',
301-
bar: 42,
302-
optional: { baz: true, qux: undefined },
303-
};
304-
await apiClient['api.v1.test.optional'].post(body).decodeExpecting(200);
305-
assert.deepEqual(body, {
306-
id: 1337,
307-
foo: 'test',
308-
bar: 42,
309-
optional: { baz: true, qux: undefined },
310-
});
311-
});
312245
});
313246

314247
describe('superagent', async () => {

0 commit comments

Comments
 (0)