Skip to content

Commit dc4a0b3

Browse files
committed
unit test for invalid hostname
1 parent aa2c034 commit dc4a0b3

2 files changed

Lines changed: 46 additions & 11 deletions

File tree

src/GraphRequestUtil.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ export const isGraphURL = (url: string): boolean => {
7272
/**
7373
* Checks if the url is for one of the custom hosts provided during client initialization
7474
* @param {string} url - The url to be verified
75-
* @param {Set} customHostNames - The url to be verified
75+
* @param {Set} customHosts - The url to be verified
7676
* @returns {boolean} - Returns true if the url is a for a custom host
7777
*/
78-
export const isCustomHost = (url: string, customHostNames: Set<string>): boolean => {
79-
customHostNames.forEach((x) => isCustomHostValid(x));
80-
return isValidEndpoint(url, customHostNames);
78+
export const isCustomHost = (url: string, customHosts: Set<string>): boolean => {
79+
customHosts.forEach((host) => isCustomHostValid(host));
80+
return isValidEndpoint(url, customHosts);
8181
};
8282

8383
/**
8484
* Checks if the url is for one of the provided hosts.
8585
* @param {string} url - The url to be verified
86-
* @param {Set<string>} allowedHostNames - A set of hostnames.
86+
* @param {Set<string>} allowedHosts - A set of hosts.
8787
* @returns {boolean} - Returns true is for one of the provided endpoints.
8888
*/
89-
const isValidEndpoint = (url: string, allowedHostNames: Set<string> = GRAPH_URLS): boolean => {
89+
const isValidEndpoint = (url: string, allowedHosts: Set<string> = GRAPH_URLS): boolean => {
9090
// Valid Graph URL pattern - https://graph.microsoft.com/{version}/{resource}?{query-parameters}
9191
// Valid Graph URL example - https://graph.microsoft.com/v1.0/
9292
url = url.toLowerCase();
@@ -101,19 +101,23 @@ const isValidEndpoint = (url: string, allowedHostNames: Set<string> = GRAPH_URLS
101101
if (endOfHostStrPos !== -1) {
102102
if (startofPortNoPos !== -1 && startofPortNoPos < endOfHostStrPos) {
103103
hostName = url.substring(0, startofPortNoPos);
104-
return allowedHostNames.has(hostName);
104+
return allowedHosts.has(hostName);
105105
}
106106
// Parse out the host
107107
hostName = url.substring(0, endOfHostStrPos);
108-
return allowedHostNames.has(hostName);
108+
return allowedHosts.has(hostName);
109109
}
110110
}
111111

112112
return false;
113113
};
114114

115-
const isCustomHostValid = (hostName: string) => {
116-
if (hostName.indexOf("/") !== -1) {
117-
throw new GraphClientError("Please add only hostnames to the CustomHosts config");
115+
/**
116+
* Throws error if the string is not a valid host/hostname and contains other url parts.
117+
* @param {string} url - The host to be verified
118+
*/
119+
const isCustomHostValid = (host: string) => {
120+
if (host.indexOf("/") !== -1) {
121+
throw new GraphClientError("Please add only hosts or hostnames to the CustomHosts config. If the url is `http://example.com:3000/`, host is `example:3000`");
118122
}
119123
};

test/common/core/Client.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,37 @@ describe("Client.ts", () => {
175175

176176
assert.equal(context.options.headers["Authorization"], `Bearer ${accessToken}`);
177177
});
178+
179+
it("Pass invalid custom hosts", async () => {
180+
try {
181+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
182+
const accessToken = "DUMMY_TOKEN";
183+
const provider: AuthProvider = (done) => {
184+
done(null, "DUMMY_TOKEN");
185+
};
186+
187+
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Testing chained middleware array", 200, 100, "");
188+
const chaosHandler = new ChaosHandler(options);
189+
190+
const authHandler = new AuthenticationHandler(new CustomAuthenticationProvider(provider));
191+
192+
const telemetry = new TelemetryHandler();
193+
const middleware = [authHandler, telemetry, chaosHandler];
194+
195+
const customHost = "https://test_custom";
196+
const customHosts = new Set<string>([customHost]);
197+
const client = Client.initWithMiddleware({ middleware, customHosts });
198+
199+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
200+
const response = await client.api(`https://${customHost}/v1.0/me`).get();
201+
202+
throw new Error("Test fails - Error expected when custom host is not valid");
203+
} catch (error) {
204+
assert.isDefined(error);
205+
assert.isDefined(error.message);
206+
assert.equal(error.message, "Please add only hosts or hostnames to the CustomHosts config. If the url is `http://example.com:3000/`, host is `example:3000`");
207+
}
208+
});
178209
});
179210

180211
describe("init", () => {

0 commit comments

Comments
 (0)