Skip to content

Commit f7d0131

Browse files
committed
Only allow scalars and null as parameter values
Arrays, objects and resources can't be set via configuration files. This prevents gateway developers to require setting objects (like XML parsers) as parameters
1 parent e20e9f0 commit f7d0131

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/Omnipay/Common/AbstractGateway.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Guzzle\Http\Client as HttpClient;
1010
use Symfony\Component\HttpFoundation\ParameterBag;
1111
use Symfony\Component\HttpFoundation\Request as HttpRequest;
12+
use Omnipay\Common\Exception\RuntimeException;
1213

1314
/**
1415
* Base payment gateway class
@@ -138,6 +139,10 @@ public function getParameter($key)
138139
*/
139140
public function setParameter($key, $value)
140141
{
142+
if($value !== null && !is_scalar($value)) {
143+
throw new RuntimeException('Only scalar values are allowed for parameters');
144+
}
145+
141146
$this->parameters->set($key, $value);
142147

143148
return $this;

src/Omnipay/Common/Message/AbstractRequest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ protected function setParameter($key, $value)
170170
throw new RuntimeException('Request cannot be modified after it has been sent!');
171171
}
172172

173+
if($value !== null && !is_scalar($value)) {
174+
throw new RuntimeException('Only scalar values are allowed for parameters');
175+
}
176+
173177
$this->parameters->set($key, $value);
174178

175179
return $this;
@@ -243,7 +247,13 @@ public function setCard($value)
243247
$value = new CreditCard($value);
244248
}
245249

246-
return $this->setParameter('card', $value);
250+
if (null !== $this->response) {
251+
throw new RuntimeException('Request cannot be modified after it has been sent!');
252+
}
253+
254+
$this->parameters->set('card', $value);
255+
256+
return $this;
247257
}
248258

249259
/**
@@ -528,7 +538,13 @@ public function setItems($items)
528538
$items = new ItemBag($items);
529539
}
530540

531-
return $this->setParameter('items', $items);
541+
if (null !== $this->response) {
542+
throw new RuntimeException('Request cannot be modified after it has been sent!');
543+
}
544+
545+
$this->parameters->set('items', $items);
546+
547+
return $this;
532548
}
533549

534550
/**

tests/Omnipay/Common/Message/AbstractRequestTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ public function testSetCardWithArray()
7878
$this->assertSame('1234', $card->getNumber());
7979
}
8080

81+
public function testSetCardAfterRequestSent()
82+
{
83+
$this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
84+
$this->request->send();
85+
86+
$this->setExpectedException('\Omnipay\Common\Exception\RuntimeException');
87+
$this->request->setCard(array('number' => '1234'));
88+
}
89+
8190
public function testToken()
8291
{
8392
$this->assertSame($this->request, $this->request->setToken('12345'));
@@ -340,6 +349,18 @@ public function testItemsBag()
340349
$this->assertSame($itemBag, $this->request->getItems());
341350
}
342351

352+
public function testSetItemsBagAfterRequestSent()
353+
{
354+
$this->request = new AbstractRequestTest_MockAbstractRequest($this->getHttpClient(), $this->getHttpRequest());
355+
$this->request->send();
356+
357+
$itemBag = new ItemBag;
358+
$itemBag->add(array('name' => 'Floppy Disk'));
359+
360+
$this->setExpectedException('\Omnipay\Common\Exception\RuntimeException');
361+
$this->request->setItems($itemBag);
362+
}
363+
343364
public function testClientIp()
344365
{
345366
$this->assertSame($this->request, $this->request->setClientIp('127.0.0.1'));
@@ -397,6 +418,12 @@ public function testGetParameters()
397418
$this->assertEquals($expected, $this->request->getParameters());
398419
}
399420

421+
public function testSetParameterObject()
422+
{
423+
$this->setExpectedException('\Omnipay\Common\Exception\RuntimeException');
424+
$this->request->setParameter('test', new \stdClass());
425+
}
426+
400427
/**
401428
* @expectedException \Omnipay\Common\Exception\RuntimeException
402429
* @expectedExceptionMessage Request cannot be modified after it has been sent!

0 commit comments

Comments
 (0)