Skip to content

Commit f9e2145

Browse files
musalesebastienlevertgavinbarron
authored
fix: remove the SdkVersion header when redirected to a non-graph endpoint (#1947)
This happens during large file upload as some endpoints throw a CORS error when the SdkVersion header is attached. This change deletes the header from the request object when the request endpoint is a non-graph endpoint. Co-authored-by: Sébastien Levert <[email protected]> Co-authored-by: Gavin Barron <[email protected]>
1 parent ae3c0b1 commit f9e2145

File tree

3 files changed

+67
-22
lines changed

3 files changed

+67
-22
lines changed

packages/mgt-element/src/IGraph.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,24 @@ export interface IGraph {
6666
*/
6767
createBatch(): IBatch;
6868
}
69+
70+
export type GraphEndpoint =
71+
| 'https://graph.microsoft.com'
72+
| 'https://graph.microsoft.us'
73+
| 'https://dod-graph.microsoft.us'
74+
| 'https://graph.microsoft.de'
75+
| 'https://microsoftgraph.chinacloudapi.cn';
76+
77+
export const MICROSOFT_GRAPH_ENDPOINTS: Set<GraphEndpoint> = new Set<GraphEndpoint>();
78+
export const MICROSOFT_GRAPH_DEFAULT_ENDPOINT: GraphEndpoint = 'https://graph.microsoft.com';
79+
80+
(() => {
81+
const endpoints: GraphEndpoint[] = [
82+
MICROSOFT_GRAPH_DEFAULT_ENDPOINT,
83+
'https://graph.microsoft.us',
84+
'https://dod-graph.microsoft.us',
85+
'https://graph.microsoft.de',
86+
'https://microsoftgraph.chinacloudapi.cn'
87+
];
88+
endpoints.forEach(endpoint => MICROSOFT_GRAPH_ENDPOINTS.add(endpoint));
89+
})();

packages/mgt-element/src/utils/GraphHelpers.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import { AuthenticationHandlerOptions, Middleware } from '@microsoft/microsoft-graph-client';
9-
import { Providers } from '..';
9+
import { GraphEndpoint, MICROSOFT_GRAPH_ENDPOINTS, Providers } from '..';
1010

1111
/**
1212
* creates an AuthenticationHandlerOptions from scopes array that
@@ -46,3 +46,20 @@ export function chainMiddleware(...middleware: Middleware[]): Middleware {
4646
}
4747
return rootMiddleware;
4848
}
49+
50+
/**
51+
* Helper method to validate a base URL string
52+
* @param url a URL string
53+
* @returns GraphEndpoint
54+
*/
55+
export function validateBaseURL(url: string): GraphEndpoint {
56+
try {
57+
const urlObj = new URL(url);
58+
const originAsEndpoint = urlObj.origin as GraphEndpoint;
59+
if (MICROSOFT_GRAPH_ENDPOINTS.has(originAsEndpoint)) {
60+
return originAsEndpoint;
61+
}
62+
} catch (error) {
63+
return;
64+
}
65+
}

packages/mgt-element/src/utils/SdkVersionMiddleware.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { Context, Middleware } from '@microsoft/microsoft-graph-client';
99
import { getRequestHeader, setRequestHeader } from '@microsoft/microsoft-graph-client/lib/es/middleware/MiddlewareUtil';
1010
import { ComponentMiddlewareOptions } from './ComponentMiddlewareOptions';
11+
import { validateBaseURL } from './GraphHelpers';
1112

1213
/**
1314
* Implements Middleware for the Graph sdk to inject
@@ -33,33 +34,39 @@ export class SdkVersionMiddleware implements Middleware {
3334
// tslint:disable-next-line: completed-docs
3435
public async execute(context: Context): Promise<void> {
3536
try {
36-
// Header parts must follow the format: 'name/version'
37-
const headerParts: string[] = [];
37+
if (typeof context.request === 'string') {
38+
if (validateBaseURL(context.request)) {
39+
// Header parts must follow the format: 'name/version'
40+
const headerParts: string[] = [];
3841

39-
const componentOptions = context.middlewareControl.getMiddlewareOptions(
40-
ComponentMiddlewareOptions
41-
) as ComponentMiddlewareOptions;
42+
const componentOptions = context.middlewareControl.getMiddlewareOptions(
43+
ComponentMiddlewareOptions
44+
) as ComponentMiddlewareOptions;
4245

43-
if (componentOptions) {
44-
const componentVersion: string = `${componentOptions.componentName}/${this._packageVersion}`;
45-
headerParts.push(componentVersion);
46-
}
46+
if (componentOptions) {
47+
const componentVersion: string = `${componentOptions.componentName}/${this._packageVersion}`;
48+
headerParts.push(componentVersion);
49+
}
4750

48-
if (this._providerName) {
49-
const providerVersion: string = `${this._providerName}/${this._packageVersion}`;
50-
headerParts.push(providerVersion);
51-
}
51+
if (this._providerName) {
52+
const providerVersion: string = `${this._providerName}/${this._packageVersion}`;
53+
headerParts.push(providerVersion);
54+
}
5255

53-
// Package version
54-
const packageVersion: string = `mgt/${this._packageVersion}`;
55-
headerParts.push(packageVersion);
56+
// Package version
57+
const packageVersion: string = `mgt/${this._packageVersion}`;
58+
headerParts.push(packageVersion);
5659

57-
// Existing SdkVersion header value
58-
headerParts.push(getRequestHeader(context.request, context.options, 'SdkVersion'));
60+
// Existing SdkVersion header value
61+
headerParts.push(getRequestHeader(context.request, context.options, 'SdkVersion'));
5962

60-
// Join the header parts together and update the SdkVersion request header value
61-
const sdkVersionHeaderValue = headerParts.join(', ');
62-
setRequestHeader(context.request, context.options, 'SdkVersion', sdkVersionHeaderValue);
63+
// Join the header parts together and update the SdkVersion request header value
64+
const sdkVersionHeaderValue = headerParts.join(', ');
65+
setRequestHeader(context.request, context.options, 'SdkVersion', sdkVersionHeaderValue);
66+
} else {
67+
delete context?.options?.headers['SdkVersion'];
68+
}
69+
}
6370
} catch (error) {
6471
// ignore error
6572
}

0 commit comments

Comments
 (0)