Skip to content

Commit

Permalink
Do not expose $_SERVER in templates by default
Browse files Browse the repository at this point in the history
This is important primarily because $_SERVER may contain sensitive
data, like AWS access keys, which then can be accessed from user-
provided templates.

Fixes #114
  • Loading branch information
sanmai committed Feb 11, 2019
1 parent 107d483 commit 365953e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Liquid/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ public function __construct(array $assigns = array(), array $registers = array()
$this->assigns = array($assigns);
$this->registers = $registers;
$this->filterbank = new Filterbank($this);

// first empty array serves as source for overrides, e.g. as in TagDecrement
$this->environments = array(array(), $_SERVER);
$this->environments = array(array(), array());

if (Liquid::get('EXPOSE_SERVER')) {
$this->environments[1] = $_SERVER;
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Liquid/Liquid.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ class Liquid
'PAGINATION_REQUEST_KEY' => 'page',

// The name of the context key used to denote the current page number
'PAGINATION_CONTEXT_KEY' => 'page'
'PAGINATION_CONTEXT_KEY' => 'page',

// Whenever variables from $_SERVER should be directly available to templates
'EXPOSE_SERVER' => false,
);

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/Liquid/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,28 @@ public function testGetNoOverride()
$context->set('test', 'test');
$this->assertEquals('test', $context->get('test'));
}

public function testServerNotExposedByDefault()
{
$_SERVER['AWS_SECRET_ACCESS_KEY'] = 'super_secret';

$context = new Context();
$this->assertNull($context->get('AWS_SECRET_ACCESS_KEY'));

$context->set('AWS_SECRET_ACCESS_KEY', 'test');
$this->assertEquals('test', $context->get('AWS_SECRET_ACCESS_KEY'));
}

public function testServerExposedWhenRequested()
{
Liquid::set('EXPOSE_SERVER', true);

$_SERVER['AWS_SECRET_ACCESS_KEY'] = 'super_secret';

$context = new Context();
$this->assertEquals('super_secret', $context->get('AWS_SECRET_ACCESS_KEY'));

$context->set('AWS_SECRET_ACCESS_KEY', 'test');
$this->assertEquals('super_secret', $context->get('AWS_SECRET_ACCESS_KEY'), '$_SERVER should take precedence in this case');
}
}
1 change: 1 addition & 0 deletions tests/Liquid/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected function setUp()
'VARIABLE_START' => '{{',
'VARIABLE_END' => '}}',
'VARIABLE_NAME' => '[a-zA-Z_][a-zA-Z0-9_.-]*',
'EXPOSE_SERVER' => false,
);

foreach ($defaultConfig as $configKey => $configValue) {
Expand Down

0 comments on commit 365953e

Please sign in to comment.