【问题标题】:C++ Webkit GTK , How to disable cross origin policy?C++ Webkit GTK,如何禁用跨源策略?
【发布时间】:2020-09-05 18:44:06
【问题描述】:

我正在尝试使用“file://”域页面在 iframe 中加载 http://google.com。当然,我收到“Google.com 不允许”错误。 我已经尝试过反向代理,但我认为反向代理没有意义。

在那之后,我研究了几个小时关于禁用或绕过 webkit gtk 中的“跨源策略”。

我在本手册页中尝试了一些解决方案,https://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html

所以,我尝试在 WebKitSettings 中添加此块

   WebKitSettings *settings =
    webkit_web_view_get_settings(WEBKIT_WEB_VIEW(webview));
    webkit_settings_set_allow_file_access_from_file_urls(settings, true);
    webkit_settings_set_allow_file_access_from_file_urls(settings,true);

但它不起作用。我仍然无法在 iframe 中连接到 google.com(或任何受 cors 保护的网站)。

根据我最近的研究,Webkit GTK 手册对此有一些小技巧。 它被称为property

(允许从文件 URL 访问文件)

但我不知道如何实现我的代码。

编辑:

我在我的代码中添加了这一行

webkit_settings_set_allow_universal_access_from_file_urls(settings,true);

现在我也收到“在框架中拒绝连接,因为它将 X-Frame-Options 设置为 SAMEORIGIN”错误。 如何在 webkitgtk 中设置跨域?

【问题讨论】:

  • 您无法绕过 CORS 策略。您的应用应在发出请求的服务器上列入白名单(在您的情况下为 Google)。

标签: c++ browser gtk webkit webkitgtk


【解决方案1】:

正如 cmets 中已经指出的,CORS 政策不能被绕过。

您将无法在 iframe 中加载任何经过适当配置以防止这种情况发生的网站。

解决此问题的唯一方法是从您拥有的网站向您要显示的网站发出服务器端请求,为您的网站配置正确的 X-Frame-Options 并使其返回它从应显示的站点获取的内容。

一种代理,但仍然非常容易出错。

我在 PHP 中做了一个快速的概念证明。

https://lucasreta.com/test/google.com,我们有以下脚本,它检索 google.com 的内容,解析并显示它们:

<?php

$url = "https://www.google.com";
$request = curl_init();
curl_setopt_array($request, [
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTPHEADER => [
    "Content-Type: text/html; charset=UTF-8"
  ]
]);
$return = curl_exec($request);
curl_close($request);

header('Content-Type: text/html; charset=UTF-8');

# after setting headers, we echo the response
# obtained from the site to display and tweak it
# a bit in order to fix local urls
echo str_replace("=\"/", "=\"$url", $return);

https://lucasreta.com/test/google.com/iframe,我们将上面返回的内容包含在我们的 iframe 中:

<style>
* {font-family: sans-serif}
iframe {width: 90%; height: 85vh;}
</style>

<h1>Google in an iframe</h1>

<iframe src="../"></iframe>

【讨论】:

  • 这很好地解释了为什么会发生这种情况,但是如果您的 iframe 页面是本地的 (file://),并且您在 WebKitGTK 小部件中显示它,您能否添加一些关于如何执行此操作的信息?在这种情况下,如何使用正确的框架选项对其进行配置?
猜你喜欢
  • 2011-01-26
  • 1970-01-01
  • 2015-09-07
  • 2014-01-23
  • 1970-01-01
  • 2013-04-19
  • 2013-06-09
  • 2015-07-11
相关资源
最近更新 更多