Skip to content

Commit

Permalink
[ss-2017-007] Ensure xls formulae are safely sanitised on output
Browse files Browse the repository at this point in the history
CSVParser now strips leading tabs on cells
  • Loading branch information
Damian Mooyman committed Nov 30, 2017
1 parent d57dea0 commit cfe1d4f
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 68 deletions.
4 changes: 3 additions & 1 deletion src/Dev/CSVParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ protected function fetchCSVRow()
array($this->enclosure, $this->delimiter),
$value
);

// Trim leading tab
// [SS-2017-007] Ensure all cells with leading [@=+] have a leading tab
$value = ltrim($value, "\t");
if (array_key_exists($i, $this->headerRow)) {
if ($this->headerRow[$i]) {
$row[$this->headerRow[$i]] = $value;
Expand Down
19 changes: 17 additions & 2 deletions src/Forms/GridField/GridFieldExportButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\DataObject;

/**
* Adds an "Export list" button to the bottom of a {@link GridField}.
*/
class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler
{

/**
* @var array Map of a property name on the exported objects, with values being the column title in the CSV file.
* Note that titles are only used when {@link $csvHasHeader} is set to TRUE.
Expand All @@ -38,6 +38,15 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
*/
protected $targetFragment;

/**
* Set to true to disable XLS sanitisation
* [SS-2017-007] Ensure all cells with leading [@=+] have a leading tab
*
* @config
* @var bool
*/
private static $xls_export_disabled = false;

/**
* @param string $targetFragment The HTML fragment to write the button into
* @param array $exportColumns The columns to include in the export
Expand Down Expand Up @@ -170,7 +179,7 @@ public function generateExportFileData($gridField)
}

//Remove GridFieldPaginator as we're going to export the entire list.
$gridField->getConfig()->removeComponentsByType('SilverStripe\\Forms\\GridField\\GridFieldPaginator');
$gridField->getConfig()->removeComponentsByType(GridFieldPaginator::class);

$items = $gridField->getManipulatedList();

Expand Down Expand Up @@ -203,6 +212,12 @@ public function generateExportFileData($gridField)
}
}

// [SS-2017-007] Sanitise XLS executable column values with a leading tab
if (!Config::inst()->get(get_class($this), 'xls_export_disabled')
&& preg_match('/^[-@=+].*/', $value)
) {
$value = "\t" . $value;
}
$columnData[] = $value;
}

Expand Down
87 changes: 53 additions & 34 deletions tests/php/Dev/CSVParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,44 @@ public function testParsingWithHeaders()
$firstNames = $birthdays = $biographies = $registered = array();
foreach ($csv as $record) {
/* Each row in the CSV file will be keyed with the header row */
$this->assertEquals(array('FirstName','Biography','Birthday','IsRegistered'), array_keys($record));
$this->assertEquals(
['FirstName','Biography','Birthday','IsRegistered'],
array_keys($record)
);
$firstNames[] = $record['FirstName'];
$biographies[] = $record['Biography'];
$birthdays[] = $record['Birthday'];
$registered[] = $record['IsRegistered'];
}

$this->assertEquals(array('John','Jane','Jamie','Järg'), $firstNames);
$this->assertEquals(
['John','Jane','Jamie','Järg','Jacob'],
$firstNames
);

$this->assertEquals(
array(
"He's a good guy",
"She is awesome." . PHP_EOL
. "So awesome that she gets multiple rows and \"escaped\" strings in her biography",
"Pretty old, with an escaped comma",
"Unicode FTW"),
[
"He's a good guy",
"She is awesome."
. PHP_EOL
. "So awesome that she gets multiple rows and \"escaped\" strings in her biography",
"Pretty old, with an escaped comma",
"Unicode FTW",
"Likes leading tabs in his biography",
],
$biographies
);
$this->assertEquals([
"1988-01-31",
"1982-01-31",
"1882-01-31",
"1982-06-30"
"1982-06-30",
"2000-04-30",
], $birthdays);
$this->assertEquals(array('1', '0', '1', '1'), $registered);
$this->assertEquals(
['1', '0', '1', '1', '0'],
$registered
);
}

public function testParsingWithHeadersAndColumnMap()
Expand All @@ -62,41 +75,43 @@ public function testParsingWithHeadersAndColumnMap()
$csv = new CSVParser($this->csvPath . 'PlayersWithHeader.csv');

