Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Strip Tags in New Input Implementation #1252

Merged
merged 2 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/UI/Implementation/Component/Input/Field/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ private function getOperations() {
* @inheritdoc
*/
final public function getContent() {
if (is_null($this->content)) {
throw new \LogicException("No content of this field has been evaluated yet. Seems withInput was not called.");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, good one.

return $this->content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function withInput(PostData $input);
/**
* Get the current content of the input.
*
* @return Result|null
* @return Result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

*/
public function getContent();
}
18 changes: 18 additions & 0 deletions src/UI/Implementation/Component/Input/Field/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@
namespace ILIAS\UI\Implementation\Component\Input\Field;

use ILIAS\UI\Component as C;
use ILIAS\Data\Factory as DataFactory;
use ILIAS\Transformation\Factory as TransformationFactory;
use ILIAS\Validation\Factory as ValidationFactory;

/**
* This implements the text input.
*/
class Text extends Input implements C\Input\Field\Text {
/**
* @inheritdoc
*/
public function __construct(
DataFactory $data_factory,
ValidationFactory $validation_factory,
TransformationFactory $transformation_factory,
$label,
$byline
) {
parent::__construct($data_factory, $validation_factory, $transformation_factory, $label, $byline);
$this->setAdditionalTransformation($transformation_factory->custom(function($v) {
return strip_tags($v);
}));
}

/**
* @inheritdoc
Expand Down
20 changes: 19 additions & 1 deletion src/UI/Implementation/Component/Input/Field/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,35 @@

use ILIAS\UI\Component as C;
use ILIAS\UI\Implementation\Component\JavaScriptBindable;
use ILIAS\Data\Factory as DataFactory;
use ILIAS\Transformation\Factory as TransformationFactory;
use ILIAS\Validation\Factory as ValidationFactory;

/**
* This implements the textarea input.
*/
class Textarea extends Input implements C\Input\Field\Textarea {

use JavaScriptBindable;

protected $max_limit;
protected $min_limit;

/**
* @inheritdoc
*/
public function __construct(
DataFactory $data_factory,
ValidationFactory $validation_factory,
TransformationFactory $transformation_factory,
$label,
$byline
) {
parent::__construct($data_factory, $validation_factory, $transformation_factory, $label, $byline);
$this->setAdditionalTransformation($transformation_factory->custom(function($v) {
return strip_tags($v);
}));
}

/**
* set maximum number of characters
* @param $max_limit
Expand Down
4 changes: 3 additions & 1 deletion tests/UI/Component/Input/Field/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ public function test_withError() {


public function test_getContent() {
$this->assertEquals(null, $this->input->getContent());
$this->expectException(\LogicException::class);

$this->input->getContent();
}


Expand Down
11 changes: 11 additions & 0 deletions tests/UI/Component/Input/Field/TextInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,15 @@ public function test_value_required() {
$value2 = $text2->getContent();
$this->assertTrue($value2->isError());
}

public function test_stripsTags() {
$f = $this->buildFactory();
$name = "name_0";
$text = $f->text("")
->withNameFrom($this->name_source)
->withInput(new DefPostData([$name => "<script>alert()</script>"]));

$content = $text->getContent();
$this->assertEquals("alert()", $content->value());
}
}
13 changes: 12 additions & 1 deletion tests/UI/Component/Input/Field/TextareaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,15 @@ public function test_renderer_with_error()
$expected = trim(preg_replace('/\t+/', '', $expected));
$this->assertEquals($expected, $html);
}
}

public function test_stripsTags() {
$f = $this->buildFactory();
$name = "name_0";
$text = $f->textarea("")
->withNameFrom($this->name_source)
->withInput(new DefPostData([$name => "<script>alert()</script>"]));

$content = $text->getContent();
$this->assertEquals("alert()", $content->value());
}
}