|
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\CnConfig; |
|
19
|
|
|
|
|
20
|
|
|
use fkooman\Http\Request; |
|
21
|
|
|
use fkooman\Rest\Service; |
|
22
|
|
|
use fkooman\Rest\ServiceModuleInterface; |
|
23
|
|
|
use fkooman\Http\JsonResponse; |
|
24
|
|
|
use Psr\Log\LoggerInterface; |
|
25
|
|
|
use fkooman\VPN\Server\InputValidation; |
|
26
|
|
|
use fkooman\Json\Json; |
|
27
|
|
|
use fkooman\Rest\Plugin\Authentication\Bearer\TokenInfo; |
|
28
|
|
|
use fkooman\IO\IOInterface; |
|
29
|
|
|
use fkooman\VPN\Server\Utils; |
|
30
|
|
|
|
|
31
|
|
|
class CnConfigModule implements ServiceModuleInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var string */ |
|
34
|
|
|
private $configDir; |
|
35
|
|
|
|
|
36
|
|
|
/** @var \Psr\Log\LoggerInterface */ |
|
37
|
|
|
private $logger; |
|
38
|
|
|
|
|
39
|
|
|
/** @var \fkooman\IO\IOInterface */ |
|
40
|
|
|
private $io; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct($configDir, LoggerInterface $logger, IOInterface $io) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->configDir = $configDir; |
|
45
|
|
|
$this->logger = $logger; |
|
46
|
|
|
$this->io = $io; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function init(Service $service) |
|
50
|
|
|
{ |
|
51
|
|
|
// GET /config/common_names get all CNs |
|
52
|
|
|
// GET /config/common_names?user_id=foo get all CNs, but only for one user |
|
53
|
|
|
$service->get( |
|
54
|
|
|
'/config/common_names', |
|
55
|
|
|
function (Request $request, TokenInfo $tokenInfo) { |
|
56
|
|
|
Utils::requireScope($tokenInfo, ['admin', 'portal']); |
|
57
|
|
|
$userId = $request->getUrl()->getQueryParameter('user_id'); |
|
58
|
|
|
if (!is_null($userId)) { |
|
59
|
|
|
InputValidation::userId($userId); |
|
60
|
|
|
// filter the directory list if user_id was specified |
|
61
|
|
|
$fileFilter = sprintf('%s_*', $userId); |
|
62
|
|
|
} else { |
|
63
|
|
|
// only admin is allow to request all CNs for all users |
|
64
|
|
|
Utils::requireScope($tokenInfo, ['admin']); |
|
65
|
|
|
$fileFilter = '*'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$cnConfigArray = []; |
|
69
|
|
View Code Duplication |
foreach ($this->io->readFolder($this->configDir, $fileFilter) as $configFile) { |
|
|
|
|
|
|
70
|
|
|
$cnConfig = new CnConfig( |
|
71
|
|
|
Json::decode( |
|
72
|
|
|
$this->io->readFile($configFile) |
|
73
|
|
|
) |
|
74
|
|
|
); |
|
75
|
|
|
$cnConfigArray[basename($configFile)] = $cnConfig->toArray(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$response = new JsonResponse(); |
|
79
|
|
|
$response->setBody($cnConfigArray); |
|
80
|
|
|
|
|
81
|
|
|
return $response; |
|
82
|
|
|
} |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
// GET /config/common_names/:commonName get a particular CN |
|
86
|
|
|
$service->get( |
|
87
|
|
|
'/config/common_names/:commonName', |
|
88
|
|
View Code Duplication |
function (Request $request, TokenInfo $tokenInfo, $commonName) { |
|
|
|
|
|
|
89
|
|
|
Utils::requireScope($tokenInfo, ['admin']); |
|
90
|
|
|
InputValidation::commonName($commonName); |
|
91
|
|
|
|
|
92
|
|
|
$fileName = sprintf('%s/%s', $this->configDir, $commonName); |
|
93
|
|
|
if (!$this->io->isFile($fileName)) { |
|
94
|
|
|
// if the file does not exist, use default values |
|
95
|
|
|
$cnConfig = new CnConfig([]); |
|
96
|
|
|
} else { |
|
97
|
|
|
$cnConfig = new CnConfig( |
|
98
|
|
|
Json::decode( |
|
99
|
|
|
$this->io->readFile( |
|
100
|
|
|
sprintf('%s/%s', $this->configDir, $commonName) |
|
101
|
|
|
) |
|
102
|
|
|
) |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$response = new JsonResponse(); |
|
107
|
|
|
$response->setBody($cnConfig->toArray()); |
|
108
|
|
|
|
|
109
|
|
|
return $response; |
|
110
|
|
|
} |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
// PUT /config/common_names/:commonName set new config for CN |
|
114
|
|
|
$service->put( |
|
115
|
|
|
'/config/common_names/:commonName', |
|
116
|
|
View Code Duplication |
function (Request $request, TokenInfo $tokenInfo, $commonName) { |
|
|
|
|
|
|
117
|
|
|
Utils::requireScope($tokenInfo, ['admin']); |
|
118
|
|
|
InputValidation::commonName($commonName); |
|
119
|
|
|
|
|
120
|
|
|
// we wrap the request body in an CnConfig object to validate |
|
121
|
|
|
// whatever is there |
|
122
|
|
|
$requestCnConfig = new CnConfig(Json::decode($request->getBody())); |
|
123
|
|
|
|
|
124
|
|
|
$this->io->writeFile( |
|
125
|
|
|
sprintf('%s/%s', $this->configDir, $commonName), |
|
126
|
|
|
Json::encode($requestCnConfig->toArray()) |
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
$response = new JsonResponse(); |
|
130
|
|
|
$response->setBody(['ok' => true]); |
|
131
|
|
|
|
|
132
|
|
|
return $response; |
|
133
|
|
|
} |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
$fileFiltercan 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: