Passed
Push — master ( bd880f...4484cd )
by Tim
01:58
created

Debug::getValue()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 26
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\saml2debug\Controller;
6
7
use SimpleSAML\Configuration;
8
use SimpleSAML\Error;
9
use SimpleSAML\Module;
10
use SimpleSAML\Session;
11
use SimpleSAML\XHTML\Template;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
/**
16
 * Controller class for the saml2debug module.
17
 *
18
 * This class serves the different views available in the module.
19
 *
20
 * @package simplesamlphp/simplesamlphp-module-saml2debug
21
 */
22
class Debug
23
{
24
    /** @var \SimpleSAML\Configuration */
25
    protected Configuration $config;
26
27
    /** @var \SimpleSAML\Session */
28
    protected Session $session;
29
30
31
    /**
32
     * Controller constructor.
33
     *
34
     * It initializes the global configuration and session for the controllers implemented here.
35
     *
36
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
37
     * @param \SimpleSAML\Session $session The session to use by the controllers.
38
     *
39
     * @throws \Exception
40
     */
41
    public function __construct(
42
        Configuration $config,
43
        Session $session
44
    ) {
45
        $this->config = $config;
46
        $this->session = $session;
47
    }
48
49
50
    /**
51
     * Show SAML2 debugging info.
52
     *
53
     * @param \Symfony\Component\HttpFoundation\Request $request
54
     * @return \SimpleSAML\XHTML\Template
55
     * @throws \Exception
56
     */
57
    public function main(Request $request): Template
58
    {
59
        $decoded = '';
60
        $encoded = 'fZJNT%2BMwEIbvSPwHy%2Fd8tMvHympSdUGISuwS0cCBm%2BtMUwfbk%2FU4zfLvSVMq2Euv45n3fd7xzOb%2FrGE78KTRZXwSp5yBU1hp' .
61
                   'V2f8ubyLfvJ5fn42I2lNKxZd2Lon%2BNsBBTZMOhLjQ8Y77wRK0iSctEAiKLFa%2FH4Q0zgVrceACg1ny9uMy7rCdaM2%2Bs0BWrtppK2U' .
62
                   'AdeoVjW2ruq1bevGImcvR6zpHmtJ1MHSUZAuDKU0vY7Si2h6VU5%2BiMuJuLx65az4dPql3SHBKaz1oYnEfVkWUfG4KkeBna7A%2Fxm6M1' .
63
                   '4j1gZihZazBRH4MODcoKPOgl%2BB32kFz08PGd%2BG0JJIkr7v46%2BhRCaEpod17DCRivYZCkmkd4N28B3wfNyrGKP5bws9DS6PKDz%2F' .
64
                   'Mpsl36Tyz%2F%2Fax1jeFmi0emcLY7C%2F8SDD0Z7dobcynHbbV3QVbcZW0TlqQemNhoqzJD%2B4%2Fn8Yw7l8AA%3D%3D';
65
66
        $activeTab = 0;
67
68
        if ($request->query->has('encoded')) {
69
            if (!$request->query->has('binding')) {
70
                throw new Error\BadRequest('Missing binding');
71
            }
72
            $decoded = $this->decode($request->query->get('encoded'), $request->query->get('binding'));
0 ignored issues
show
Unused Code introduced by
The call to SimpleSAML\Module\saml2d...troller\Debug::decode() has too many arguments starting with $request->query->get('binding'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
            /** @scrutinizer ignore-call */ 
73
            $decoded = $this->decode($request->query->get('encoded'), $request->query->get('binding'));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
73
            $activeTab = 1;
74
        }
75
76
        if ($request->query->has('decoded')) {
77
            $encoded = $this->encode($request->query->get('decoded'));
0 ignored issues
show
Bug introduced by
The call to SimpleSAML\Module\saml2d...troller\Debug::encode() has too few arguments starting with binding. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            /** @scrutinizer ignore-call */ 
78
            $encoded = $this->encode($request->query->get('decoded'));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
78
        }
79
80
        $t = new Template($this->config, 'saml2debug:debug.twig');
81
        $t->data['encoded'] = $encoded;
82
        $t->data['decoded'] = $decoded;
83
        $t->data['activeTab'] = $activeTab;
84
85
        return $t;
86
    }
87
88
89
    /**
90
     * @param string $raw
91
     * @return string
92
     */
93
    private function getValue(string $raw): string
94
    {
95
        $val = $raw;
96
97
        $url = parse_url($raw, PHP_URL_QUERY);
98
        if (!empty($url)) {
99
            $val = $url;
100
        }
101
102
        $arr = [];
103
        parse_str($val, $arr);
104
105
        if (array_key_exists('SAMLResponse', $arr)) {
106
            return $arr['SAMLResponse'];
107
        }
108
        if (array_key_exists('SAMLRequest', $arr)) {
109
            return $arr['SAMLRequest'];
110
        }
111
        if (array_key_exists('LogoutRequest', $arr)) {
112
            return $arr['LogoutRequest'];
113
        }
114
        if (array_key_exists('LogoutResponse', $arr)) {
115
            return $arr['LogoutResponse'];
116
        }
117
118
        return rawurldecode(stripslashes($val));
119
    }
120
121
122
    /**
123
     * @param string $raw
124
     * @return string
125
     */
126
    private function decode(string $raw): string
127
    {
128
        $message = $this->getValue($raw);
129
130
        $base64decoded = base64_decode($message);
131
        $gzinflated = gzinflate($base64decoded);
132
        if ($gzinflated !== false) {
133
            $base64decoded = $gzinflated;
134
        }
135
        $decoded = htmlspecialchars($base64decoded);
136
        return $decoded;
137
    }
138
139
140
    /**
141
     * @param string $message
142
     * @param string $binding
143
     * @return string
144
     */
145
    private function encode(string $message, string $binding): string
146
    {
147
        if ($binding === 'redirect') {
148
            return urlencode(base64_encode(gzdeflate(stripslashes($message))));
149
        } else {
150
            return base64_encode(stripslashes($message));
151
        }
152
    }
153
}
154