Skip to content

Commit 17522d6

Browse files
Araqnarimiran
andcommitted
fixes a possible 'javascript:' protocol exploit [backport:1.0] (#19134)
* fixes a possible 'javascript:' protocol exploit [backport:1.0] * add tests * Update tests/stdlib/trstgen.nim * add the same logic for hyperlinks * move the logic into a proc Co-authored-by: narimiran <[email protected]> (cherry picked from commit 9338aa2)
1 parent f8047c3 commit 17522d6

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

lib/packages/docutils/rstgen.nim

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
## **Note:** Import ``packages/docutils/rstgen`` to use this module
2727

2828
import strutils, os, hashes, strtabs, rstast, rst, highlite, tables, sequtils,
29-
algorithm, parseutils
29+
algorithm, parseutils, strscans
3030

3131
import ../../std/private/since
3232

@@ -786,6 +786,16 @@ proc renderOverline(d: PDoc, n: PRstNode, result: var string) =
786786
rstnodeToRefname(n), tmp, $chr(n.level - 1 + ord('A'))])
787787

788788

789+
proc safeProtocol(linkStr: var string) =
790+
var protocol = ""
791+
if scanf(linkStr, "$w:", protocol):
792+
# if it has a protocol at all, ensure that it's not 'javascript:' or worse:
793+
if cmpIgnoreCase(protocol, "http") == 0 or cmpIgnoreCase(protocol, "https") == 0 or
794+
cmpIgnoreCase(protocol, "ftp") == 0:
795+
discard "it's fine"
796+
else:
797+
linkStr = ""
798+
789799
proc renderTocEntry(d: PDoc, e: TocEntry, result: var string) =
790800
dispA(d.target, result,
791801
"<li><a class=\"reference\" id=\"$1_toc\" href=\"#$1\">$2</a></li>\n",
@@ -850,6 +860,8 @@ proc renderImage(d: PDoc, n: PRstNode, result: var string) =
850860

851861
# support for `:target:` links for images:
852862
var target = esc(d.target, getFieldValue(n, "target").strip())
863+
safeProtocol(target)
864+
853865
if target.len > 0:
854866
# `htmlOut` needs to be of the following format for link to work for images:
855867
# <a class="reference external" href="target"><img src=\"$1\"$2/></a>

0 commit comments

Comments
 (0)