1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

fix(cli/fetch): fix relative redirect (#6715)

This commit is contained in:
Marcos Casagrande 2020-07-13 06:53:36 +02:00 committed by GitHub
parent ac000341db
commit 4aeac64ecd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 5 deletions

View file

@ -342,11 +342,7 @@ export async function fetch(
!redirectUrl.startsWith("http://") && !redirectUrl.startsWith("http://") &&
!redirectUrl.startsWith("https://") !redirectUrl.startsWith("https://")
) { ) {
redirectUrl = redirectUrl = new URL(redirectUrl, url).href;
url.split("//")[0] +
"//" +
url.split("//")[1].split("/")[0] +
redirectUrl; // TODO: handle relative redirection more gracefully
} }
url = redirectUrl; url = redirectUrl;
redirected = true; redirected = true;

View file

@ -335,6 +335,27 @@ unitTest(
} }
); );
unitTest(
{
perms: { net: true },
},
async function fetchWithRelativeRedirectionUrl(): Promise<void> {
const cases = [
["end", "http://localhost:4550/a/b/end"],
["/end", "http://localhost:4550/end"],
];
for (const [loc, redUrl] of cases) {
const response = await fetch("http://localhost:4550/a/b/c", {
headers: new Headers([["x-location", loc]]),
});
assertEquals(response.url, redUrl);
assertEquals(response.redirected, true);
assertEquals(response.status, 404);
assertEquals(await response.text(), "");
}
}
);
unitTest( unitTest(
{ {
perms: { net: true }, perms: { net: true },

View file

@ -240,6 +240,19 @@ unitTest(function urlBaseURL(): void {
); );
const url = new URL("/foo/bar?baz=foo#qux", base); const url = new URL("/foo/bar?baz=foo#qux", base);
assertEquals(url.href, "https://foo:bar@baz.qat:8000/foo/bar?baz=foo#qux"); assertEquals(url.href, "https://foo:bar@baz.qat:8000/foo/bar?baz=foo#qux");
assertEquals(
new URL("D", "https://foo.bar/path/a/b/c/d").href,
"https://foo.bar/path/a/b/c/D"
);
assertEquals(new URL("D", "https://foo.bar").href, "https://foo.bar/D");
assertEquals(new URL("D", "https://foo.bar/").href, "https://foo.bar/D");
assertEquals(
new URL("/d", "https://foo.bar/path/a/b/c/d").href,
"https://foo.bar/d"
);
}); });
unitTest(function urlBaseString(): void { unitTest(function urlBaseString(): void {

View file

@ -146,6 +146,14 @@ pub async fn run_all_servers() {
let u = url.parse::<Uri>().unwrap(); let u = url.parse::<Uri>().unwrap();
warp::redirect(u) warp::redirect(u)
}) })
.or(
warp::path!("a" / "b" / "c")
.and(warp::header::<String>("x-location"))
.map(|token: String| {
let uri: Uri = token.parse().unwrap();
warp::redirect(uri)
}),
)
.or( .or(
warp::any() warp::any()
.and(warp::path::peek()) .and(warp::path::peek())