Skip to content

Commit f08b600

Browse files
authored
refactor out gotos to avoid raptor attack (#23)
1 parent 6f0718c commit f08b600

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

src/Contentful.php

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GuzzleHttp\Exception\RequestException;
77
use function GuzzleHttp\Promise\coroutine;
88
use GuzzleHttp\Promise\FulfilledPromise;
9+
use function GuzzleHttp\Promise\promise_for;
910
use GuzzleHttp\Promise\PromiseInterface;
1011
use Markup\Contentful\Cache\NullCacheItemPool;
1112
use Markup\Contentful\Decorator\AssetDecoratorInterface;
@@ -385,7 +386,6 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
385386
return $pool->getItem($cacheKey);
386387
};
387388
$assetDecorator = $this->ensureAssetDecorator($spaceData['asset_decorator']);
388-
$finalPromise = null;
389389
/**
390390
* Returns a built response if it passes test, or null if it doesn't.
391391
*
@@ -430,8 +430,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
430430

431431
$builtResponse = $buildResponseFromJson($cacheItemJson);
432432
if ($builtResponse) {
433-
$finalPromise = new FulfilledPromise($builtResponse);
434-
goto finalYield;
433+
yield promise_for($builtResponse);
434+
return;
435435
}
436436
}
437437
if ($this->cacheFailResponses) {
@@ -449,8 +449,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
449449
);
450450
$builtResponse = $buildResponseFromJson($fallbackJson);
451451
if ($builtResponse) {
452-
$finalPromise = new FulfilledPromise($builtResponse);
453-
goto finalYield;
452+
yield promise_for($builtResponse);
453+
return;
454454
}
455455
}
456456
}
@@ -499,22 +499,20 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
499499
$writeCacheItem->set($fallbackJson);
500500
$writeCache->save($writeCacheItem);
501501

502-
$finalPromise = new FulfilledPromise(
503-
$this->buildResponseFromRaw(
504-
json_decode($fallbackJson, true),
505-
$spaceData['name'],
506-
$assetDecorator,
507-
$shouldBuildTypedResources
508-
)
509-
);
510-
goto finalYield;
502+
yield promise_for($this->buildResponseFromRaw(
503+
json_decode($fallbackJson, true),
504+
$spaceData['name'],
505+
$assetDecorator,
506+
$shouldBuildTypedResources
507+
));
508+
return;
511509
}
512510
}
513511
//if there is a rate limit error, wait (if applicable)
514512
if ($e->hasResponse() && $e->getResponse()->getStatusCode() === 429 && $spaceData['retry_time_after_rate_limit_in_ms']) {
515513
usleep(intval($spaceData['retry_time_after_rate_limit_in_ms']));
516514

517-
$finalPromise = (yield $this->doRequest(
515+
yield $this->doRequest(
518516
$spaceData,
519517
$spaceName,
520518
$endpointUrl,
@@ -524,8 +522,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
524522
$cacheDisambiguator,
525523
$parameters,
526524
array_merge($options, ['async' => true])
527-
));
528-
goto finalYield;
525+
);
526+
return;
529527
}
530528
$unavailableException = new ResourceUnavailableException($e->getResponse(), $exceptionMessage, 0, $e);
531529
}
@@ -594,8 +592,8 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
594592
);
595593
$builtResponse = $buildResponseFromJson($fallbackJson);
596594
if ($builtResponse) {
597-
$finalPromise = new FulfilledPromise($builtResponse);
598-
goto finalYield;
595+
yield promise_for($builtResponse);
596+
return;
599597
}
600598
}
601599
}
@@ -613,9 +611,7 @@ function () use ($spaceData, $spaceName, $endpointUrl, $exceptionMessage, $api,
613611
LogInterface::TYPE_RESPONSE
614612
);
615613

616-
finalYield:
617-
618-
yield $finalPromise ?: new FulfilledPromise($builtResponse);
614+
yield promise_for($builtResponse);
619615
}
620616
);
621617

0 commit comments

Comments
 (0)