Page MenuHomePhabricator

phan-taint-check-plugin needs clearer error messages
Open, Needs TriagePublic

Description

Here's a random example:

includes/Output/IframeSandbox.php:240 SecurityCheck-DoubleEscaped Calling method \MediaWiki\Html\Html::element() in \MediaWiki\Output\IframeSandbox::getHtml that outputs using tainted argument #2. (Caused by: includes/Html/Html.php +264) (Caused by: includes/Output/OutputPage.php +2988; includes/Output/OutputPage.php +2974; includes/Output/OutputPage.php +2973)

I can't for the life of me tell what Phan thinks is being double-escaped here. The code snippet is

public function getHtml(): string {
	return Html::element( 'iframe', [
		// bunch of attributes
	] );
}

Is "argument #2" 1-based or 0-based? Is it even claiming that the double-escaping happens in argument #2, or is that only mentioned in passing? Does "outputs using tainted argument" refer to Html::element or IframeSandbox::getHtml? (I guess the former because the other doesn't have arguments, but the phrasing is ambiguous.) What are the two "Caused by" remarks trying to say?

Now imagine a new developer (who is unfamiliar with Phan and with MediaWiki) having to interpret this error message.

Some ideas of how it could be improved:

  • Say what's going on, do not rely on the error code. An error message can't be too verbose of course, but it can include a short URL to a wiki page which explains what this type of error is about.
  • Use something more understandable instead of "tainted". (What does it even mean in this context? That it's unsafe input? That it's already escaped?)
  • Be clear about 0-based vs. 1-based indexes.
  • If the parent method (IframeSandbox::getHtml in this case) is only mentioned to orient the reader to where this is happening , and isn't related to the actual problem, move it to the error prefix (after the filename / line number).
  • Instead of "Caused by", say something like "Call stack for method1", "Call stack for method 2". Also if the call stack can include method names, that would make it more convenient to follow.

Event Timeline

Generally speaking, I wholeheartedly agree. I've had similar ideas in the past, and I'm fairly sure I even created some tasks about it, but never ended up actually implementing them. Some specific thoughts in response to your ideas:

Say what's going on, do not rely on the error code. An error message can't be too verbose of course, but it can include a short URL to a wiki page which explains what this type of error is about.

I've never thought of that, but indeed, it would be nice to add links.

Use something more understandable instead of "tainted". (What does it even mean in this context? That it's unsafe input? That it's already escaped?)

I agree, it's pretty much jargon. Even the "outputs" is not clear at all, given that there's nothing being output. In this specific scenario, I think it's just a consequence of DoubleEscaped detection being added at a later time, and just reusing the message that was used for other things such as XSS.

Be clear about 0-based vs. 1-based indexes.

Fair point. Taint-check always tries to include a code snippet for the argument in the error message, but only when it's shorter than 25 characters. In this case, the problematic argument is the array of attributes, which is presumably longer than 25 characters. Another thing we could do is include the name of the parameter. I'm just not sure what that would look in practice; I assume something like the following could also be confusing:

argument #1 (`$paramName`) (`$this->getArgument( 'baz' )`)

If the parent method (IframeSandbox::getHtml in this case) is only mentioned to orient the reader to where this is happening , and isn't related to the actual problem, move it to the error prefix (after the filename / line number).

