Issues (116)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

spec/ClientSpec.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace spec\Scriptotek\Alma;
4
5
use GuzzleHttp\Psr7\Response;
6
use Http\Client\Common\Exception\ServerErrorException;
7
use Http\Mock\Client as MockHttp;
8
use PhpSpec\ObjectBehavior;
9
use Scriptotek\Alma\Exception\InvalidApiKey;
10
use Scriptotek\Alma\Exception\RequestFailed;
11
use Scriptotek\Alma\Exception\ResourceNotFound;
12
use Scriptotek\Alma\Zones;
13
14
function str_random()
15
{
16
    return strval(rand(10000, 99999));
17
}
18
19
class ClientSpec extends ObjectBehavior
20
{
21
    public function let()
22
    {
23
        $http = new MockHttp();
24
        $apiKey = 'DummyApiKey';
25
        $region = 'eu';
26
        $this->beConstructedWith($apiKey, $region, Zones::INSTITUTION, $http);
27
28
        return $http;
29
    }
30
31
    public function it_can_be_configured_with_custom_base_url()
32
    {
33
        $this->beConstructedWith('DummyKey');
34
        $this->setEntryPoint('http://proxy.foxy');
35
        $this->entrypoint->shouldBe('http://proxy.foxy');
0 ignored issues
show
The property entrypoint does not exist on object<spec\Scriptotek\Alma\ClientSpec>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
36
    }
37
38
39
    protected function httpWithResponseBody($body, $statusCode = 200, $headers = [])
40
    {
41
        $http = $this->let();
42
        $http->addResponse(new Response($statusCode, $headers, $body));
43
44
        return $http;
45
    }
46
47
    public function it_is_initializable()
48
    {
49
        $this->shouldHaveType('Scriptotek\Alma\Client');
50
    }
51
52
    public function it_only_accepts_valid_regions()
53
    {
54
        $this->shouldThrow('Scriptotek\Alma\Exception\ClientException')
55
            ->duringSetRegion('ux');
56
    }
57
58
    public function it_can_make_GET_requests()
59
    {
60
        $responseBody = str_random();
61
        $this->httpWithResponseBody($responseBody);
62
        $this->get(str_random())->shouldBe($responseBody);
63
    }
64
65
    public function it_sends_an_API_key_with_each_request()
66
    {
67
        $http = $this->httpWithResponseBody(str_random());
68
        $this->getJSON(str_random());
69
70
        // Query string should include apikey
71
        expect($http->getRequests()[0])->getHeaderLine('authorization')->toBe('apikey DummyApiKey');
72
    }
73
74
    public function it_can_request_and_parse_JSON()
75
    {
76
        $responseBody = json_encode(['some_key' => 'some_value']);
77
        $http = $this->httpWithResponseBody($responseBody);
78
79
        $this->getJSON(str_random())->some_key->shouldBe('some_value');
80
81
        $request = $http->getRequests()[0];
82
        expect($request->getHeader('Accept')[0])->toBe('application/json');
83
    }
84
85
    public function it_can_post_and_parse_JSON()
86
    {
87
        $http = $this->httpWithResponseBody(SpecHelper::getDummyData('create_loan_response.json', false));
88
89
        $this->postJSON(str_random())->loan_id->shouldBe('7329587120002204');
90
91
        $request = $http->getRequests()[0];
92
        expect($request->getHeader('Content-Type')[0])->toBe('application/json');
93
        expect($request->getHeader('Accept')[0])->toBe('application/json');
94
    }
95
96
    public function it_can_request_and_parse_XML()
97
    {
98
        $responseBody = "<?xml version=\"1.0\"?>\n<some_key>some_value</some_key>\n";
99
        $http = $this->httpWithResponseBody($responseBody);
100
101
        $xml = $this->getXML(str_random());
102
103
        // Request headers should include Accept: application/xml
104
        expect($http->getRequests()[0])->getHeader('Accept')[0]->toBe('application/xml');
105
106
        // Response should be of type QuiteSimpleXMLElement
107
        $xml->shouldHaveType('Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement');
108
109
        // and have the expected value
110
        $xml->asXML()->shouldBe($responseBody);
111
    }
112
113
    public function it_can_make_PUT_requests()
114
    {
115
        $responseBody = str_random();
116
        $http = $this->httpWithResponseBody($responseBody);
117
118
        $this->put(str_random(), str_random(), 'application/json')->shouldBe($responseBody);
119
120
        expect($http->getRequests())->toHaveCount(1);
121
        expect($http->getRequests()[0])->getMethod()->toBe('PUT');
122
    }
123
124
    public function it_can_get_redirect_locations()
125
    {
126
        $http = $this->let();
127
        $response = new Response();
128
        $response = $response->withHeader('Location', 'http://test.test');
129
        $http->addResponse($response);
130
131
        $this->getRedirectLocation('/')->shouldBe('http://test.test');
132
    }
133
134
    public function it_processes_json_error_responses()
135
    {
136
        $http = $this->let();
137
138
        $exception = SpecHelper::makeExceptionResponse(
139
            SpecHelper::getDummyData('error_response.json', false)
140
        );
141
        $http->addException($exception);
142
143
        $this->shouldThrow(new RequestFailed(
144
            'Mandatory field is missing: library',
145
            '401664'
146
        ))->during('getJSON', ['/items/123']);
147
148
        expect($http->getRequests())->toHaveCount(1);
149
    }
150
151 View Code Duplication
    public function it_processes_xml_error_responses()
152
    {
153
        $http = $this->let();
154
155
        $exception = SpecHelper::makeExceptionResponse(
156
            SpecHelper::getDummyData('error_response.xml', false),
157
            400,
158
            'application/xml;charset=utf-8'
159
        );
160
        $http->addException($exception);
161
162
        $this->shouldThrow(new RequestFailed(
163
            'Mandatory field is missing: library',
164
            '401664'
165
        ))->during('getXML', ['/items/123']);
166
167
        expect($http->getRequests())->toHaveCount(1);
168
    }
169
170
    public function it_can_throw_resource_not_found()
171
    {
172
        $http = $this->let();
173
174
        $exception = SpecHelper::makeExceptionResponse(
175
            SpecHelper::getDummyData('item_barcode_error_response.json', false)
176
        );
177
        $http->addException($exception);
178
179
        $this->shouldThrow(new ResourceNotFound('No items found for barcode 123.', '401689'))
180
            ->during('getJSON', ['/items/123']);
181
182
        expect($http->getRequests())->toHaveCount(1);
183
    }
184
185
    public function it_can_throw_resource_not_found_for_500_errors_too()
186
    {
187
        $http = $this->let();
188
189
        // For Analytics reports, Alma will return 500, not 4xx
190
        $exception = SpecHelper::makeExceptionResponse(
191
            SpecHelper::getDummyData('report_not_found_response.xml', false),
192
            500,
193
            'application/xml;charset=utf-8',
194
            ServerErrorException::class
195
        );
196
        $http->addException($exception);
197
198
        $this->shouldThrow(new ResourceNotFound('Path not found (/test/path)', 'INTERNAL_SERVER_ERROR'))
199
            ->during('getXML', ['/analytics/reports', ['path' => '/test/path']]);
200
201
        expect($http->getRequests())->toHaveCount(1);
202
    }
203
204 View Code Duplication
    public function it_can_throw_invalid_api_key()
205
    {
206
        $http = $this->let();
207
208
        $exception = SpecHelper::makeExceptionResponse(
209
            'Invalid API Key',
210
            400,
211
            'text/plain;charset=UTF-8'
212
        );
213
        $http->addException($exception);
214
215
        $this->shouldThrow(new InvalidApiKey('Invalid API Key', 0))
216
           ->during('getJSON', ['/items/123']);
217
218
        expect($http->getRequests())->toHaveCount(1);
219
    }
220
221
    public function it_will_retry_when_reaching_rate_limit()
222
    {
223
        $http = $this->let();
224
225
        $exception = SpecHelper::makeExceptionResponse(
226
            SpecHelper::getDummyData('per_second_threshold_error_response.json', false),
227
            400,
228
            'application/json;charset=utf-8'
229
        );
230
        $http->addException($exception);
231
232
        $this->getJSON('/items/123');
233
234
        expect($http->getRequests())->toHaveCount(2);
235
    }
236
}
237