GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a78803...190821 )
by François
02:07
created

StaticConfig::enableCommonName()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 16
loc 16
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
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
namespace fkooman\VPN\Server;
18
19
use fkooman\Json\Json;
20
use RuntimeException;
21
22
/**
23
 * Manage static configuration for configurations.
24
 * Features:
25
 * - disable a configuration based on CN
26
 * - set static IPv4/IPv6 address for a CN.
27
 */
28
class StaticConfig
29
{
30
    /** @var string */
31
    private $staticConfigDir;
32
33
    /** @var IP */
34
    private $ipRange;
35
36
    /** @var IP */
37
    private $poolRange;
38
39
    public function __construct($staticConfigDir, IP $ipRange, IP $poolRange)
40
    {
41
        $this->staticConfigDir = $staticConfigDir;
42
        $this->ipRange = $ipRange;
43
        $this->poolRange = $poolRange;
44
    }
45
46
    private function parseConfig($commonName)
47
    {
48
        $commonNamePath = sprintf('%s/%s', $this->staticConfigDir, $commonName);
49
        // XXX do something if file does not exist
50
        try {
51
            return Json::decodeFile($commonNamePath);
0 ignored issues
show
Security File Exposure introduced by
$commonNamePath can 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:

$sanitized = (integer) $tainted;
Loading history...
52
        } catch (RuntimeException $e) {
53
            return [];
54
        }
55
    }
56
57 View Code Duplication
    public function disableCommonName($commonName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        Utils::validateCommonName($commonName);
60
61
        $clientConfig = $this->parseConfig($commonName);
62
        if (array_key_exists('disable', $clientConfig) && $clientConfig['disable']) {
63
            // already disabled
64
            return false;
65
        }
66
67
        $clientConfig['disable'] = true;
68
        $this->writeFile($commonName, $clientConfig);
69
70
        return true;
71
    }
72
73 View Code Duplication
    public function enableCommonName($commonName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        Utils::validateCommonName($commonName);
76
77
        $clientConfig = $this->parseConfig($commonName);
78
        if (array_key_exists('disable', $clientConfig) && $clientConfig['disable']) {
79
            // it is disabled, enable it
80
            $clientConfig['disable'] = false;
81
            $this->writeFile($commonName, $clientConfig);
82
83
            return true;
84
        }
85
86
        // it is not disabled
87
        return false;
88
    }
89
90
    public function isDisabled($commonName)
91
    {
92
        Utils::validateCommonName($commonName);
93
94
        $clientConfig = $this->parseConfig($commonName);
95
96
        return array_key_exists('disable', $clientConfig) && $clientConfig['disable'];
97
    }
98
99
    public function getDisabledCommonNames($userId = null)
100
    {
101
        if (!is_null($userId)) {
102
            Utils::validateUserId($userId);
103
        }
104
105
        $disabledCommonNames = array();
106
        $pathFilter = sprintf('%s/*', $this->staticConfigDir);
107
        if (!is_null($userId)) {
108
            $pathFilter = sprintf('%s/%s_*', $this->staticConfigDir, $userId);
109
        }
110
111
        foreach (glob($pathFilter) as $commonNamePath) {
0 ignored issues
show
Security File Exposure introduced by
$pathFilter can 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:

$sanitized = (integer) $tainted;
Loading history...
112
            $commonName = basename($commonNamePath);
113
114
            if ($this->isDisabled($commonName)) {
115
                $disabledCommonNames[] = $commonName;
116
            }
117
        }
118
119
        return $disabledCommonNames;
120
    }
121
122
    public function getStaticAddresses($userId = null)
123
    {
124
        if (!is_null($userId)) {
125
            Utils::validateUserId($userId);
126
        }
127
128
        $staticAddresses = array();
129
        $pathFilter = sprintf('%s/*', $this->staticConfigDir);
130
        if (!is_null($userId)) {
131
            $pathFilter = sprintf('%s/%s_*', $this->staticConfigDir, $userId);
132
        }
133
134
        foreach (glob($pathFilter) as $commonNamePath) {
0 ignored issues
show
Security File Exposure introduced by
$pathFilter can 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:

$sanitized = (integer) $tainted;
Loading history...
135
            $commonName = basename($commonNamePath);
136
            $ip = $this->getStaticAddress($commonName);
137
            if (!is_null($ip['v4'])) {
138
                $staticAddresses[$commonName] = $ip;
139
            }
140
        }
141
142
        return $staticAddresses;
143
    }
144
145
    public function getStaticAddress($commonName)
146
    {
147
        Utils::validateCommonName($commonName);
148
149
        $clientConfig = $this->parseConfig($commonName);
150
151
        $v4 = null;
152
        if (array_key_exists('v4', $clientConfig)) {
153
            $v4 = $clientConfig['v4'];
154
        }
155
156
        return array(
157
            'v4' => $v4,
158
        );
159
    }
160
161
    public function setStaticAddresses($commonName, $v4)
162
    {
163
        Utils::validateCommonName($commonName);
164
        if (!is_null($v4)) {
165
            Utils::validateAddress($v4);
166
167
            // IP MUST be in ipRange
168
            if (!$this->ipRange->inRange($v4)) {
169
                throw new RuntimeException('IP is out of range');
170
            }
171
172
            // IP MUST NOT be in poolRange (including network and broadcast
173
            if ($this->poolRange->inRange($v4, true)) {
174
                throw new RuntimeException('IP cannot be in poolRange');
175
            }
176
177
            // cannot be server IP (we assume for now firstHost is server IP
178
            if ($v4 === $this->ipRange->getFirstHost()) {
179
                throw new RuntimeException('IP cannot be server IP');
180
            }
181
182
            // XXX make sure it is not already in use by another config, it is
183
            // okay if it is this config!
184
            $staticAddresses = $this->getStaticAddresses();
185
            foreach ($staticAddresses as $cn => $c) {
186
                if ($c['v4'] === $v4) {
187
                    if ($commonName === $cn) {
188
                        // the commonName is allowed to have the same address,
189
                        // i.e. when setting the same IPv4 address that was 
190
                        // already assigned to that CN
191
                        continue;
192
                    }
193
194
                    throw new RuntimeException(sprintf('IP address already in use by "%s"', $cn));
195
                }
196
            }
197
        }   
198
        $clientConfig = $this->parseConfig($commonName);
199
        $clientConfig['v4'] = $v4;
200
        $this->writeFile($commonName, $clientConfig);
201
202
        return true;
203
    }
204
205
    private function writeFile($commonName, array $clientConfig)
206
    {
207
        $commonNamePath = sprintf('%s/%s', $this->staticConfigDir, $commonName);
208
209
        if (false === @file_put_contents($commonNamePath, Json::encode($clientConfig))) {
0 ignored issues
show
Security File Manipulation introduced by
$commonNamePath can contain request data and is used in file manipulation 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:

$sanitized = (integer) $tainted;
Loading history...
210
            throw new RuntimeException('unable to write to static configuration file');
211
        }
212
    }
213
}
214