/* We can set up column remapping. The keys are case-insensitive. */
$csv->mapColumns(
array(
$csv->mapColumns([
'FirstName' => '__fn',
'bIoGrApHy' => '__BG',
)
);
]);

$firstNames = $birthdays = $biographies = $registered = array();
foreach ($csv as $record) {
/* Each row in the CSV file will be keyed with the renamed columns. Any unmapped column names will be
* left as-is. */
$this->assertEquals(array('__fn','__BG','Birthday','IsRegistered'), array_keys($record));
$this->assertEquals(['__fn','__BG','Birthday','IsRegistered'], array_keys($record));
$firstNames[] = $record['__fn'];
$biographies[] = $record['__BG'];
$birthdays[] = $record['Birthday'];
$registered[] = $record['IsRegistered'];
}

$this->assertEquals(array('John','Jane','Jamie','Järg'), $firstNames);
$this->assertEquals(['John','Jane','Jamie','Järg','Jacob'], $firstNames);
$this->assertEquals(
array(
"He's a good guy",
"She is awesome."
. PHP_EOL . "So awesome that she gets multiple rows and \"escaped\" strings in her biography",
"Pretty old, with an escaped comma",
"Unicode FTW"),
[
"He's a good guy",
"She is awesome."
. PHP_EOL
. "So awesome that she gets multiple rows and \"escaped\" strings in her biography",
"Pretty old, with an escaped comma",
"Unicode FTW",
"Likes leading tabs in his biography",
],
$biographies
);
$this->assertEquals([
"1988-01-31",
"1982-01-31",
"1882-01-31",
"1982-06-30"
"1982-06-30",
"2000-04-30",
], $birthdays);
$this->assertEquals(array('1', '0', '1', '1'), $registered);
$this->assertEquals(array('1', '0', '1', '1', '0'), $registered);
}

public function testParsingWithExplicitHeaderRow()
Expand All @@ -117,24 +132,28 @@ public function testParsingWithExplicitHeaderRow()
}

/* And the first row will be returned in the data */
$this->assertEquals(array('FirstName','John','Jane','Jamie','Järg'), $firstNames);
$this->assertEquals(['FirstName','John','Jane','Jamie','Järg','Jacob'], $firstNames);
$this->assertEquals(
array(
'Biography',
"He's a good guy",
"She is awesome." . PHP_EOL
. "So awesome that she gets multiple rows and \"escaped\" strings in her biography",
"Pretty old, with an escaped comma",
"Unicode FTW"),
[
'Biography',
"He's a good guy",
"She is awesome."
. PHP_EOL
. "So awesome that she gets multiple rows and \"escaped\" strings in her biography",
"Pretty old, with an escaped comma",
"Unicode FTW",
"Likes leading tabs in his biography"
],
$biographies
);
$this->assertEquals([
"Birthday",
"1988-01-31",
"1982-01-31",
"1882-01-31",
"1982-06-30"
"1982-06-30",
"2000-04-30",
], $birthdays);
$this->assertEquals(array('IsRegistered', '1', '0', '1', '1'), $registered);
$this->assertEquals(['IsRegistered', '1', '0', '1', '1', '0'], $registered);
}
}
6 changes: 3 additions & 3 deletions tests/php/Dev/CsvBulkLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testLoad()
$results = $loader->load($filepath);

// Test that right amount of columns was imported
$this->assertEquals(4, $results->Count(), 'Test correct count of imported data');
$this->assertEquals(5, $results->Count(), 'Test correct count of imported data');

// Test that columns were correctly imported
$obj = DataObject::get_one(
Expand All @@ -76,15 +76,15 @@ public function testDeleteExistingRecords()
$filepath = $this->csvPath . 'PlayersWithHeader.csv';
$loader->deleteExistingRecords = true;
$results1 = $loader->load($filepath);
$this->assertEquals(4, $results1->Count(), 'Test correct count of imported data on first load');
$this->assertEquals(5, $results1->Count(), 'Test correct count of imported data on first load');

//delete existing data before doing second CSV import
$results2 = $loader->load($filepath);
//get all instances of the loaded DataObject from the database and count them
$resultDataObject = DataObject::get(Player::class);

$this->assertEquals(
4,
5,
$resultDataObject->count(),
'Test if existing data is deleted before new data is added'
);
Expand Down
1 change: 1 addition & 0 deletions tests/php/Dev/CsvBulkLoaderTest/csv/PlayersWithHeader.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
So awesome that she gets multiple rows and \"escaped\" strings in her biography","1982-01-31","0"
"Jamie","Pretty old\, with an escaped comma","1882-01-31","1"
"Järg","Unicode FTW","1982-06-30","1"
"Jacob"," Likes leading tabs in his biography","2000-04-30","0"
68 changes: 40 additions & 28 deletions tests/php/Forms/GridField/GridFieldExportButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@
use SilverStripe\Forms\GridField\GridFieldExportButton;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\ORM\FieldType\DBField;

class GridFieldExportButtonTest extends SapphireTest
{

/**
* @var DataList
*/
protected $list;

/**
* @var GridField
*/
protected $gridField;

protected $form;

protected static $fixture_file = 'GridFieldExportButtonTest.yml';

protected static $extra_dataobjects = array(
protected static $extra_dataobjects = [
Team::class,
NoView::class,
);
];

protected function setUp()
{
Expand All @@ -44,7 +49,7 @@ public function testCanView()
$list = new DataList(NoView::class);

$button = new GridFieldExportButton();
$button->setExportColumns(array('Name' => 'My Name'));
$button->setExportColumns(['Name' => 'My Name']);

$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton());
$gridField = new GridField('testfield', 'testfield', $list, $config);
Expand All @@ -58,7 +63,7 @@ public function testCanView()
public function testGenerateFileDataBasicFields()
{
$button = new GridFieldExportButton();
$button->setExportColumns(array('Name' => 'My Name'));
$button->setExportColumns(['Name' => 'My Name']);

$this->assertEquals(
'"My Name"'."\n".
Expand All @@ -68,17 +73,32 @@ public function testGenerateFileDataBasicFields()
);
}

public function testXLSSanitisation()
{
// Create risky object
$object = new Team();
$object->Name = '=SUM(1, 2)';
$object->write();

// Export
$button = new GridFieldExportButton();
$button->setExportColumns(['Name' => 'My Name']);

$this->assertEquals(
"\"My Name\"\n\"\t=SUM(1, 2)\"\nTest\nTest2\n",
$button->generateExportFileData($this->gridField)
);
}

public function testGenerateFileDataAnonymousFunctionField()
{
$button = new GridFieldExportButton();
$button->setExportColumns(
array(
$button->setExportColumns([
'Name' => 'Name',
'City' => function ($obj) {
'City' => function (DBField $obj) {
return $obj->getValue() . ' city';
}
)
);
]);

$this->assertEquals(
'Name,City'."\n".
Expand All @@ -91,12 +111,10 @@ public function testGenerateFileDataAnonymousFunctionField()
public function testBuiltInFunctionNameCanBeUsedAsHeader()
{
$button = new GridFieldExportButton();
$button->setExportColumns(
array(
$button->setExportColumns([
'Name' => 'Name',
'City' => 'strtolower'
)
);
'City' => 'strtolower',
]);

$this->assertEquals(
'Name,strtolower'."\n".
Expand All @@ -109,12 +127,10 @@ public function testBuiltInFunctionNameCanBeUsedAsHeader()
public function testNoCsvHeaders()
{
$button = new GridFieldExportButton();
$button->setExportColumns(
array(
$button->setExportColumns([
'Name' => 'Name',
'City' => 'City'
)
);
'City' => 'City',
]);
$button->setCsvHasHeader(false);

$this->assertEquals(
Expand All @@ -132,9 +148,7 @@ public function testArrayListInput()
//Create an ArrayList 1 greater the Paginator's default 15 rows
$arrayList = new ArrayList();
for ($i = 1; $i <= 16; $i++) {
$dataobject = new DataObject(
array ( 'ID' => $i )
);
$dataobject = new DataObject(['ID' => $i]);
$arrayList->add($dataobject);
}
$this->gridField->setList($arrayList);
Expand Down Expand Up @@ -164,11 +178,9 @@ public function testArrayListInput()
public function testZeroValue()
{
$button = new GridFieldExportButton();
$button->setExportColumns(
array(
$button->setExportColumns([
'RugbyTeamNumber' => 'Rugby Team Number'
)
);
]);

$this->assertEquals(
"\"Rugby Team Number\"\n2\n0\n",
Expand Down

0 comments on commit cfe1d4f

Please sign in to comment.