Jump to content

User:CSteipp (WMF)/Training/VulnTagging easy

From mediawiki.org

WARNING: This is a vulnerable script for demonstration. Don't use it!

This parser function is vulnerable to a few common attacks from Security_for_developers. Answers are on the Discussion page.


<?php

define( NS_TAGS, 10023 );

$wgHooks['ParserFirstCallInit'][] = 'wfVulntagging';

function wfVulntagging( &$parser ) {
	$parser->setHook( 'vtag', 'wfAddTags' );
	return true;
}


function wfAddTags( $input, $argv, $parser ) {
	$articleId = $parser->getTitle()->getArticleID();

	if ( isset( $argv['articleid'] ) ) {
		$articleId = $argv['articleid'];
	}

	$dbr = wfGetDB( DB_SLAVE );

	$res = $dbr->select(
		'vulntags',
		array( 'vt_tid', 'vt_tag_text' ),
		array( "vt_article_id = $articleId" ),
		__METHOD__
	);

	$tags = array();

	foreach ( $res as $tag ) {
		$tags[] = "<li class='tag' id='tag-".$tag->vt_tid."'>".$tag->vt_tag_text."</li>";
	}

	return "<ul id='vuln-tag-list' class='tags-for-$articleId'>" . implode( "\n", $tags ) . "</ul>";
}