2017-04-03 28 views
0

我理解的这个安全隐患,为什么我得到的错误是这样的:沟通与单独的Chrome标签

Uncaught DOMException: Blocked a frame with origin 
"http://myurl.com" from accessing a cross-origin frame. 

所以我不知道如果有,我做这一个安全的方式。

有两个网站内部我公司 - 我的,另外一个,做在同一个域不存在

在我的页面中,我有兴趣加载第二页,让我可以从该页面访问ID元素,获取这些ID元素包含的数据,并将该数据返回到我的页面,这样我就可以将其显示给我的用户。我没有API来从第二个来源获取这些数据。最终,如果我还有一种方法可以将数据返回到源页面,我会很喜欢它,但是注射和攻击的风险一般很大,我怀疑有什么办法可以做到,甚至尽管我的意图不是恶意的。

我试过几件事情:

/* Literally load the page within my own and pull data once it's loaded */ 
$('#test').load('url.com/site2'); 

/* load the second page as a variable, then try to access an id on 
that page through the variable */ 
var win = window.open('url.com/site2'); 
var test = win.getElementByID('#id_element_i_want_to_pull'); 

/* I can do something using PHP, but this just loads the page, but doesn't allow 
me to access any of the ID elements on that page which 
doesn't really help me: */ 
$temp = file_get_contents('url.com/site2'); 

有什么办法去了解呢?我没有权限访问第二台服务器上的代码,因此,如果需要的话,我可能无法(或许)将代码放在那里,这样我就可以访问这些代码。如果这是唯一的方法,我至少想知道它,并知道如何才能完成这种类型的请求。

回答

1

我认为你在正确的轨道上加载到服务器上,你只需要将它解析成你可以用id来获取东西的东西。由于我在PHP中做了很多工作已经有一段时间了,但是您应该可以使用DOMDocument类来完成此操作。基本上,你加载文本,把它扔进这些人之一,然后通过他们的ID获取元素。

1

如果这是一个你没有直接访问的网站,听起来好像你已经可以使用DOMDocument类来做一些DOM“hoovering”或“scraping”。

使用DOMDocument,您可以获取整个页面的内容,然后通过您正在查找的标记/属性对其进行过滤。我已经写在PHP7这样的事情在过去,这可能会帮助:

class HooverDom { 
     public $content; 

     public static function checkContentUrl($url) { 
     if (stripos($url, 'http') !== 0) { 
      return 'http://' . $url; 
     } 
     return $url; 
     } 

     public function getContent($url) { 
     if (!$this->content) { 
      $url = self::checkContentUrl($url); 

      if ($url) { 
       $this->content = new \DOMDocument('1.0', 'utf-8'); 
       $this->content->preserveWhiteSpace = false; 
       // suppress warnings from invalid code 
       @$this->content->loadHTMLFile($url); 
      } 
     } 
     return $this->content; 
    } 

    /** 
     * @param $url 
     * @param $tag 
     * 
     * @return array 
     * Extract tags that are of interest 
     */ 
    public function getTags($url, $tag) { 
     $count = 0; 
     $result = array(); 
     $url = self::checkContentUrl($url); 
     if (!$url) return false; 

     $elements = $this->getContent($url)->getElementsByTagName($tag); 

     foreach ($elements as $node) { 
      $result[$count]['value'] = trim(preg_replace('/\s+/', ' ', $node->nodeValue)); 
      if ($node->hasAttributes()) { 
       foreach ($node->attributes as $name => $attr) { 
        $result[$count]['attributes'][$name] = $attr->value; 
       } 
      } 
      $count++; 
     } 
     return $result; 
    } 

     /** 
     * @param $url 
     * @param $attr 
     * @param null [$domain] 
     * 
     * @return array 
     * Extract specific attributes rather than tags. Get all tags with * 
     * and get their attributes. Optional $domain value keeps all results 
     * within supplied domain name 
     */ 
     public function getAttributes($url, $attr, $domain = null) { 
     $result = array(); 
     $elements = $this->getContent($url)->getElementsByTagName('*'); 

     foreach ($elements as $node) { 
      if ($node->hasAttribute($attr)) { 
       $value = $node->getAttribute($attr); 
       if ($domain) { 
        if (stripos($value, $domain) !== FALSE) { 
        $result[] = trim($value); 
        } 
       } else { 
       $result[] = trim($value); 
       } 
      } 
     } 
     return $result; 
     } 
} 

define('DEFAULT_URL', 'https://developer.mozilla.org/en-US'); 
define('DEFAULT_TAG', 'div'); 

$vac = new HooverDom(); 

$url = strip_tags($_GET['url'] ?? DEFAULT_URL); 
$tag = strip_tags($_GET['tag'] ?? DEFAULT_TAG); 

echo 'Dump of tags: ' . PHP_EOL; 
var_dump($vac->getTags($url, $tag)); 

这将抓住所有的页面上的链接,并吐出列表供您。这样你就可以使用一些结构来代替file_get_contents()中的大量字符串。

输出会是这个样子使用https://developer.mozilla.org/en-US/为例:

array (size=56) 
    0 => 
    array (size=2) 
     'value' => string 'Mozilla is working on a new program for developers and other web builders like you. Help shape that program by taking our 10 minute survey: https://googl/forms/Ync2VuTWwAkQFvJx2' (length=178) 
    'attributes' => 
    array (size=1) 
     'class' => string 'global-notice' (length=13) 
    1 => 
    array (size=2) 
    'value' => string 'Mozilla is working on a new program for developers and other web builders like you. Help shape that program by taking our 10 minute survey: ' (length=178) 
    'attributes' => 
    array (size=1) 
     'class' => string 'wrap center' (length=11) 

..........

对不起一些格式化事故,让我知道,如果你需要澄清任何事情。您可以遍历结果并隔离特定的元素ID /类/您正在查找的任何其他属性,并在“值”中获取内容。

请注意NULL合并运算符(?),只有在PHP 7中运行时才会运行5.