Skip to content

Commit

Permalink
fix(twilio-run): handle adding object as header correclty as an error (
Browse files Browse the repository at this point in the history
…#526)

handle adding object as header correclty as an error
  • Loading branch information
makserik authored Dec 11, 2024
1 parent ca64521 commit d43ab63
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/three-gifts-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@twilio/runtime-handler': minor
'twilio-run': minor
---

handle adding object as header correctly as an error
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ test('sets headers with string cookies', () => {
expect(response['headers']).toEqual(expected);
});

test('object cant be a header', () => {
const response = new Response();
expect(response['headers']).toEqual({
'Set-Cookie': [],
});

expect(() => {
response.appendHeader('Access-Control-Allow-Origin', {} as any);
}).toThrow('Header value cannot be an object');
});

test('sets headers with an array of cookies', () => {
const response = new Response();
expect(response['headers']).toEqual({
Expand Down
5 changes: 5 additions & 0 deletions packages/runtime-handler/src/dev-runtime/internal/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ export class Response implements TwilioResponse {
appendHeader(key: string, value: HeaderValue): Response {
log('Appending header for %s', key, value);
this.headers = this.headers || {};

if (typeof value === 'object' && !Array.isArray(value)) {
throw new Error('Header value cannot be an object');
}

let newHeaderValue: HeaderValue = [];
if (key.toLowerCase() === COOKIE_HEADER.toLowerCase()) {
const existingValue = this.headers[COOKIE_HEADER];
Expand Down
11 changes: 11 additions & 0 deletions packages/twilio-run/__tests__/runtime/internal/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ test('appends a new header correctly', () => {
});
});

test('object cant be a header', () => {
const response = new Response();
expect(response['headers']).toEqual({
'Set-Cookie': [],
});

expect(() => {
response.appendHeader('Access-Control-Allow-Origin', {} as any);
}).toThrow('Header value cannot be an object');
});

test('appends a header correctly with no existing one', () => {
const response = new Response();
expect(response['headers']).toEqual({
Expand Down
3 changes: 3 additions & 0 deletions packages/twilio-run/src/runtime/internal/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export class Response implements TwilioResponse {

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

0 comments on commit d43ab63

Please sign in to comment.