Skip to content

Commit d43ab63

Browse files
authored
fix(twilio-run): handle adding object as header correclty as an error (#526)
handle adding object as header correclty as an error
1 parent ca64521 commit d43ab63

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

.changeset/three-gifts-smash.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@twilio/runtime-handler': minor
3+
'twilio-run': minor
4+
---
5+
6+
handle adding object as header correctly as an error

packages/runtime-handler/__tests__/dev-runtime/internal/response.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ test('sets headers with string cookies', () => {
8484
expect(response['headers']).toEqual(expected);
8585
});
8686

87+
test('object cant be a header', () => {
88+
const response = new Response();
89+
expect(response['headers']).toEqual({
90+
'Set-Cookie': [],
91+
});
92+
93+
expect(() => {
94+
response.appendHeader('Access-Control-Allow-Origin', {} as any);
95+
}).toThrow('Header value cannot be an object');
96+
});
97+
8798
test('sets headers with an array of cookies', () => {
8899
const response = new Response();
89100
expect(response['headers']).toEqual({

packages/runtime-handler/src/dev-runtime/internal/response.ts

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ export class Response implements TwilioResponse {
7070
appendHeader(key: string, value: HeaderValue): Response {
7171
log('Appending header for %s', key, value);
7272
this.headers = this.headers || {};
73+
74+
if (typeof value === 'object' && !Array.isArray(value)) {
75+
throw new Error('Header value cannot be an object');
76+
}
77+
7378
let newHeaderValue: HeaderValue = [];
7479
if (key.toLowerCase() === COOKIE_HEADER.toLowerCase()) {
7580
const existingValue = this.headers[COOKIE_HEADER];

packages/twilio-run/__tests__/runtime/internal/response.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ test('appends a new header correctly', () => {
8585
});
8686
});
8787

88+
test('object cant be a header', () => {
89+
const response = new Response();
90+
expect(response['headers']).toEqual({
91+
'Set-Cookie': [],
92+
});
93+
94+
expect(() => {
95+
response.appendHeader('Access-Control-Allow-Origin', {} as any);
96+
}).toThrow('Header value cannot be an object');
97+
});
98+
8899
test('appends a header correctly with no existing one', () => {
89100
const response = new Response();
90101
expect(response['headers']).toEqual({

packages/twilio-run/src/runtime/internal/response.ts

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ export class Response implements TwilioResponse {
6767

6868
appendHeader(key: string, value: HeaderValue): Response {
6969
debug('Appending header for %s', key, value);
70+
if (typeof value === 'object' && !Array.isArray(value)) {
71+
throw new Error('Header value cannot be an object');
72+
}
7073
this.headers = this.headers || {};
7174
let newHeaderValue: HeaderValue = [];
7275
if (key.toLowerCase() === COOKIE_HEADER.toLowerCase()) {

0 commit comments

Comments
 (0)