I've actually always wondered whether including the parent method (which, as you guessed, is only there to orient the reader) is useful at all, and considered removing it. The main reason why I haven't done it thus far is that I really have no idea whether other people (who haven't spent a lot of time analyzing taint-check issues) find it useful.

Instead of "Caused by", say something like "Call stack for method1", "Call stack for method 2". Also if the call stack can include method names, that would make it more convenient to follow.

The caused-by lines are not necessarily call stacks. They also include things such as assignments. I agree that there are likely much better ways to phrase that, but I'm not sure what to use exactly.

Also, another thing I've always wanted to do is to clearly state the purpose of the two caused-by sets used in the error message when flagging a function call. The first one explains why the parameter is tainted (e.g., why the function parameter eventually gets escaped), while the second one explains why the argument is tainted (i.e., what caused the argument to be already escaped). Once again though, I'm not sure how to explain that without using jargon.


As an aside, in this example taint-check has the following caused-by lines for the parameter: includes/Html/Html.php +264. That's the line where the element() method is defined, which looks surprising. What really makes taint-check think that the parameter gets escaped is the following annotation in the method docblock: * @param-taint $attribs escapes_html. The master version of taint-check (not released yet) does a slightly better job and it at least tells you that it's coming from an annotation: annotations in \Html::element(). We could still do better by including the actual parameter whose annotation is being read (or the return value); however, that's more complex as I wrote in T291743.

I would like to do this in the near future. My main problem is that I'm not sure how to rephrase the error messages, I only know how to change the code :) I think it's a combination of 2 things: the concepts involved being somewhat complex, and the fact that I'm used to this jargon and find it hard to read it as someone who isn't. I would very much welcome ideas for improving these messages. Take the following snippet (demo):

<?php

function printParameter( $x ) {
	echo $x;
}

function execute() {
    $value = $_GET['foo'];
    printParameter( $value );
}

Here, $value is user-controlled, and it's passed to printParameter() which prints it, thus resulting in an XSS vulnerability. Currently, taint-check would report:

input:9: SecurityCheck-XSS Calling method \printParameter() in \execute that outputs using tainted argument #1 ($value). (Caused by: input +4) (Caused by: input +8)

Which you would read as:

XSS vulnerability at line 9, inside the \execute() function: the first argument ($value) is user-controlled (because of line 8), and the function you're calling outputs it (at line 4)

Note that in real code, the list of lines that cause a value to be considered user-controlled (or a function to be considered as outputting an argument) can be very long, and they could span multiple function calls, assignments, and different files. For this reason, it's more practical to have them all at the end of the message as is currently the case.

With that in mind: any suggestions?

Thanks for working on this!

Maybe just split it up?

Potential XSS vulnerability at line 9, inside the \execute() function: the first argument ($value) is user-controlled, and gets output without escaping.
Code locations for user-controlled input: (...)
Code locations for unescaped output: (...)

Maybe just split it up?

Yes, that's pretty much what we're already doing and I'd like to keep that. But I forgot to mention one of the limitations we currently have: the message must be single-line. Phan forcefully strips all newlines from the issue message. I had the idea of adding indentation to the lines, to show for example where a function call beings and ends, but this is just impossible for now and that could affect readability quite a bit...

(hi, I found this task from T401748)

I think the real fix to make the "caused by" information comprehensible would be to make the plugin emit a graph that shows how the tainted data flows through the relevant lines of code, instead of just a flat list of these lines.

For example, in this fairly simple case: DEMO there are two different paths for tainted data to be assigned to the property, which is output as HTML. Phan currently reports:

input:38: SecurityCheck-XSS Echoing expression that was not html escaped (Caused by: input +34; input +22; input +11; input +7; input +3; input +26; input +15; input +3)

…basically listing all lines from the two paths, but excluding some – not all – duplicates in a way I don't understand.

I wish it would instead report something like this, which is a graph in Graphviz DOT format:

input:38: SecurityCheck-XSS Echoing expression that was not html escaped, caused by: `digraph { "input:38" -> "input:34" -> "input:22" -> "input:11" -> "input:7" -> "input:3"; "input:34" -> "input:26" -> "input:15" -> "input:3" }`

…which is arguably a bit more readable by itself, and which can be visualized like this:

graph.png (264×539 px, 25 KB)

I feel like the plugin has to already have this information somewhere, but I couldn't figure out where. It might be "flattening" this graph for performance reasons?

In the particular context of T401748, I think this would also make it obvious when an issue is emitted due to reaching the maximum analysis depth (since the graph would have some root/leaf notes that are not taint sinks/sources).


I recently learned that Psalm has a taint check mode too, and it can output such graphs, although only for the entire codebase, which would probably be completely impractical for MediaWiki. For the example above, it generates:


taints.png (798×1,019 px, 69 KB)

…which, actually, doesn't look right (unsafeB calls unsafe1, not unsafe2), but you get the idea.

I think the real fix to make the "caused by" information comprehensible would be to make the plugin emit a graph that shows how the tainted data flows through the relevant lines of code, instead of just a flat list of these lines.

100% agree, but I would call this the "ideal" fix rather than "real":

  • For emitting the graph as images, there's obviously some scaffolding and dependencies to add, then it needs to be integrated in phan, and presumably also in CI?
  • Alternatively, we could emit a text representation of the graph. I had filed T291379 years ago (the title isn't very clear or easy to find). However, phan forces issue output on a single line, and I don't think there's a way to have a multi-line message.

But if we could find a way to do it, I agree that it would be the best way forwards for clarity.

…basically listing all lines from the two paths, but excluding some – not all – duplicates in a way I don't understand.

The deduplication is best-effort and kinda random-ish. It was an incremental step from a previous version where caused-by lines were append-only and full of duplicates. If you feel brave, you can take a look at CausedByLines::asMergedWith. Improving deduplication (potentially by just using a better data structure) of caused-by lines is one of the things I consider the most important as next steps for the plugin.

(My mental roadmap is: improve caused-by output format and deduplication; any other caused-by improvements as needed; performance improvements as needed; finally land r681443 once performance and new issues are under control; T281479)

I feel like the plugin has to already have this information somewhere, but I couldn't figure out where. It might be "flattening" this graph for performance reasons?

Caused-by lines are represented by the CausedByLines class linked above and stored on individual code elements (the taintedOriginalError dynamic property). The class is essentially a wrapper around a (flat!) array of "lines", where each "line" contains the actual line in the source code, as well as additional data (taintedness and method links being transferred) that later allows us to decide whether a given line should be reported for a given issue. We don't have hierarchical information, which is why deduplication is best-effort and kinda sucks, aside from being relatively expensive-ish. Adding that hierarchy would indeed be a game-changer, allowing us to basically get deduplication for free, while having more possibilities with the output format.

I recently learned that Psalm has a taint check mode too, and it can output such graphs, although only for the entire codebase, which would probably be completely impractical for MediaWiki.

Yeah, I have played a bit with psalm's taint-checking mode in the past. I think it does some things better than taint-check, but it also has certain design choices that make it impractical for us. For example being a separate analysis mode, and perhaps most importantly, the fact that all issues are reported on the line of the sink; so for example, in MediaWiki all XSSs would be reported on the line of the echo in OutputPage (or something like that; I haven't actually tried it out, not recently anyway). I do like how it guides you through the source, though.

Change #1358905 had a related patch set uploaded (by Daimona Eaytoy; author: Daimona Eaytoy):

[mediawiki/tools/phan/SecurityCheckPlugin@master] Store the role of each caused-by location

https://gerrit.wikimedia.org/r/1358905

Change #1358909 had a related patch set uploaded (by Daimona Eaytoy; author: Daimona Eaytoy):

[mediawiki/tools/phan/SecurityCheckPlugin@master] Show the role of caused-by locations in issue messages

https://gerrit.wikimedia.org/r/1358909

Change #1359722 had a related patch set uploaded (by Daimona Eaytoy; author: Daimona Eaytoy):

[mediawiki/tools/phan/SecurityCheckPlugin@master] [WIP] Make issue messages clearer for developers that do not know taint analysis

https://gerrit.wikimedia.org/r/1359722

I ended up throwing Claude at the problem to implement some ideas I had in mind for a looooong time but hadn't yet found the time/energy to actually write.