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

Revert "Redirect gateway-like urls to ipfs://" #15597

Merged
merged 1 commit into from
Oct 21, 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
284 changes: 18 additions & 266 deletions browser/ipfs/test/ipfs_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,20 +395,6 @@ class IpfsServiceBrowserTest : public InProcessBrowserTest {
return http_response;
}

std::unique_ptr<net::test_server::HttpResponse> HandlePublicGatewayRequest(
const net::test_server::HttpRequest& request) {
auto http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_content_type("text/html");

// IPFS gateways set this
http_response->AddCustomHeader("access-control-allow-origin", "*");
http_response->AddCustomHeader("x-ipfs-path", "/ipfs/Qmm");
http_response->set_code(net::HTTP_OK);

return http_response;
}

std::unique_ptr<net::test_server::HttpResponse> HandleEmbeddedSrvrRequest(
const net::test_server::HttpRequest& request) {
auto http_response =
Expand Down Expand Up @@ -1067,291 +1053,57 @@ IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, CannotLoadIPFSImageFromHTTP) {
EXPECT_EQ(base::Value(true), loaded.value);
}

IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest,
TopLevelAutoRedirectsOff_DoNotTranslateToIPFS) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, false);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);

auto tab_url = GetURL(
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.a.com",
"/simple.html?a=b");

ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(contents->GetURL(), tab_url);
}

IN_PROC_BROWSER_TEST_F(
IpfsServiceBrowserTest,
TopLevelAutoRedirectsOn_DoNotRedirectFromGatewayLikeUrl_IpfsDisabled) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_DISABLED));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);

auto tab_url = GetURL(
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.a.com",
"/simple.html?a=b");

ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(contents->GetURL(), tab_url);
}

IN_PROC_BROWSER_TEST_F(
IpfsServiceBrowserTest,
TopLevelAutoRedirectsOn_Gateway_RedirectFromGatewayLikeUrl_IpfsSubDomain) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);

auto tab_url = GetURL(
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.a.com",
"/simple.html?a=b");

ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(
contents->GetURL(),
GetURL(
"b.com",
"/ipfs/bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
"simple.html?a=b"));
}

IN_PROC_BROWSER_TEST_F(
IpfsServiceBrowserTest,
TopLevelAutoRedirectsOn_DoNotRedirectFromGatewayLikeUrl_IfGatewayUrl) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);

auto tab_url = GetURL(
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.b.com",
"/simple.html?a=b");

ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(contents->GetURL(), tab_url);
}

IN_PROC_BROWSER_TEST_F(
IpfsServiceBrowserTest,
TopLevelAutoRedirectsOn_DoNotRedirectFromGatewayLikeUrl_IfLocalhostUrl) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);

auto tab_url = GetURL(
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs."
"localhost",
"/simple.html?a=b");

ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(contents->GetURL(), tab_url);
}

IN_PROC_BROWSER_TEST_F(
IpfsServiceBrowserTest,
TopLevelAutoRedirectsOn_DoNotRedirectFromGatewayLikeUrl_NoXIpfsPathHeader) {
IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, TopLevelAutoRedirectsOn) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandleEmbeddedSrvrRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);

auto tab_url =
GetURL("a.com",
"/ipfs/"
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq");

auto tab_url = GetURL("a.com", "/simple.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(contents->GetURL(), tab_url);
}
EXPECT_EQ(contents->GetURL().host(), tab_url.host());

IN_PROC_BROWSER_TEST_F(
IpfsServiceBrowserTest,
TopLevelAutoRedirectsOn_Gateway_RedirectFromGatewayLikeUrl_IpfsPath) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_GATEWAY));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);

auto tab_url = GetURL(
"a.com",
"/ipfs/bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
"simple.html?a=b");

ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(
contents->GetURL(),
GetURL(
"b.com",
"/ipfs/bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
"simple.html?a=b"));
}

IN_PROC_BROWSER_TEST_F(
IpfsServiceBrowserTest,
TopLevelAutoRedirectsOn_ASK_RedirectFromGatewayLikeUrl_IpfsPath) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);

auto tab_url = GetURL(
"a.com",
"/ipfs/bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
"simple.html?a=b");

ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(
contents->GetURL(),
GURL("ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
"simple.html?a=b"));
}

IN_PROC_BROWSER_TEST_F(
IpfsServiceBrowserTest,
TopLevelAutoRedirectsOn_ASK_RedirectFromGatewayLikeUrl_IpfsSubDomain) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);

auto tab_url = GetURL(
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.a.com",
"/simple.html?a=b");

tab_url = GURL("ipfs://Qmc2JTQo4iXf24g98otZmGFQq176eQ2Cdbb88qA5ToMEvC/2");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(
auto domain = GetDomainAndRegistry(
contents->GetURL(),
GURL("ipfs://bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq/"
"simple.html?a=b"));
}

IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest,
TopLevelAutoRedirectsOn_DoNotTranslateSimpleUrls) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);

auto tab_url = GetURL("a.com", "/simple.html?a=b");

ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(contents->GetURL(), tab_url);
EXPECT_EQ(domain, gateway.host());
}

IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest,
TopLevelAutoRedirectsOn_DoNotTranslateIncompleteUrls) {
TopLevelAutoRedirectsOnWithQuery) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
base::BindRepeating(&IpfsServiceBrowserTest::HandleEmbeddedSrvrRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, true);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);

auto tab_url = GetURL("ipfs.a.com", "/simple.html?a=b");

ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), GetURL("a.com", "/simple.html?abc=123xyz&other=qwerty")));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(contents->GetURL(), tab_url);
EXPECT_EQ(contents->GetURL().query(), "abc=123xyz&other=qwerty");
}

IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, TopLevelAutoRedirectsOff) {
ResetTestServer(
base::BindRepeating(&IpfsServiceBrowserTest::HandlePublicGatewayRequest,
base::BindRepeating(&IpfsServiceBrowserTest::HandleEmbeddedSrvrRequest,
base::Unretained(this)));
browser()->profile()->GetPrefs()->SetBoolean(kIPFSAutoRedirectGateway, false);
browser()->profile()->GetPrefs()->SetInteger(
kIPFSResolveMethod,
static_cast<int>(ipfs::IPFSResolveMethodTypes::IPFS_ASK));
GURL gateway = GetURL("b.com", "/");
SetIPFSDefaultGatewayForTest(gateway);

auto tab_url = GetURL(
"bafybeiemxf5abjwjbikoz4mc3a3dla6ual3jsgpdr4cjr3oz3evfyavhwq.ipfs.a.com",
"/simple.html?a=b");

ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), tab_url));
SetIPFSDefaultGatewayForTest(GetURL("b.com", "/"));
GURL other_gateway = GetURL("a.com", "/simple.html");
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GetURL("a.com", "/simple.html")));
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(contents->GetURL(), tab_url);
EXPECT_EQ(contents->GetURL().host(), other_gateway.host());
}

IN_PROC_BROWSER_TEST_F(IpfsServiceBrowserTest, ImportTextToIpfs) {
Expand Down
25 changes: 10 additions & 15 deletions browser/net/ipfs_redirect_network_delegate_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "net/base/net_errors.h"
#include "net/base/url_util.h"

namespace ipfs {

Expand Down Expand Up @@ -95,13 +94,6 @@ int OnHeadersReceived_IPFSRedirectWork(
std::shared_ptr<brave::BraveRequestInfo> ctx) {
if (!ctx->browser_context)
return net::OK;

// Auto-redirect gateway-like urls is enabled only for top-level frames
// to avoid mixed content corner cases.
if (ctx->resource_type == blink::mojom::ResourceType::kSubFrame) {
return net::OK;
}

auto* prefs = user_prefs::UserPrefs::Get(ctx->browser_context);
if (IsIpfsResolveMethodDisabled(prefs)) {
return net::OK;
Expand All @@ -113,21 +105,24 @@ int OnHeadersReceived_IPFSRedirectWork(
response_headers->GetNormalizedHeader("x-ipfs-path", &ipfs_path) &&
// Make sure we don't infinite redirect
!ctx->request_url.DomainIs(ctx->ipfs_gateway_url.host()) &&
!net::IsLocalhost(ctx->request_url)) {
auto translated_url = ipfs::TranslateToCurrentGatewayUrl(ctx->request_url);
// Do not redirect if the frame is not ipfs/ipns
IsIPFSScheme(ctx->initiator_url)) {
GURL::Replacements replacements;
replacements.SetPathStr(ipfs_path);

if (!translated_url) {
return net::OK;
if (ctx->request_url.has_query()) {
replacements.SetQueryStr(ctx->request_url.query_piece());
}

GURL new_url = ctx->ipfs_gateway_url.ReplaceComponents(replacements);

*override_response_headers =
new net::HttpResponseHeaders(response_headers->raw_headers());
(*override_response_headers)
->ReplaceStatusLine("HTTP/1.1 307 Temporary Redirect");
(*override_response_headers)->RemoveHeader("Location");
(*override_response_headers)
->AddHeader("Location", translated_url.value().spec());
*allowed_unsafe_redirect_url = translated_url.value();
(*override_response_headers)->AddHeader("Location", new_url.spec());
*allowed_unsafe_redirect_url = new_url;
}

return net::OK;
Expand Down
Loading