Skip to content

Commit

Permalink
fix: ignore requests for site icons
Browse files Browse the repository at this point in the history
  • Loading branch information
shetzel committed Jan 8, 2024
1 parent 7253cec commit aeab4a9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/webOAuthServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import { JwtOAuth2Config } from './org/authInfo';
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/core', 'auth');

// Server ignores requests for site icons
const iconPaths = ['/favicon.ico', '/apple-touch-icon-precomposed.png'];

/**
* Handles the creation of a web server for web based login flows.
*
Expand Down Expand Up @@ -186,6 +189,8 @@ export class WebOAuthServer extends AsyncCreatable<WebOAuthServer.Options> {
this.webServer.reportSuccess(response);
} else if (url.pathname === '/OauthError') {
this.webServer.reportError(this.oauthError, response);
} else if (iconPaths.includes(url.pathname ?? '')) {
this.logger.debug(`Ignoring request for icon path: ${url.pathname}`);
} else {
this.webServer.sendError(404, 'Resource not found', response);
const errName = 'invalidRequestUri';
Expand Down
67 changes: 67 additions & 0 deletions test/unit/webOauthServerTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,73 @@ describe('WebOauthServer', () => {
expect(reportErrorSpy.callCount).to.equal(1);
expect(reportErrorSpy.args[0][0]).to.equal(authError);
});

it('should ignore requests for favicon and continue', async () => {
const oauthServer = await WebOAuthServer.create({ oauthConfig: {} });
const validateStateStub = stubMethod($$.SANDBOX, oauthServer, 'validateState').returns(true);
await oauthServer.start();

// @ts-expect-error because private member
const webServer = oauthServer.webServer;
const reportSuccessSpy = spyMethod($$.SANDBOX, webServer, 'reportSuccess');

const origOn = webServer.server.on;
let requestListener: http.RequestListener;
stubMethod($$.SANDBOX, webServer.server, 'on').callsFake((event, callback) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return,@typescript-eslint/no-unsafe-argument
if (event !== 'request') return origOn.call(webServer.server, event, callback);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
requestListener = callback;
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
callback(
{
method: 'GET',
url: 'http://localhost:1717/favicon.ico',
},
{
setHeader: () => {},
writeHead: () => {},
end: () => {},
}
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
callback(
{
method: 'GET',
url: `http://localhost:1717/OauthRedirect?code=${authCode}&state=972475373f51`,
query: { code: authCode },
},
{
setHeader: () => {},
writeHead: () => {},
end: () => {},
}
);
});

// stub the redirect to ensure proper redirect handling and the web server is closed.
redirectStub = stubMethod($$.SANDBOX, webServer, 'doRedirect').callsFake(async (status, url, response) => {
expect(status).to.equal(303);
expect(url).to.equal('/OauthSuccess');
expect(response).to.be.ok;
// eslint-disable-next-line @typescript-eslint/await-thenable
await requestListener(
// @ts-expect-error
{ method: 'GET', url: `http://localhost:1717${url}` },
{
setHeader: () => {},
writeHead: () => {},
end: () => {},
}
);
});

const authInfo = await oauthServer.authorizeAndSave();
expect(authInfo.getFields()).to.deep.equal(authFields);
expect(redirectStub.callCount).to.equal(1);
expect(validateStateStub.callCount).to.equal(1);
expect(reportSuccessSpy.callCount).to.equal(1);
});
});

it('should error if postback has error', async () => {
Expand Down

3 comments on commit aeab4a9

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - ubuntu-latest

Benchmark suite Current: aeab4a9 Previous: 90d025d Ratio
Child logger creation 474612 ops/sec (±1.51%) 470001 ops/sec (±1.38%) 0.99
Logging a string on root logger 737360 ops/sec (±8.39%) 843426 ops/sec (±7.78%) 1.14
Logging an object on root logger 557383 ops/sec (±8.33%) 622345 ops/sec (±13.12%) 1.12
Logging an object with a message on root logger 12374 ops/sec (±198.73%) 23761 ops/sec (±184.91%) 1.92
Logging an object with a redacted prop on root logger 441129 ops/sec (±7.99%) 474599 ops/sec (±9.10%) 1.08
Logging a nested 3-level object on root logger 341289 ops/sec (±9.24%) 27204 ops/sec (±182.15%) 0.0797095716533495

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logger Benchmarks - windows-latest

Benchmark suite Current: aeab4a9 Previous: 90d025d Ratio
Child logger creation 348711 ops/sec (±0.51%) 339649 ops/sec (±0.47%) 0.97
Logging a string on root logger 847217 ops/sec (±7.54%) 829622 ops/sec (±7.98%) 0.98
Logging an object on root logger 645736 ops/sec (±5.96%) 569402 ops/sec (±7.41%) 0.88
Logging an object with a message on root logger 1663 ops/sec (±276.26%) 7007 ops/sec (±201.14%) 4.21
Logging an object with a redacted prop on root logger 442408 ops/sec (±14.56%) 456951 ops/sec (±7.14%) 1.03
Logging a nested 3-level object on root logger 332738 ops/sec (±6.85%) 322977 ops/sec (±5.76%) 0.97

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Logger Benchmarks - windows-latest'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: aeab4a9 Previous: 90d025d Ratio
Logging an object with a message on root logger 1663 ops/sec (±276.26%) 7007 ops/sec (±201.14%) 4.21

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.