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

logic added for redirecting to subdomains #22944

Merged
merged 3 commits into from
Dec 19, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -1271,5 +1271,11 @@
"domain": "www.fayettevillenc.va.gov",
"src": "/locations/FayettevilleVetCenter.asp",
"dest": "/fayetteville-nc-vet-center/"
},
{
"domain": "www.vetcenter.va.gov",
"src": "/Vet_Centers_are_Here_For_You_24_7.asp",
"dest": "https://www.vetcenter.va.gov/",
"isToSubdomain": true
}
]
20 changes: 19 additions & 1 deletion src/applications/proxy-rewrite/redirects/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const deriveIsHostMatch = (redirect, currentWindow) => {
const redirectIfNecessary = currentWindow => {
// Check if the route matches an absolute cross-domain redirect.
const absoluteCrossDomainRedirects = crossDomainRedirects?.filter(
redirect => !redirect?.catchAll,
redirect => !redirect?.catchAll && !redirect?.isToSubdomain,
);
const absoluteRedirectMatch = absoluteCrossDomainRedirects?.find(redirect => {
const isHostMatch = deriveIsHostMatch(redirect, currentWindow);
Expand Down Expand Up @@ -58,6 +58,24 @@ const redirectIfNecessary = currentWindow => {
catchAllRedirectMatch.dest
}`;
}

// Check if redirect destination is to a subdomain
const toSubdomainRedirects = crossDomainRedirects?.filter(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESLint disabled here

redirect => redirect?.isToSubdomain,
);

const toSubdomainRedirectMatch = toSubdomainRedirects?.find(redirect => {
const isHostMatch = deriveIsHostMatch(redirect, currentWindow);
const currentPathMatchesSubdomainSrc =
currentWindow.location.pathname === redirect.src.toLowerCase();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is going to be problematic. We likely want to call toLowerCase on both values being compared.

return isHostMatch && currentPathMatchesSubdomainSrc;
});

// Redirect if there is a to subdomain match
if (toSubdomainRedirectMatch) {
// eslint-disable-next-line no-param-reassign

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESLint disabled here

currentWindow.location.href = `${catchAllRedirectMatch.dest}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like this should be:

currentWindow.location.href = `${toSubdomainRedirectMatch.dest}`;

}
};

export default redirectIfNecessary;
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,21 @@ describe('Redirect replaced pages', () => {
});

describe('Validate crossDomainRedirects.json', () => {
const redirectsBySource = redirects.reduce((grouped, redirect) => {
const fullPath = `https://${redirect.domain}${redirect.src}`;
const items = grouped[fullPath] || [];
return {
...grouped,
[fullPath]: items.concat(redirect.dest),
};
}, {});
const nonSubdomainRedirects = redirects.filter(
redirect => !redirect.isToSubdomain,
);

const redirectsBySource = nonSubdomainRedirects.reduce(
(grouped, redirect) => {
const fullPath = `https://${redirect.domain}${redirect.src}`;
const items = grouped[fullPath] || [];
return {
...grouped,
[fullPath]: items.concat(redirect.dest),
};
},
{},
);
Object.entries(redirectsBySource).forEach(([fullSource, destinations]) => {
it(`${fullSource} is a valid URL`, () => {
const url = new URL(fullSource);
Expand Down