some protection against injected doc comments from templates

This commit is contained in:
Araq 2018-09-08 10:45:51 +02:00
commit 769b562764
2 changed files with 12 additions and 7 deletions

View file

@ -369,7 +369,8 @@ proc isRunnableExample(n: PNode): bool =
result = n.kind == nkSym and n.sym.magic == mRunnableExamples or result = n.kind == nkSym and n.sym.magic == mRunnableExamples or
n.kind == nkIdent and n.ident.s == "runnableExamples" n.kind == nkIdent and n.ident.s == "runnableExamples"
proc getAllRunnableExamples(d: PDoc; n: PNode; dest: var Rope) = proc getAllRunnableExamplesRec(d: PDoc; n, orig: PNode; dest: var Rope) =
if n.info.fileIndex != orig.info.fileIndex: return
case n.kind case n.kind
of nkCallKinds: of nkCallKinds:
if isRunnableExample(n[0]) and if isRunnableExample(n[0]) and
@ -394,7 +395,10 @@ proc getAllRunnableExamples(d: PDoc; n: PNode; dest: var Rope) =
dest.add(d.config.getOrDefault"doc.listing_end" % id) dest.add(d.config.getOrDefault"doc.listing_end" % id)
else: discard else: discard
for i in 0 ..< n.safeLen: for i in 0 ..< n.safeLen:
getAllRunnableExamples(d, n[i], dest) getAllRunnableExamplesRec(d, n[i], orig, dest)
proc getAllRunnableExamples(d: PDoc; n: PNode; dest: var Rope) =
getAllRunnableExamplesRec(d, n, n, dest)
when false: when false:
proc findDocComment(n: PNode): PNode = proc findDocComment(n: PNode): PNode =
@ -677,7 +681,8 @@ proc traceDeps(d: PDoc, it: PNode) =
"$1", [rope esc(d.target, changeFileExt(external, "")), "$1", [rope esc(d.target, changeFileExt(external, "")),
rope changeFileExt(external, "html")]) rope changeFileExt(external, "html")])
proc generateDoc*(d: PDoc, n: PNode) = proc generateDoc*(d: PDoc, n, orig: PNode) =
if orig.info.fileIndex != n.info.fileIndex: return
case n.kind case n.kind
of nkCommentStmt: add(d.modDesc, genComment(d, n)) of nkCommentStmt: add(d.modDesc, genComment(d, n))
of nkProcDef: of nkProcDef:
@ -704,11 +709,11 @@ proc generateDoc*(d: PDoc, n: PNode) =
genItem(d, n.sons[i], n.sons[i].sons[0], genItem(d, n.sons[i], n.sons[i].sons[0],
succ(skType, ord(n.kind)-ord(nkTypeSection))) succ(skType, ord(n.kind)-ord(nkTypeSection)))
of nkStmtList: of nkStmtList:
for i in countup(0, sonsLen(n) - 1): generateDoc(d, n.sons[i]) for i in countup(0, sonsLen(n) - 1): generateDoc(d, n.sons[i], orig)
of nkWhenStmt: of nkWhenStmt:
# generate documentation for the first branch only: # generate documentation for the first branch only:
if not checkForFalse(n.sons[0].sons[0]): if not checkForFalse(n.sons[0].sons[0]):
generateDoc(d, lastSon(n.sons[0])) generateDoc(d, lastSon(n.sons[0]), orig)
of nkImportStmt: of nkImportStmt:
for i in 0 .. sonsLen(n)-1: traceDeps(d, n.sons[i]) for i in 0 .. sonsLen(n)-1: traceDeps(d, n.sons[i])
of nkFromStmt, nkImportExceptStmt: traceDeps(d, n.sons[0]) of nkFromStmt, nkImportExceptStmt: traceDeps(d, n.sons[0])
@ -902,7 +907,7 @@ proc commandDoc*(cache: IdentCache, conf: ConfigRef) =
if ast == nil: return if ast == nil: return
var d = newDocumentor(conf.projectFull, cache, conf) var d = newDocumentor(conf.projectFull, cache, conf)
d.hasToc = true d.hasToc = true
generateDoc(d, ast) generateDoc(d, ast, ast)
writeOutput(d) writeOutput(d)
generateIndex(d) generateIndex(d)

View file

@ -49,7 +49,7 @@ proc processNode(c: PPassContext, n: PNode): PNode =
result = n result = n
var g = PGen(c) var g = PGen(c)
if shouldProcess(g): if shouldProcess(g):
generateDoc(g.doc, n) generateDoc(g.doc, n, n)
proc processNodeJson(c: PPassContext, n: PNode): PNode = proc processNodeJson(c: PPassContext, n: PNode): PNode =
result = n result = n