-
Notifications
You must be signed in to change notification settings - Fork 29
feat: improve HTTP redirects behavior #262
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
6595367
d8f96e5
aae3f89
4773399
b385b34
71eb245
f85d30e
57c91e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| /* eslint-disable class-methods-use-this */ | ||
| /* eslint-disable class-methods-use-this, no-restricted-syntax */ | ||
|
|
||
| /** | ||
| * (C) Copyright IBM Corp. 2014, 2023. | ||
|
|
@@ -238,6 +238,20 @@ export class RequestWrapper { | |
| data = await this.gzipRequestBody(data, headers); | ||
| } | ||
|
|
||
| // Holds the hostname of the current request. | ||
| // It's used between redirects. | ||
| let currentHost = new URL(url).hostname; | ||
|
|
||
| // Headers that are considered being safe | ||
| // and can be copied during HTTP redirects | ||
| // on the `cloud.ibm.com` namespace. | ||
| const safeHeaders = { | ||
| Authorization: headers.Authorization, | ||
| Cookie: headers.Cookie, | ||
| Cookie2: headers.Cookie2, | ||
| 'WWW-Authenticate': headers['WWW-Authenticate'], | ||
| }; | ||
|
|
||
| const requestParams = { | ||
| url, | ||
| method, | ||
|
|
@@ -247,6 +261,21 @@ export class RequestWrapper { | |
| raxConfig: this.raxConfig, | ||
| responseType: options.responseType || 'json', | ||
| paramsSerializer: { serialize: (params) => stringify(params) }, | ||
| maxRedirects: 10, | ||
| beforeRedirect: (nextRequest: any) => { | ||
| if (shouldCopySafeHeadersOnRedirect(currentHost, nextRequest.hostname)) { | ||
| for (const [name, value] of Object.entries(safeHeaders)) { | ||
| // Copy the header to the redirected request if it's defined | ||
| // and not already present. | ||
| if (value && nextRequest.headers[name] === undefined) { | ||
| nextRequest.headers[name] = value; | ||
| } | ||
| } | ||
|
|
||
| // Update the host for the next redirect. | ||
| currentHost = nextRequest.hostname; | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| return this.axiosInstance(requestParams).then( | ||
|
|
@@ -580,3 +609,8 @@ function ensureJSONResponseBodyIsObject(response: any): any | string { | |
|
|
||
| return dataAsObject; | ||
| } | ||
|
|
||
| // Returns true iff safe headers should be copied to a redirected request. | ||
| function shouldCopySafeHeadersOnRedirect(fromHost: string, toHost: string): boolean { | ||
| return fromHost.endsWith('.cloud.ibm.com') && toHost.endsWith('.cloud.ibm.com'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic should work as long as we are also supporting the scenario where a redirection to the same host also results in the safe headers being copied, which I assume is the case since you're not completely overriding axios' redirection behavior (presumably) :)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those scenarios are covered by the unit tests now and yes, we are not overriding the whole redirection behavior, it's just a sort of post-processing in the chain, before sending the next, redirected request. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| /** | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to add tests for redirects to the same host:
|
||
| * Copyright 2023 IBM Corp. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| const nock = require('nock'); | ||
| const { AxiosError } = require('axios'); | ||
| const { NoAuthAuthenticator, BaseService } = require('../../dist'); | ||
|
|
||
| // Disable real network connections. | ||
| nock.disableNetConnect(); | ||
|
|
||
| const safeHeaders = { | ||
| Authorization: 'foo', | ||
| Cookie: 'baz', | ||
| Cookie2: 'baz2', | ||
| 'WWW-Authenticate': 'bar', | ||
| }; | ||
|
|
||
| function initService(url) { | ||
| const service = new BaseService({ | ||
| authenticator: new NoAuthAuthenticator(), | ||
| serviceUrl: url, | ||
| }); | ||
|
|
||
| const parameters = { | ||
| options: { | ||
| method: 'GET', | ||
| url: '/', | ||
| headers: safeHeaders, | ||
| }, | ||
| defaultOptions: { | ||
| serviceUrl: url, | ||
| }, | ||
| }; | ||
|
|
||
| return { service, parameters }; | ||
| } | ||
|
|
||
| describe('Node Core redirects', () => { | ||
| afterEach(() => { | ||
| nock.cleanAll(); | ||
| }); | ||
|
|
||
| it('should include safe headers within cloud.ibm.com domain', async () => { | ||
| const url1 = 'http://region1.cloud.ibm.com'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's use https in these URLs :)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed! I also change the protocols in my Python core PR, because I used HTTP there too. |
||
| const url2 = 'http://region2.cloud.ibm.com'; | ||
|
|
||
| const { service, parameters } = initService(url1); | ||
|
|
||
| const scopes = [ | ||
| nock(url1) | ||
| .matchHeader('Authorization', safeHeaders.Authorization) | ||
| .matchHeader('Cookie', safeHeaders.Cookie) | ||
| .matchHeader('Cookie2', safeHeaders.Cookie2) | ||
| .matchHeader('WWW-Authenticate', safeHeaders['WWW-Authenticate']) | ||
| .get('/') | ||
| .reply(302, 'just about to redirect', { Location: url2 }), | ||
| nock(url2) | ||
| .matchHeader('Authorization', safeHeaders.Authorization) | ||
| .matchHeader('Cookie', safeHeaders.Cookie) | ||
| .matchHeader('Cookie2', safeHeaders.Cookie2) | ||
| .matchHeader('WWW-Authenticate', safeHeaders['WWW-Authenticate']) | ||
| .get('/') | ||
| .reply(200, 'successfully redirected'), | ||
| ]; | ||
|
|
||
| const result = await service.createRequest(parameters); | ||
| expect(result.result).toBe('successfully redirected'); | ||
| expect(result.status).toBe(200); | ||
|
|
||
| // Ensure all mocks satisfied. | ||
| scopes.forEach((s) => s.done()); | ||
| }); | ||
|
|
||
| it('should exclude safe headers from cloud.ibm.com to not cloud.ibm.com domain', async () => { | ||
| const url1 = 'http://region1.cloud.ibm.com'; | ||
| const url2 = 'http://region2.notcloud.ibm.com'; | ||
|
|
||
| const { service, parameters } = initService(url1); | ||
|
|
||
| const scopes = [ | ||
| nock(url1) | ||
| .matchHeader('Authorization', safeHeaders.Authorization) | ||
| .matchHeader('Cookie', safeHeaders.Cookie) | ||
| .matchHeader('Cookie2', safeHeaders.Cookie2) | ||
| .matchHeader('WWW-Authenticate', safeHeaders['WWW-Authenticate']) | ||
| .get('/') | ||
| .reply(302, 'just about to redirect', { Location: url2 }), | ||
| nock(url2) | ||
| .matchHeader('Authorization', (val) => val === undefined) | ||
| .matchHeader('Cookie', (val) => val === undefined) | ||
| .matchHeader('Cookie2', safeHeaders.Cookie2) | ||
| .matchHeader('WWW-Authenticate', safeHeaders['WWW-Authenticate']) | ||
| .get('/') | ||
| .reply(200, 'successfully redirected'), | ||
| ]; | ||
|
|
||
| const result = await service.createRequest(parameters); | ||
|
|
||
| expect(result.result).toBe('successfully redirected'); | ||
| expect(result.status).toBe(200); | ||
|
|
||
| // Ensure all mocks satisfied. | ||
| scopes.forEach((s) => s.done()); | ||
| }); | ||
|
|
||
| it('should exclude safe headers from not cloud.ibm.com to cloud.ibm.com domain', async () => { | ||
| const url1 = 'http://region2.notcloud.ibm.com'; | ||
| const url2 = 'http://region1.cloud.ibm.com'; | ||
|
|
||
| const { service, parameters } = initService(url1); | ||
|
|
||
| const scopes = [ | ||
| nock(url1) | ||
| .matchHeader('Authorization', safeHeaders.Authorization) | ||
| .matchHeader('Cookie', safeHeaders.Cookie) | ||
| .matchHeader('Cookie2', safeHeaders.Cookie2) | ||
| .matchHeader('WWW-Authenticate', safeHeaders['WWW-Authenticate']) | ||
| .get('/') | ||
| .reply(302, 'just about to redirect', { Location: url2 }), | ||
| nock(url2) | ||
| .matchHeader('Authorization', (val) => val === undefined) | ||
| .matchHeader('Cookie', (val) => val === undefined) | ||
| .matchHeader('Cookie2', safeHeaders.Cookie2) | ||
| .matchHeader('WWW-Authenticate', safeHeaders['WWW-Authenticate']) | ||
| .get('/') | ||
| .reply(200, 'successfully redirected'), | ||
| ]; | ||
|
|
||
| const result = await service.createRequest(parameters); | ||
|
|
||
| expect(result.result).toBe('successfully redirected'); | ||
| expect(result.status).toBe(200); | ||
|
|
||
| // Ensure all mocks satisfied. | ||
| scopes.forEach((s) => s.done()); | ||
| }); | ||
|
|
||
| it('should exclude safe headers from not cloud.ibm.com to not cloud.ibm.com domain', async () => { | ||
| const url1 = 'http://region1.notcloud.ibm.com'; | ||
| const url2 = 'http://region2.notcloud.ibm.com'; | ||
|
|
||
| const { service, parameters } = initService(url1); | ||
|
|
||
| const scopes = [ | ||
| nock(url1) | ||
| .matchHeader('Authorization', safeHeaders.Authorization) | ||
| .matchHeader('Cookie', safeHeaders.Cookie) | ||
| .matchHeader('Cookie2', safeHeaders.Cookie2) | ||
| .matchHeader('WWW-Authenticate', safeHeaders['WWW-Authenticate']) | ||
| .get('/') | ||
| .reply(302, 'just about to redirect', { Location: url2 }), | ||
| nock(url2) | ||
| .matchHeader('Authorization', (val) => val === undefined) | ||
| .matchHeader('Cookie', (val) => val === undefined) | ||
| .matchHeader('Cookie2', safeHeaders.Cookie2) | ||
| .matchHeader('WWW-Authenticate', safeHeaders['WWW-Authenticate']) | ||
| .get('/') | ||
| .reply(200, 'successfully redirected'), | ||
| ]; | ||
|
|
||
| const result = await service.createRequest(parameters); | ||
|
|
||
| expect(result.result).toBe('successfully redirected'); | ||
| expect(result.status).toBe(200); | ||
|
|
||
| // Ensure all mocks satisfied. | ||
| scopes.forEach((s) => s.done()); | ||
| }); | ||
|
|
||
| it('should fail due to exhaustion', async () => { | ||
| const scopes = []; | ||
| for (let i = 1; i <= 11; i++) { | ||
| scopes.push( | ||
| nock(`http://region${i}.cloud.ibm.com`) | ||
| .matchHeader('Authorization', safeHeaders.Authorization) | ||
| .matchHeader('Cookie', safeHeaders.Cookie) | ||
| .matchHeader('Cookie2', safeHeaders.Cookie2) | ||
| .matchHeader('WWW-Authenticate', safeHeaders['WWW-Authenticate']) | ||
| .get('/') | ||
| .reply(302, 'just about to redirect', { Location: `http://region${i + 1}.cloud.ibm.com` }) | ||
| ); | ||
| } | ||
|
|
||
| const { service, parameters } = initService('http://region1.cloud.ibm.com'); | ||
|
|
||
| let result; | ||
| let error; | ||
| try { | ||
| result = await service.createRequest(parameters); | ||
| } catch (err) { | ||
| error = err; | ||
| } | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| expect(error).not.toBeUndefined(); | ||
| expect(error.statusText).toBe(AxiosError.ERR_FR_TOO_MANY_REDIRECTS); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch!