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 ( e09c6b...714f59 )
by François
03:38
created

StaticConfig::disableCommonName()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 15
loc 15
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
    public function __construct($staticConfigDir)
34
    {
35
        $this->staticConfigDir = $staticConfigDir;
36
    }
37
38
    private function parseConfig($commonName)
39
    {
40
        $commonNamePath = sprintf('%s/%s', $this->staticConfigDir, $commonName);
41
        // XXX do something if file does not exist
42
        try {
43
            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...
44
        } catch (RuntimeException $e) {
45
            return [];
46
        }
47
    }
48
49 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...
50
    {
51
        Utils::validateCommonName($commonName);
52
53
        $clientConfig = $this->parseConfig($commonName);
54
        if (array_key_exists('disable', $clientConfig) && $clientConfig['disable']) {
55
            // already disabled
56
            return false;
57
        }
58
59
        $clientConfig['disable'] = true;
60
        $this->writeFile($commonName, $clientConfig);
61
62
        return true;
63
    }
64
65 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...
66
    {
67
        Utils::validateCommonName($commonName);
68
69
        $clientConfig = $this->parseConfig($commonName);
70
        if (array_key_exists('disable', $clientConfig) && $clientConfig['disable']) {
71
            // it is disabled, enable it
72
            $clientConfig['disable'] = false;
73
            $this->writeFile($commonName, $clientConfig);
74
75
            return true;
76
        }
77
78
        // it is not disabled
79
        return false;
80
    }
81
82
    public function isDisabled($commonName)
83
    {
84
        Utils::validateCommonName($commonName);
85
86
        $clientConfig = $this->parseConfig($commonName);
87
88
        return array_key_exists('disable', $clientConfig) && $clientConfig['disable'];
89
    }
90
91
    public function getDisabledCommonNames($userId = null)
92
    {
93
        if (!is_null($userId)) {
94
            Utils::validateUserId($userId);
95
        }
96
97
        $disabledCommonNames = array();
98
        $pathFilter = sprintf('%s/*', $this->staticConfigDir);
99
        if (!is_null($userId)) {
100
            $pathFilter = sprintf('%s/%s_*', $this->staticConfigDir, $userId);
101
        }
102
103
        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...
104
            $commonName = basename($commonNamePath);
105
106
            if ($this->isDisabled($commonName)) {
107
                $disabledCommonNames[] = $commonName;
108
            }
109
        }
110
111
        return $disabledCommonNames;
112
    }
113
114
    public function getStaticAddresses($commonName)
115
    {
116
        Utils::validateCommonName($commonName);
117
118
        $clientConfig = $this->parseConfig($commonName);
119
120
        $v4 = null;
121
        if (array_key_exists('v4', $clientConfig)) {
122
            $v4 = $clientConfig['v4'];
123
        }
124
        $v6 = null;
125
        if (array_key_exists('v6', $clientConfig)) {
126
            $v6 = $clientConfig['v6'];
127
        }
128
129
        return array(
130
            'v4' => $v4,
131
            'v6' => $v6,
132
        );
133
    }
134
135
    public function setStaticAddresses($commonName, $v4, $v6)
136
    {
137
        Utils::validateCommonName($commonName);
138
139
        $clientConfig = $this->parseConfig($commonName);
140
141
        $clientConfig['v4'] = $v4;
142
        $clientConfig['v6'] = $v6;
143
        $this->writeFile($commonName, $clientConfig);
144
145
        return true;
146
    }
147
148
    private function writeFile($commonName, array $clientConfig)
149
    {
150
        $commonNamePath = sprintf('%s/%s', $this->staticConfigDir, $commonName);
151
152
        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...
153
            throw new RuntimeException('unable to write to static configuration file');
154
        }
155
    }
156
}
157