Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(twilio-run): handle adding object as header correctly as an error #526

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading