Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correctly set x-ms-original-url to the full original request URL (#286) #314

Merged
merged 6 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cypress/integration/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ context.only("/api", () => {
expect(body).to.include("x-swa-custom");
});
});
it("Should correctly set x-ms-original-url to the full request url", () => {
const HEADER_URL = "http://0.0.0.0:1234/api/headers";
cy.request({ url: HEADER_URL, failOnStatusCode: false }).then((response) => {
expect(response.status).to.eq(200);
expect(response.body["x-ms-original-url"]).to.equal(HEADER_URL);
});
});
});

describe(`Accessing /api/status`, () => {
Expand Down Expand Up @@ -46,7 +53,7 @@ context.only("/api", () => {

cy.request({ url: `http://0.0.0.0:1234/api/info`, failOnStatusCode: false }).then((response) => {
expect(response.status).to.eq(200);
expect(response.body).to.eq('authorized');
expect(response.body).to.eq("authorized");
});
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/msha/handlers/function.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ registerProcessExit(() => {

function injectHeaders(req: http.ClientRequest, host: string) {
logger.silly(`injecting headers to Functions request:`);

req.setHeader("x-ms-original-url", encodeURI(new URL(req.path!, host).toString()));
logger.silly(` - x-ms-original-url: ${chalk.yellow(req.getHeader("x-ms-original-url"))}`);

if (!req.getHeader("x-ms-original-url")) {
req.setHeader("x-ms-original-url", encodeURI(new URL(req.path!, host).toString()));
logger.silly(` - x-ms-original-url: ${chalk.yellow(req.getHeader("x-ms-original-url"))}`);
}
// generate a fake correlation ID
req.setHeader("x-ms-request-id", `SWA-CLI-${Math.random().toString(36).substring(2).toUpperCase()}`);
logger.silly(` - x-ms-request-id: ${chalk.yellow(req.getHeader("x-ms-request-id"))}`);
Expand Down
4 changes: 1 addition & 3 deletions src/msha/middlewares/request.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,7 @@ export async function requestMiddleware(
return await handleAuthRequest(req, res, matchingRouteRule, userConfig);
}

getResponse(req, res, matchingRouteRule, userConfig, isFunctionReq);

if (!isFunctionReq) {
if (!getResponse(req, res, matchingRouteRule, userConfig, isFunctionReq)) {
logger.silly(` - url: ${chalk.yellow(req.url)}`);
logger.silly(` - target: ${chalk.yellow(target)}`);

Expand Down
21 changes: 13 additions & 8 deletions src/msha/middlewares/response.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,35 @@ export function getResponse(
matchedRoute: SWAConfigFileRoute | undefined,
userConfig: SWAConfigFile | undefined,
isFunctionRequest: boolean
) {
): boolean {
const statusCodeToServe = parseInt(`${matchedRoute?.statusCode}`, 10);
const redirect = matchedRoute?.redirect;
const rewrite = matchedRoute?.rewrite;

logger.silly(`using userConfig`);
logger.silly({ userConfig });

if (redirect) {
logger.silly(` - redirect rule detected. Exit`);

return applyRedirectResponse(req, res, matchedRoute);
applyRedirectResponse(req, res, matchedRoute);
return false;
}

// We should always set the x-ms-original-url to be the full request URL.
req.headers["x-ms-original-url"] = new URL(req.url!, `http://${req.headers.host}`).href;
if (rewrite) {
req.url = rewrite;
}

if ([403, 401].includes(statusCodeToServe)) {
logger.silly(` - ${statusCodeToServe} code detected. Exit`);

return handleErrorPage(req, res, statusCodeToServe, userConfig?.responseOverrides);
handleErrorPage(req, res, statusCodeToServe, userConfig?.responseOverrides);
return false;
}

if (isFunctionRequest) {
return handleFunctionRequest(req, res);
handleFunctionRequest(req, res);
return true;
}

const storageResult = getStorageContent(
Expand All @@ -60,12 +63,14 @@ export function getResponse(
);

if (storageResult.isFunctionFallbackRequest) {
return handleFunctionRequest(req, res);
req.url = userConfig?.navigationFallback.rewrite!;
handleFunctionRequest(req, res);
return true;
}

if (statusCodeToServe) {
res.statusCode = statusCodeToServe;
}
return false;
}

export function getStorageContent(
Expand Down