Skip to content

Commit c4a9c21

Browse files
vuanhphungIsaac
andcommitted
fix: drain the /aad/auth redirect body during tenant discovery
The redirect response body was never read, so on the connection's keep-alive agent a large body could hold the socket. Drain it before reading the Location header. Also assert that discovery uses the connection's https agent (proxy and TLS options), and rename the pre-discovery tests. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
1 parent 8586347 commit c4a9c21

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎lib/connection/auth/DatabricksOAuth/AzureTenantDiscovery.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export async function discoverAzureTenantId(host: string, agent?: http.Agent): P
6262
);
6363
}
6464

65+
// Drain the (small) body so the keep-alive agent can reuse the socket.
66+
await response.text().catch(() => undefined);
6567
const location = response.headers.get('location');
6668
if (response.status < 300 || response.status >= 400 || !location) {
6769
throw new AuthenticationError(

‎tests/unit/connection/auth/DatabricksOAuth/AzureTenantDiscovery.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,28 @@ describe('discoverAzureTenantId', () => {
8282
expect(paths).to.deep.equal(['/aad/auth']);
8383
});
8484

85+
it('drains the redirect body so a keep-alive agent reuses the socket', async () => {
86+
respond = (req, res) => {
87+
res.writeHead(302, { Location: 'https://login.microsoftonline.com/tenant-xyz/oauth2/authorize' });
88+
// Larger than the stream buffers, so an undrained body would hold the only socket.
89+
res.end('x'.repeat(1024 * 1024));
90+
};
91+
let connections = 0;
92+
const onConnection = () => {
93+
connections += 1;
94+
};
95+
server.on('connection', onConnection);
96+
const agent = new http.Agent({ keepAlive: true, maxSockets: 1 });
97+
try {
98+
expect(await discoverAzureTenantId(base, agent)).to.equal('tenant-xyz');
99+
expect(await discoverAzureTenantId(base, agent)).to.equal('tenant-xyz');
100+
expect(connections).to.equal(1);
101+
} finally {
102+
server.off('connection', onConnection);
103+
agent.destroy();
104+
}
105+
});
106+
85107
it('fails when /aad/auth does not redirect', async () => {
86108
respond = (req, res) => {
87109
res.writeHead(200);

‎tests/unit/connection/auth/DatabricksOAuth/OAuthManager.test.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AssertionError, expect } from 'chai';
2+
import https from 'https';
23
import sinon, { SinonStub } from 'sinon';
34
import { Issuer, BaseClient, TokenSet, GrantBody, IssuerMetadata, ClientMetadata, custom } from 'openid-client';
45
// Import the whole module once more - to stub some of its exports
@@ -540,7 +541,7 @@ describe('AzureOAuthManager (tenant awareness)', () => {
540541
(mgr as unknown as Record<string, (...a: unknown[]) => T>)[name](...args);
541542

542543
describe('getOIDCConfigUrl', () => {
543-
it('falls back to /organizations/ when azureTenantId is not set (baseline-compatible)', () => {
544+
it('uses /organizations/ when azureTenantId is not set and no tenant was discovered yet', () => {
544545
const mgr = makeAzure();
545546
const url = call<string>(mgr, 'getOIDCConfigUrl');
546547
expect(url).to.equal('https://login.microsoftonline.com/organizations/v2.0/.well-known/openid-configuration');
@@ -553,7 +554,7 @@ describe('AzureOAuthManager (tenant awareness)', () => {
553554
expect(url).to.equal(`https://login.microsoftonline.com/${tenant}/v2.0/.well-known/openid-configuration`);
554555
});
555556

556-
it('falls back to /organizations/ when azureTenantId is empty or whitespace', () => {
557+
it('uses /organizations/ before discovery when azureTenantId is empty or whitespace', () => {
557558
for (const tenant of ['', ' ']) {
558559
const mgr = makeAzure({ azureTenantId: tenant });
559560
const url = call<string>(mgr, 'getOIDCConfigUrl');
@@ -632,13 +633,19 @@ describe('AzureOAuthManager tenant discovery', () => {
632633
discoverStub.restore();
633634
});
634635

636+
const httpsAgent = new https.Agent();
637+
635638
function makeManager(overrides: Partial<OAuthManagerOptions> = {}) {
639+
const context = new ClientContextStub();
640+
sinon
641+
.stub(context.connectionProvider, 'getAgent')
642+
.callsFake(async (url?: string | URL) => (url === 'https:' ? httpsAgent : undefined));
636643
return new AzureOAuthManager({
637644
host: 'adb-1234567890123456.1.azuredatabricks.net',
638645
flow: OAuthFlow.M2M,
639646
clientId: 'test_client_id',
640647
clientSecret: 'test_client_secret',
641-
context: new ClientContextStub(),
648+
context,
642649
...overrides,
643650
});
644651
}
@@ -649,6 +656,8 @@ describe('AzureOAuthManager tenant discovery', () => {
649656
await mgr.getToken();
650657
expect(discoverStub.calledOnce).to.be.true;
651658
expect(discoverStub.firstCall.args[0]).to.equal('adb-1234567890123456.1.azuredatabricks.net');
659+
// The connection's https agent carries the proxy and TLS options.
660+
expect(discoverStub.firstCall.args[1]).to.equal(httpsAgent);
652661
expect(oidcConfigUrls).to.deep.equal([
653662
'https://login.microsoftonline.com/discovered-tenant/v2.0/.well-known/openid-configuration',
654663
]);

0 commit comments

Comments
 (0)