|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2015 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace fkooman\VPN\Server\Config; |
|
19
|
|
|
|
|
20
|
|
|
use fkooman\Json\Json; |
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
|
|
23
|
|
|
class FileConfigStorage implements ConfigStorageInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $staticConfigDir; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($staticConfigDir) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->staticConfigDir = $staticConfigDir; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getConfig($commonName) |
|
34
|
|
|
{ |
|
35
|
|
|
$commonNamePath = sprintf('%s/%s', $this->staticConfigDir, $commonName); |
|
36
|
|
|
try { |
|
37
|
|
|
return new ConfigData(Json::decodeFile($commonNamePath)); |
|
38
|
|
|
} catch (RuntimeException $e) { |
|
39
|
|
|
return new ConfigData([]); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getAllConfig($userId) |
|
44
|
|
|
{ |
|
45
|
|
|
$configArray = []; |
|
46
|
|
|
$pathFilter = sprintf('%s/*', $this->staticConfigDir); |
|
47
|
|
|
if (!is_null($userId)) { |
|
48
|
|
|
$pathFilter = sprintf('%s/%s_*', $this->staticConfigDir, $userId); |
|
49
|
|
|
} |
|
50
|
|
|
foreach (glob($pathFilter) as $commonNamePath) { |
|
|
|
|
|
|
51
|
|
|
$commonName = basename($commonNamePath); |
|
52
|
|
|
$configArray[$commonName] = $this->getConfig($commonName)->toArray(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $configArray; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function setConfig($commonName, ConfigData $config) |
|
59
|
|
|
{ |
|
60
|
|
|
$commonNamePath = sprintf('%s/%s', $this->staticConfigDir, $commonName); |
|
61
|
|
|
|
|
62
|
|
|
if (false === @file_put_contents($commonNamePath, Json::encode($config->toArray()))) { |
|
63
|
|
|
throw new RuntimeException('unable to write to static configuration file'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
$pathFiltercan contain request data and is used in file inclusion context(s) leading to a potential security vulnerability.General Strategies to prevent injection
In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) { throw new \InvalidArgumentException('This input is not allowed.'); }For numeric data, we recommend to explicitly cast the data: