]> git.wh0rd.org - tt-rss.git/blobdiff - include/functions2.php
Prevent target='_blank' vulnerability on dynamic link
[tt-rss.git] / include / functions2.php
index 6b6f5aa560b630d393b28f42c44e738fad2dcf78..96274b6a0dc141a28f51a3b09f490ac38b7e9ad2 100644 (file)
 
                $params['simple_update'] = defined('SIMPLE_UPDATE_MODE') && SIMPLE_UPDATE_MODE;
 
+               $params["icon_alert"] = base64_img("images/alert.png");
+               $params["icon_information"] = base64_img("images/information.png");
+               $params["icon_cross"] = base64_img("images/cross.png");
+               $params["icon_indicator_white"] = base64_img("images/indicator_white.gif");
+
                return $params;
        }
 
                $doc->loadHTML($charset_hack . $res);
                $xpath = new DOMXPath($doc);
 
-               $entries = $xpath->query('(//a[@href]|//img[@src])');
-
                $ttrss_uses_https = parse_url(get_self_url_prefix(), PHP_URL_SCHEME) === 'https';
+               $rewrite_base_url = $site_url ? $site_url : SELF_URL_PATH;
+
+               $entries = $xpath->query('(//a[@href]|//img[@src]|//video/source[@src])');
 
                foreach ($entries as $entry) {
 
-                       if ($site_url) {
+                       if ($entry->hasAttribute('href')) {
+                               $entry->setAttribute('href',
+                                       rewrite_relative_url($rewrite_base_url, $entry->getAttribute('href')));
 
-                               if ($entry->hasAttribute('href')) {
-                                       $entry->setAttribute('href',
-                                               rewrite_relative_url($site_url, $entry->getAttribute('href')));
+                               $entry->setAttribute('rel', 'noopener noreferrer');
+                       }
 
-                                       $entry->setAttribute('rel', 'noreferrer');
+                       if ($entry->hasAttribute('src')) {
+                               $src = rewrite_relative_url($rewrite_base_url, $entry->getAttribute('src'));
+
+                               $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
+                               $cached_filename = CACHE_DIR . '/images/' . sha1($src) . $extension;
+
+                               if (file_exists($cached_filename)) {
+                                       $src = get_self_url_prefix() . '/public.php?op=cached_image&hash=' . sha1($src) . $extension;
+
+                                       if ($entry->hasAttribute('srcset')) {
+                                               $entry->removeAttribute('srcset');
+                                       }
+
+                                       if ($entry->hasAttribute('sizes')) {
+                                               $entry->removeAttribute('sizes');
+                                       }
                                }
 
-                               if ($entry->hasAttribute('src')) {
-                                       $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
+                               $entry->setAttribute('src', $src);
+                       }
 
-                                       $cached_filename = CACHE_DIR . '/images/' . sha1($src) . '.png';
+                       if ($entry->nodeName == 'img') {
 
-                                       if (file_exists($cached_filename)) {
-                                               $src = SELF_URL_PATH . '/public.php?op=cached_image&hash=' . sha1($src);
+                               if ($entry->hasAttribute('src')) {
+                                       $is_https_url = parse_url($entry->getAttribute('src'), PHP_URL_SCHEME) === 'https';
+
+                                       if ($ttrss_uses_https && !$is_https_url) {
 
                                                if ($entry->hasAttribute('srcset')) {
                                                        $entry->removeAttribute('srcset');
                                                        $entry->removeAttribute('sizes');
                                                }
                                        }
-
-                                       $entry->setAttribute('src', $src);
                                }
 
-                               if ($entry->nodeName == 'img') {
-                                       if ($entry->hasAttribute('src')) {
-                                               $is_https_url = parse_url($entry->getAttribute('src'), PHP_URL_SCHEME) === 'https';
+                               if (($owner && get_pref("STRIP_IMAGES", $owner)) ||
+                                               $force_remove_images || $_SESSION["bw_limit"]) {
 
-                                               if ($ttrss_uses_https && !$is_https_url) {
+                                       $p = $doc->createElement('p');
 
-                                                       if ($entry->hasAttribute('srcset')) {
-                                                               $entry->removeAttribute('srcset');
-                                                       }
+                                       $a = $doc->createElement('a');
+                                       $a->setAttribute('href', $entry->getAttribute('src'));
 
-                                                       if ($entry->hasAttribute('sizes')) {
-                                                               $entry->removeAttribute('sizes');
-                                                       }
-                                               }
-                                       }
-
-                                       if (($owner && get_pref("STRIP_IMAGES", $owner)) ||
-                                                       $force_remove_images || $_SESSION["bw_limit"]) {
+                                       $a->appendChild(new DOMText($entry->getAttribute('src')));
+                                       $a->setAttribute('target', '_blank');
+                                       $a->setAttribute('rel', 'noopener noreferrer');
 
-                                               $p = $doc->createElement('p');
+                                       $p->appendChild($a);
 
-                                               $a = $doc->createElement('a');
-                                               $a->setAttribute('href', $entry->getAttribute('src'));
-
-                                               $a->appendChild(new DOMText($entry->getAttribute('src')));
-                                               $a->setAttribute('target', '_blank');
-
-                                               $p->appendChild($a);
-
-                                               $entry->parentNode->replaceChild($p, $entry);
-                                       }
+                                       $entry->parentNode->replaceChild($p, $entry);
                                }
                        }
 
                        if (strtolower($entry->nodeName) == "a") {
                                $entry->setAttribute("target", "_blank");
+                               $entry->setAttribute("rel", "noopener noreferrer");
                        }
                }
 
                        }
                }
 
-               $allowed_elements = array('a', 'address', 'audio', 'article', 'aside',
+               $allowed_elements = array('a', 'address', 'acronym', 'audio', 'article', 'aside',
                        'b', 'bdi', 'bdo', 'big', 'blockquote', 'body', 'br',
                        'caption', 'cite', 'center', 'code', 'col', 'colgroup',
-                       'data', 'dd', 'del', 'details', 'description', 'div', 'dl', 'font',
+                       'data', 'dd', 'del', 'details', 'description', 'dfn', 'div', 'dl', 'font',
                        'dt', 'em', 'footer', 'figure', 'figcaption',
                        'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'html', 'i',
                        'img', 'ins', 'kbd', 'li', 'main', 'mark', 'nav', 'noscript',
                                        </object>";
                        }
 
-                       if ($entry) $entry .= "&nbsp; <a target=\"_blank\"
+                       if ($entry) $entry .= "&nbsp; <a target=\"_blank\" rel=\"noopener noreferrer\"
                                href=\"$url\">" . basename($url) . "</a>";
 
                        return $entry;
 
 /*             $filename = substr($url, strrpos($url, "/")+1);
 
-               $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
+               $entry .= " <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"" . htmlspecialchars($url) . "\">" .
                        $filename . " (" . $ctype . ")" . "</a>"; */
 
        }
                        num_comments,
                        tag_cache,
                        author,
+                       guid,
                        orig_feed_id,
                        note
                        FROM ttrss_entries,ttrss_user_entries
                                $line = $p->hook_render_article($line);
                        }
 
-                       $num_comments = $line["num_comments"];
+                       $num_comments = (int) $line["num_comments"];
                        $entry_comments = "";
 
                        if ($num_comments > 0) {
                                        $comments_url = htmlspecialchars($line["link"]);
                                }
                                $entry_comments = "<a class=\"postComments\"
-                                       target='_blank' href=\"$comments_url\">$num_comments ".
+                                       target='_blank' rel=\"noopener noreferrer\" href=\"$comments_url\">$num_comments ".
                                        _ngettext("comment", "comments", $num_comments)."</a>";
 
                        } else {
                                if ($line["comments"] && $line["link"] != $line["comments"]) {
-                                       $entry_comments = "<a class=\"postComments\" target='_blank' href=\"".htmlspecialchars($line["comments"])."\">".__("comments")."</a>";
+                                       $entry_comments = "<a class=\"postComments\" target='_blank' rel=\"noopener noreferrer\" href=\"".htmlspecialchars($line["comments"])."\">".__("comments")."</a>";
                                }
                        }
 
                                $rv['content'] .= "<div class=\"postDate\">$parsed_updated</div>";
 
                        if ($line["link"]) {
-                               $rv['content'] .= "<div class='postTitle'><a target='_blank'
+                               $rv['content'] .= "<div class='postTitle'><a target='_blank' rel='noopener noreferrer'
                                        title=\"".htmlspecialchars($line['title'])."\"
                                        href=\"" .
                                        htmlspecialchars($line["link"]) . "\">" .
 
                                        $tmp_line = db_fetch_assoc($tmp_result);
 
-                                       $rv['content'] .= "<a target='_blank'
+                                       $rv['content'] .= "<a target='_blank' rel='noopener noreferrer'
                                                href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
                                                $tmp_line['title'] . "</a>";
 
                                        $rv['content'] .= "&nbsp;";
 
-                                       $rv['content'] .= "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
+                                       $rv['content'] .= "<a target='_blank' rel='noopener noreferrer' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
                                        $rv['content'] .= "<img title='".__('Feed URL')."' class='tinyFeedIcon' src='images/pub_set.png'></a>";
 
                                        $rv['content'] .= "</div>";
 
                                if (!$ctype) $ctype = __("unknown type");
 
-                               $filename = substr($url, strrpos($url, "/")+1);
+                               //$filename = substr($url, strrpos($url, "/")+1);
+                               $filename = basename($url);
 
                                $player = format_inline_player($url, $ctype);
 
                                if ($player) array_push($entries_inline, $player);
 
-#                              $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
+#                              $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\" rel=\"noopener noreferrer\">" .
 #                                      $filename . " (" . $ctype . ")" . "</a>";
 
-                               $entry = "<div onclick=\"window.open('".htmlspecialchars($url)."')\"
+                               $entry = "<div onclick=\"openUrlPopup('".htmlspecialchars($url)."')\"
                                        dojoType=\"dijit.MenuItem\">$filename ($ctype)</div>";
 
                                array_push($entries_html, $entry);
                                                                                src=\"" .htmlspecialchars($entry["url"]) . "\"
                                                                                " . $encsize . " /></p>";
                                                                        } else {
-                                                                               $rv .= "<p><a target=\"_blank\"
+                                                                               $rv .= "<p><a target=\"_blank\" rel=\"noopener noreferrer\"
                                                                                href=\"".htmlspecialchars($entry["url"])."\"
                                                                                >" .htmlspecialchars($entry["url"]) . "</a></p>";
                                                                        }
 
                        foreach ($entries as $entry) {
                                if ($entry["title"])
-                                       $title = "&mdash; " . truncate_string($entry["title"], 30);
+                                       $title = " &mdash; " . truncate_string($entry["title"], 30);
                                else
                                        $title = "";
 
-                               $rv .= "<div onclick='window.open(\"".htmlspecialchars($entry["url"])."\")'
-                                       dojoType=\"dijit.MenuItem\">".htmlspecialchars($entry["filename"])."$title</div>";
+                               if ($entry["filename"])
+                                       $filename = truncate_middle(htmlspecialchars($entry["filename"]), 60);
+                               else
+                                       $filename = "";
+
+                               $rv .= "<div onclick='openUrlPopup(\"".htmlspecialchars($entry["url"])."\")'
+                                       dojoType=\"dijit.MenuItem\">".$filename . $title."</div>";
 
                        };
 
        }
 
        function theme_valid($theme) {
-               if ($theme == "default.css" || $theme == "night.css") return true; // needed for array_filter
+               $bundled_themes = [ "default.php", "night.css", "compact.css" ];
+               
+               if (in_array($theme, $bundled_themes)) return true;
+
                $file = "themes/" . basename($theme);
 
                if (!file_exists($file)) $file = "themes.local/" . basename($theme);
 
                return $errors[$code];
        }
+
+       function base64_img($filename) {
+               if (file_exists($filename)) {
+                        $ext = pathinfo($filename, PATHINFO_EXTENSION);
+
+                       return "data:image/$ext;base64," . base64_encode(file_get_contents($filename));
+               } else {
+                       return "";
+               }
+       }
 ?>