microsoft-authentication-library-for-js: proxyUrl option doesn't work behind a corporate proxy
Core Library
MSAL Node (@azure/msal-node)
Core Library Version
1.18.3
Wrapper Library
Not Applicable
Wrapper Library Version
N/A
Public or Confidential Client?
Confidential
Description
For the past year or so, I have been using msal-node behind a corporate proxy with the workaround posted by another user here (overriding the networkClient with proxy-compatible versions of sendGetRequestAsync and sendPostRequestAsync). This has been working well, and now that msal-node natively supports proxies I wanted to try it out. Unfortunately, passing the proxyUrl parameter and disabling the custom networkClient yields an Endpoints Resolution Error
ClientAuthError: endpoints_resolution_error: Error: could not resolve endpoints. Please check network and try again. Detail: ClientAuthError: openid_config_error: Could not retrieve endpoints. Check your authority and verify the .well-known/openid-configuration endpoint returns the required endpoints. Attempted to retrieve endpoints from: https://login.microsoftonline.com/my tenant/v2.0/.well-known/openid-configuration
I investigated msal-nodes implementation of proxy compatibility to see if I could figure out the issue. Connection to the corporate proxy seemed to succeed. Log statements I put in the networkRequestViaProxy function showed that connection to the proxy was succeeding with 200 Connection established However, accessing microsoft’s login servers seemed to be yielding a 400 Bad Request.
{
headers: {
'Content-Type': 'text/html',
'Cache-Control': 'no-cache',
'Content-Length': '2488',
'X-Frame-Options': 'deny',
'Proxy-Connection': 'Close'
},
body: {
error: 'client_error',
error_description: 'A client error occured.\n' +
'Http status code: 400\n' +
'Http status message: badrequest\n' +
'Headers: {"Content-Type":"text/html","Cache-Control":"no-cache","Content-Length":"2488","X-Frame-Options":"deny","Proxy-Connection":"Close"}'
},
status: 400
}
And these are the contents of the payload variable in that function:
GET https://login.microsoftonline.com/my tenant/v2.0/.well-known/openid-configuration HTTP/1.1
Host: login.microsoftonline.com
Connection: close
The existing proxy implementation using http.request and manually writing http requests through the socket seems overly complex and dated and doesnt support the Proxy-Authorization header - it would be great if it were rewritten to use fetch and https-proxy-agent. They’re far newer and cleaner, and it’s how the MS Graph Client implements proxy support. I’ve never had a single issue with the Graph Clients proxy support.
Error Message
ClientAuthError: endpoints_resolution_error: Error: could not resolve endpoints. Please check network and try again. Detail: ClientAuthError: openid_config_error: Could not retrieve endpoints. Check your authority and verify the .well-known/openid-configuration endpoint returns the required endpoints. Attempted to retrieve endpoints from: https://login.microsoftonline.com/my tenant/v2.0/.well-known/openid-configuration
Msal Logs
No response
MSAL Configuration
const clientConfig = {
auth: {
clientId: vault.azure_ad_client_id,
authority: 'https://login.microsoftonline.com/my tenant/',
clientCertificate: {
thumbprint: vault.azure_ad_cert_thumbprint,
privateKey: Buffer.from(vault.azure_ad_decrypted_private_key_base64, 'base64').toString(),
},
},
system: {
//networkClient: proxyClient,
proxyUrl: 'http://host:port'
},
};
Relevant Code Snippets
//This is the implementation of the networkClient that works through the proxy without any issues
import fetch from 'node-fetch';
import { HttpsProxyAgent } from 'https-proxy-agent';
export const proxyUrl = 'http://host:port';
export const proxyAgent = new HttpsProxyAgent(proxyUrl);
export const proxyClient = {
sendGetRequestAsync: async (url, options) => {
const response = await fetch(url, { agent: proxyAgent, ...options });
const json = await response.json();
const headers = response.headers.raw();
const obj = {
headers: Object.create(Object.prototype, headers),
body: json,
status: response.status,
};
return obj;
},
sendPostRequestAsync: async (url, options) => {
const sendingOptions = options || {};
sendingOptions.method = 'post';
const response = await fetch(url, { agent: proxyAgent, ...sendingOptions });
const json = await response.json();
const headers = response.headers.raw();
const obj = {
headers: Object.create(Object.prototype, headers),
body: json,
status: response.status,
};
return obj;
},
};
Reproduction Steps
- Setup a proxy
- Attempt to use msal-node with proxyUrl
Expected Behavior
The expected behavior is the 200 Ok response and msal-node being able to fetch the token
Identity Provider
Azure AD / MSA
Browsers Affected (Select all that apply)
None (Server), Other
Regression
No response
Source
External (Customer)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 16 (6 by maintainers)
@Robbie-Microsoft I had initially tried it with the latest version, 1.5.1. I just retested it with 1.4.0 and still get the endpoints resolution error. I understand not wanting to rely on third party libraries, but msal-node and the MS Graph Client having such disparate proxy support is pretty frustrating. The graph client as it is currently uses a fetch polyfill and is compatible with https-proxy-agent.
If there’s unlikely to be any major change until Node’s native fetch supports proxies, then could I suggest a happy medium in the meantime? Currently, msal-node doesn’t support
https-proxy-agentpassed intocustomAgentOptionsbecause innetworkRequestViaHttps, the contents ofcustomAgentOptionsare presumed to be the options parameter of the nativehttp(s).Agentclass and passed into it’s constructor, rather than being treated as an Agent object itself.https.requestas you are using it, when used withhttps-proxy-agent, has complete proxy support. If I simply changecustomOptions.agent = new https.Agent(agentOptions);tocustomOptions.agent = agentOptionsinHttpClient, and then in my config pass anhttps-proxy-agentobject as such:I’m able to successfully connect through the proxy. To preserve backwards compatibility, something like this can be done:
This way, we check if the end user passed in their own custom agent object, and if they did, we use that. If they just passed in a dictionary of options, we create the Agent object for them and use that. We check if it’s either an http or https agent, because both are valid in
https.request(https-proxy-agentfor some reason is a child ofhttp.Agent.) This ensures that anyone can use their own proxy agent, backwards compatibility is preserved, and msal-node would have relatively easy proxy support with no extra third parties. The burden of using a third party proxy agent library falls on us end users. Simply doingnpm i https-proxy-agentand passing the object intocustomAgentOptionsis much easier than the status quo.On a related note, if you’re using Node 18+, native fetch breaks node-fetch and necessitates the usage of
--no-experimental-fetchWorking INetworkModule implementation with Proxy
TROUBLESHOOTING