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 ( 4e592b...d43698 )
by François
02:20
created

StaticConfig::setStaticAddresses()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 35
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 35
rs 8.439
cc 6
eloc 16
nc 6
nop 2
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
173
            if ($this->poolRange->inRange($v4)) {
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
            // cannot be network address of poolRange
183
            if ($v4 === $this->poolRange->getNetwork()) {
184
                throw new RuntimeException('IP cannot be network address of poolRange');
185
            }
186
187
            // XXX make sure it is not already in use by another config, it is
188
            // okay if it is this config!
189
        }
190
        $clientConfig = $this->parseConfig($commonName);
191
        $clientConfig['v4'] = $v4;
192
        $this->writeFile($commonName, $clientConfig);
193
194
        return true;
195
    }
196
197
    private function writeFile($commonName, array $clientConfig)
198
    {
199
        $commonNamePath = sprintf('%s/%s', $this->staticConfigDir, $commonName);
200
201
        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...
202
            throw new RuntimeException('unable to write to static configuration file');
203
        }
204
    }
205
}
206