-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathApiUtilsTest.php
More file actions
427 lines (391 loc) · 13.7 KB
/
ApiUtilsTest.php
File metadata and controls
427 lines (391 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
/**
* This file is part of the Cloudinary PHP package.
*
* (c) Cloudinary
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cloudinary\Test\Unit\Utils;
use Cloudinary\Api\ApiUtils;
use Cloudinary\Test\Unit\UnitTestCase;
use Cloudinary\Transformation\Format;
use Cloudinary\Transformation\Transformation;
use Cloudinary\Configuration\Configuration;
use Cloudinary\Utils;
/**
* Class ApiUtilsTest
*/
final class ApiUtilsTest extends UnitTestCase
{
public const API_SIGN_REQUEST_TEST_SECRET = 'hdcixPpR2iKERPwqvH6sHdK9cyac';
public const API_SIGN_REQUEST_CLOUD_NAME = 'dn6ot3ged';
public function tearDown(): void
{
parent::tearDown();
Configuration::instance()->init();
}
/**
* Data provider for the method `testSerializeArrayOfArrays()`.
*
* @return array[]
*/
public function dataProviderSerializeArrayOfArrays()
{
return [
[
'value' => '',
'result' => '',
],
[
'value' => null,
'result' => '',
],
[
'value' => 0,
'result' => '0',
],
[
'value' => [],
'result' => '',
],
[
'value' => [[], []],
'result' => '',
],
[
'value' => [['1', 0], [1, '0'], ['value']],
'result' => '1,0|1,0|value',
],
[
'value' => [[0]],
'result' => '0',
],
];
}
/**
* Data provider for the method `testSerializeHeaders()`.
*
* @return array[]
*/
public function dataProviderSerializeHeaders()
{
return [
[
'value' => ['Authorization' => 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'],
'result' => 'Authorization:Basic YWxhZGRpbjpvcGVuc2VzYW1l',
],
[
'value' => ['Authorization: Basic FvFxhZGRpbjpvcGVuc2VzYW1l'],
'result' => 'Authorization: Basic FvFxhZGRpbjpvcGVuc2VzYW1l',
],
[
'value' => ['X-Robots-Tag' => 'noindex'],
'result' => 'X-Robots-Tag:noindex',
],
[
'value' => ['X-Robots-Tag: noindex'],
'result' => 'X-Robots-Tag: noindex',
],
[
'value' => ['Link' => 1],
'result' => 'Link:1',
],
[
'value' => ['Link: 1'],
'result' => 'Link: 1',
],
];
}
/**
* Verifies that different headers are correctly serialized.
*
* @param $value
* @param $result
*
* @dataProvider dataProviderSerializeHeaders
*/
public function testSerializeHeaders($value, $result)
{
self::assertEquals(
$result,
ApiUtils::serializeHeaders($value)
);
}
/**
* Test serialization of an array with nested arrays.
*
* @param $value
* @param $result
*
* @dataProvider dataProviderSerializeArrayOfArrays
*/
public function testSerializeArrayOfArrays($value, $result)
{
self::assertEquals(
$result,
ApiUtils::serializeArrayOfArrays($value)
);
}
/**
* Verifies that an asset transformations is correctly serialized.
*/
public function testTransformationComplexExpressions()
{
$transformation1 = new Transformation();
$transformation1->scale(3204);
$transformation2 = new Transformation();
$transformation2->rotate(127);
$transformation2->format(Format::jpg());
self::assertEquals(
'c_scale,w_3204|a_127/f_jpg',
ApiUtils::serializeAssetTransformations([$transformation1, $transformation2])
);
}
/**
* Data provider for the method `testSerializeContext()`.
*
* @return array[]
*/
public function dataProviderSerializeContext()
{
return [
[
'value' => '',
'result' => '',
],
[
'value' => null,
'result' => '',
],
[
'value' => 0,
'result' => '0',
],
[
'value' => [],
'result' => '',
],
[
'value' => ['!@#$%^&?*-+=[]{}()' => '!@#$%^&?*-+=[]{}()'],
'result' => '!@#$%^&?*-+\=[]{}()=!@#$%^&?*-+\=[]{}()',
],
[
'value' => ['caption' => 'cap=caps', 'alt' => 'alternative|alt=a'],
'result' => 'caption=cap\=caps|alt=alternative\|alt\=a',
],
[
'value' => ['caption' => ['cap1', 'cap2'], 'alt' => ['a|"a"', 'b="b"']],
'result' => 'caption=["cap1","cap2"]|alt=["a\|\"a\"","b\=\"b\""]',
],
];
}
/**
* Verifies that context data is correctly serialized.
*
* @param $value
* @param $result
*
* @dataProvider dataProviderSerializeContext
*/
public function testSerializeContext($value, $result)
{
self::assertEquals(
$result,
ApiUtils::serializeContext($value)
);
}
/**
* Data provider for the method `testSignParameters()`.
*
* @return array[]
*/
public function dataProviderSignParameters()
{
return [
[
'value' => ['p1' => 'v1'],
'result' => '4cdcfc973f12ab6a9a6ba56595a9c2897bdb8f32',
],
[
'value' => ['p1' => 'v1,v2'],
'result' => '9e06ad20c8a98319b00503edc4053be120017905',
],
[
'value' => ['p1' => ['v1', 'v2']],
'result' => '9e06ad20c8a98319b00503edc4053be120017905',
],
[
'value' => ['p1' => 'v1=v2*|}{ & !@#$%^&*()_;/.,?><\\/|_+a'],
'result' => 'ced1e363d8db0a8d7ebcfb9e67fadbf5ee78a0f1',
],
];
}
/**
* Verifies that correct signature is produced based on parameters.
*
* @param $value
* @param $result
*
* @dataProvider dataProviderSignParameters
*/
public function testSignParameters($value, $result)
{
self::assertEquals(
$result,
ApiUtils::signParameters($value, self::API_SECRET)
);
}
/**
* Data provider for the method `testSignParametersWithExplicitSignatureAlgorithm()`.
*
* @return array[]
*/
public function dataProviderSignWithAlgorithmParameters()
{
return [
[
'value' => ['p1' => 'v1'],
'result' => '4cdcfc973f12ab6a9a6ba56595a9c2897bdb8f32',
'signatureAlgorithm' => 'SHA1',
],
[
'value' => ['p1' => 'v1,v2'],
'result' => '9e06ad20c8a98319b00503edc4053be120017905',
'signatureAlgorithm' => 'sha1',
],
[
'value' => ['p1' => ['v1', 'v2']],
'result' => '9e06ad20c8a98319b00503edc4053be120017905',
'signatureAlgorithm' => 'sha1',
],
[
'value' => ['p1' => 'v1=v2*|}{ & !@#$%^&*()_;/.,?><\\/|_+a'],
'result' => 'ced1e363d8db0a8d7ebcfb9e67fadbf5ee78a0f1',
'signatureAlgorithm' => 'sha1',
],
[
'value' => ['p1' => 'v1=v2*|}{ & !@#$%^&*()_;/.,?><\\/|_+a'],
'result' => '0c06416c30bfc727eb2cbc9f93245be70bd6567c788b5bd93a3772e8253312bf',
'signatureAlgorithm' => 'sha256',
],
];
}
/**
* Verifies that correct signature is produced based on parameters using an explicit algorithm.
*
* @param $value
* @param $result
* @param $signatureAlgorithm
*
* @dataProvider dataProviderSignWithAlgorithmParameters
*/
public function testSignParametersWithExplicitSignatureAlgorithm($value, $result, $signatureAlgorithm)
{
self::assertEquals(
$result,
ApiUtils::signParameters($value, self::API_SECRET, $signatureAlgorithm)
);
}
/**
* Verifies that correct sha256 and sha1 hashes are produced based on signature algorithm from global config.
*/
public function testApiSignRequestWithGlobalConfig()
{
$initialParams = [
'cloud_name' => self::API_SIGN_REQUEST_CLOUD_NAME,
'timestamp' => 1568810420,
'username' => '[email protected]'
];
$params = $initialParams;
Configuration::instance()->cloud->apiSecret = self::API_SIGN_REQUEST_TEST_SECRET;
Configuration::instance()->cloud->signatureAlgorithm = Utils::ALGO_SHA256;
ApiUtils::signRequest($params, Configuration::instance()->cloud);
$expected = '45ddaa4fa01f0c2826f32f669d2e4514faf275fe6df053f1a150e7beae58a3bd';
self::assertEquals($expected, $params['signature']);
$params = $initialParams;
Configuration::instance()->cloud->signatureAlgorithm = null;
ApiUtils::signRequest($params, Configuration::instance()->cloud);
$expectedSha1 = '14c00ba6d0dfdedbc86b316847d95b9e6cd46d94';
self::assertEquals($expectedSha1, $params['signature']);
}
/**
* Verifies that correct sha256 and sha1 hashes are produced based on explicitly passed signature algorithm.
*/
public function testApiSignRequestWithExplicitConfig()
{
$params = [
'cloud_name' => self::API_SIGN_REQUEST_CLOUD_NAME,
'timestamp' => 1568810420,
'username' => '[email protected]'
];
$config = new Configuration('cloudinary://key:' . self::API_SIGN_REQUEST_TEST_SECRET . '@test123');
$config->cloud->signatureAlgorithm = Utils::ALGO_SHA256;
ApiUtils::signRequest($params, $config->cloud);
$expected = '45ddaa4fa01f0c2826f32f669d2e4514faf275fe6df053f1a150e7beae58a3bd';
self::assertEquals($expected, $params['signature']);
}
/**
* Should prevent parameter smuggling via & characters in parameter values.
*/
public function testApiSignRequestPreventsParameterSmuggling()
{
// Test with notification_url containing & characters
$paramsWithAmpersand = [
'cloud_name' => self::API_SIGN_REQUEST_CLOUD_NAME,
'timestamp' => 1568810420,
'notification_url' => 'https://fake.com/callback?a=1&tags=hello,world'
];
$config = new Configuration('cloudinary://key:' . self::API_SIGN_REQUEST_TEST_SECRET . '@test123');
ApiUtils::signRequest($paramsWithAmpersand, $config->cloud);
$signatureWithAmpersand = $paramsWithAmpersand['signature'];
// Test that attempting to smuggle parameters by splitting the notification_url fails
$paramsSmugggled = [
'cloud_name' => self::API_SIGN_REQUEST_CLOUD_NAME,
'timestamp' => 1568810420,
'notification_url' => 'https://fake.com/callback?a=1',
'tags' => 'hello,world' // This would be smuggled if & encoding didn't work
];
ApiUtils::signRequest($paramsSmugggled, $config->cloud);
$signatureSmugggled = $paramsSmugggled['signature'];
// The signatures should be different, proving that parameter smuggling is prevented
self::assertNotEquals($signatureWithAmpersand, $signatureSmugggled,
'Signatures should be different to prevent parameter smuggling');
// Verify the expected signature for the properly encoded case
$expectedSignature = '4fdf465dd89451cc1ed8ec5b3e314e8a51695704';
self::assertEquals($expectedSignature, $signatureWithAmpersand);
// Verify the expected signature for the smuggled parameters case
$expectedSmuggledSignature = '7b4e3a539ff1fa6e6700c41b3a2ee77586a025f9';
self::assertEquals($expectedSmuggledSignature, $signatureSmugggled);
}
/**
* Should apply the configured signature version from CloudConfig.
*/
public function testConfiguredSignatureVersionIsApplied()
{
$params = [
'cloud_name' => self::API_SIGN_REQUEST_CLOUD_NAME,
'timestamp' => 1568810420,
'notification_url' => 'https://fake.com/callback?a=1&tags=hello,world'
];
$config = new Configuration('cloudinary://key:' . self::API_SIGN_REQUEST_TEST_SECRET . '@test123');
// Test with signature version 1 (legacy behavior - no URL encoding)
$config->cloud->signatureVersion = 1;
$paramsV1 = $params;
ApiUtils::signRequest($paramsV1, $config->cloud);
$signatureV1 = $paramsV1['signature'];
// Test with signature version 2 (current behavior - with URL encoding)
$config->cloud->signatureVersion = 2;
$paramsV2 = $params;
ApiUtils::signRequest($paramsV2, $config->cloud);
$signatureV2 = $paramsV2['signature'];
// Signatures should be different, proving the version setting is applied
self::assertNotEquals($signatureV1, $signatureV2,
'Signature versions should produce different results');
// Version 2 should match the expected encoded signature
$expectedV2Signature = '4fdf465dd89451cc1ed8ec5b3e314e8a51695704';
self::assertEquals($expectedV2Signature, $signatureV2,
'Version 2 should match expected encoded signature');
}
}