From 495ac923f398443be45c20ab29d183fe47e08911 Mon Sep 17 00:00:00 2001 From: Denys Kurylenko Date: Mon, 1 Mar 2021 06:28:19 -0800 Subject: [PATCH] Allow UrlRewriter to change protocol, i.e. https->http, and http->https Fixes https://github.com/bazelbuild/bazel/issues/13114 Closes #13115. PiperOrigin-RevId: 360172767 --- .../repository/downloader/UrlRewriter.java | 23 +++++++++++------- .../downloader/UrlRewriterTest.java | 24 +++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/UrlRewriter.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/UrlRewriter.java index a081f433b5c96c..a814559a63f7db 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/UrlRewriter.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/UrlRewriter.java @@ -187,17 +187,24 @@ private ImmutableList applyRewriteRules(URL url) { } return rewrittenUrls.build().stream() - .map( - urlString -> { - try { - return new URL(url.getProtocol() + "://" + urlString); - } catch (MalformedURLException e) { - throw new IllegalStateException(e); - } - }) + .map(urlString -> prefixWithProtocol(urlString, url.getProtocol())) .collect(toImmutableList()); } + /** Prefixes url with protocol if not already prefixed by {@link #REWRITABLE_SCHEMES} */ + private static URL prefixWithProtocol(String url, String protocol) { + try { + for (String schemaPrefix : REWRITABLE_SCHEMES) { + if (url.startsWith(schemaPrefix + "://")) { + return new URL(url); + } + } + return new URL(protocol + "://" + url); + } catch (MalformedURLException e) { + throw new IllegalStateException(e); + } + } + @Nullable public String getAllBlockedMessage() { return config.getAllBlockedMessage(); diff --git a/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/UrlRewriterTest.java b/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/UrlRewriterTest.java index 805cf016d2438f..2d48b759a4af3e 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/UrlRewriterTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/UrlRewriterTest.java @@ -212,4 +212,28 @@ public void multipleAllBlockedMessage() throws Exception { assertThat(e.getLocation()).isEqualTo(Location.fromFileLineColumn("/some/file", 3, 0)); } } + + @Test + public void rewritingUrlsAllowsProtocolRewrite() throws Exception { + String config = + "" + + "block *\n" + + "allow mycorp.com\n" + + "allow othercorp.com\n" + + "rewrite bad.com/foo/(.*) http://mycorp.com/$1\n" + + "rewrite bad.com/bar/(.*) https://othercorp.com/bar/$1\n"; + + UrlRewriter munger = new UrlRewriter(str -> {}, "/dev/null", new StringReader(config)); + + List amended = + munger.amend( + ImmutableList.of( + new URL("https://www.bad.com"), + new URL("https://bad.com/foo/bar"), + new URL("http://bad.com/bar/xyz"))); + + assertThat(amended) + .containsExactly( + new URL("http://mycorp.com/bar"), new URL("https://othercorp.com/bar/xyz")); + } }