Skip to content

Commit 72c583d

Browse files
authored
Remove redundant SSL config and add timeout option (#604)
Add a 60 second timeout on all request methods Remove redundant SSL configuration for verifying peers (on by default now)
1 parent 132d9a5 commit 72c583d

5 files changed

Lines changed: 27 additions & 29 deletions

File tree

src/ReCaptcha/RequestMethod/CurlPost.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function submit(RequestParameters $params): string
8585
CURLOPT_HEADER => false,
8686
CURLOPT_RETURNTRANSFER => true,
8787
CURLOPT_SSL_VERIFYPEER => true,
88+
CURLOPT_TIMEOUT => 60,
8889
];
8990
curl_setopt_array($handle, $options);
9091

src/ReCaptcha/RequestMethod/Post.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ public function submit(RequestParameters $params): string
7575
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
7676
'method' => 'POST',
7777
'content' => $params->toQueryString(),
78-
// Force the peer to validate (not needed in 5.6.0+, but still works)
79-
'verify_peer' => true,
78+
'timeout' => 60,
8079
],
8180
];
8281
$context = stream_context_create($options);

src/ReCaptcha/RequestMethod/SocketPost.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public function submit(RequestParameters $params): string
8383
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
8484
}
8585

86+
if (false === stream_set_timeout($handle, 60)) {
87+
return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
88+
}
89+
8690
$content = $params->toQueryString();
8791

8892
$request = 'POST '.$urlParsed['path']." HTTP/1.0\r\n";

tests/ReCaptcha/RequestMethod/PostTest.php

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,6 @@ public function testHTTPContextOptions(): void
7373
$this->assertEquals(1, $this->runcount, 'The assertion was ran');
7474
}
7575

76-
public function testSSLContextOptions(): void
77-
{
78-
$req = new Post();
79-
self::$assert = [$this, 'sslContextOptionsCallback'];
80-
$req->submit($this->parameters);
81-
$this->assertEquals(1, $this->runcount, 'The assertion was ran');
82-
}
83-
8476
public function testOverrideVerifyUrl(): void
8577
{
8678
$req = new Post('https://over.ride/some/path');
@@ -138,26 +130,9 @@ public function httpContextOptionsCallback(array $args): void
138130
/** @var string $header */
139131
$header = $httpOptions['header'];
140132
$this->assertStringContainsStringIgnoringCase('Content-type: application/x-www-form-urlencoded', $header);
141-
}
142-
143-
/**
144-
* @param array<int, mixed> $args
145-
*/
146-
public function sslContextOptionsCallback(array $args): void
147-
{
148-
++$this->runcount;
149-
$this->assertCommonOptions($args);
150-
151-
/** @var resource $context */
152-
$context = $args[2];
153-
$options = stream_context_get_options($context);
154-
$this->assertArrayHasKey('http', $options);
155-
156-
/** @var array<string, mixed> $httpOptions */
157-
$httpOptions = $options['http'];
158133

159-
$this->assertArrayHasKey('verify_peer', $httpOptions);
160-
$this->assertTrue($httpOptions['verify_peer']);
134+
$this->assertArrayHasKey('timeout', $httpOptions);
135+
$this->assertEquals(60, $httpOptions['timeout']);
161136
}
162137

163138
/**

tests/ReCaptcha/RequestMethod/SocketPostTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class SocketPostGlobalState
5858
public static array $fgetsResponses = [];
5959
public static int $feofCount = 0;
6060
public static bool $fcloseCalled = false;
61+
public static bool $streamSetTimeoutSuccess = true;
6162
}
6263

6364
/**
@@ -100,6 +101,14 @@ function feof(\stdClass $handle): bool
100101
return empty(SocketPostGlobalState::$fgetsResponses);
101102
}
102103

104+
/**
105+
* Mock stream_set_timeout in the ReCaptcha\RequestMethod namespace.
106+
*/
107+
function stream_set_timeout(\stdClass $handle, int $seconds, int $microseconds = 0): bool
108+
{
109+
return SocketPostGlobalState::$streamSetTimeoutSuccess;
110+
}
111+
103112
/**
104113
* Mock fclose in the ReCaptcha\RequestMethod namespace.
105114
*/
@@ -126,6 +135,7 @@ protected function setUp(): void
126135
SocketPostGlobalState::$fwriteData = '';
127136
SocketPostGlobalState::$fgetsResponses = [];
128137
SocketPostGlobalState::$fcloseCalled = false;
138+
SocketPostGlobalState::$streamSetTimeoutSuccess = true;
129139
}
130140

131141
public function testSubmit(): void
@@ -147,6 +157,15 @@ public function testSubmit(): void
147157
$this->assertTrue(SocketPostGlobalState::$fcloseCalled);
148158
}
149159

160+
public function testStreamTimeoutFailureReturnsError(): void
161+
{
162+
SocketPostGlobalState::$streamSetTimeoutSuccess = false;
163+
$sp = new SocketPost();
164+
$response = $sp->submit(new RequestParameters('secret', 'response'));
165+
166+
$this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}', $response);
167+
}
168+
150169
public function testUrlFailureReturnsError(): void
151170
{
152171
$sp = new SocketPost('invalid_url');

0 commit comments

Comments
 (0)