mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-19 13:22:42 +01:00
Don't cache resources we can't generate properly
Summary: Fixes T10843. In a multi-server setup, we can do this: - Two servers, A and B. - You push an update. - A gets pushed first. - After A has been pushed, but before B has been pushed, a user loads a page from A. - It generates resource URIs like `/stuff/new/package.css`. - Those requests hit B. - B doesn't have the new resources yet. - It responds with old resources. - Your CDN caches things. You now have a poisoned CDN: old data is saved in a new URL. To try to avoid this with as little work as possible and generally make it hard to get wrong, check the URL hash against the hash we would generate. If they don't match, serve our best guess at the resource, but don't cache it. This should make things mostly keep working during the push, but prevent caches from becoming poisoned, and everyone should get a working version of everything after the push finishes. Test Plan: - `curl`'d a resource, got a cacheable one. - Changed the hash a little, `curl`'d again. This time: valid resource, but not cacheable. Reviewers: chad Reviewed By: chad Maniphest Tasks: T10843 Differential Revision: https://secure.phabricator.com/D15775
This commit is contained in:
parent
9656fe48bc
commit
43935d5916
3 changed files with 30 additions and 10 deletions
|
@ -208,6 +208,11 @@ final class CelerityResourceMap extends Phobject {
|
|||
}
|
||||
|
||||
|
||||
public function getHashForName($name) {
|
||||
return idx($this->nameMap, $name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the absolute URI for a resource, identified by hash.
|
||||
* This method is fairly low-level and ignores packaging.
|
||||
|
|
|
@ -31,7 +31,11 @@ final class CelerityPhabricatorResourceController
|
|||
return new Aphront400Response();
|
||||
}
|
||||
|
||||
return $this->serveResource($this->path);
|
||||
return $this->serveResource(
|
||||
array(
|
||||
'path' => $this->path,
|
||||
'hash' => $this->hash,
|
||||
));
|
||||
}
|
||||
|
||||
protected function buildResourceTransformer() {
|
||||
|
|
|
@ -24,7 +24,10 @@ abstract class CelerityResourceController extends PhabricatorController {
|
|||
|
||||
abstract public function getCelerityResourceMap();
|
||||
|
||||
protected function serveResource($path, $package_hash = null) {
|
||||
protected function serveResource(array $spec) {
|
||||
$path = $spec['path'];
|
||||
$hash = idx($spec, 'hash');
|
||||
|
||||
// Sanity checking to keep this from exposing anything sensitive, since it
|
||||
// ultimately boils down to disk reads.
|
||||
if (preg_match('@(//|\.\.)@', $path)) {
|
||||
|
@ -40,18 +43,24 @@ abstract class CelerityResourceController extends PhabricatorController {
|
|||
|
||||
$dev_mode = PhabricatorEnv::getEnvConfig('phabricator.developer-mode');
|
||||
|
||||
if (AphrontRequest::getHTTPHeader('If-Modified-Since') && !$dev_mode) {
|
||||
$map = $this->getCelerityResourceMap();
|
||||
$expect_hash = $map->getHashForName($path);
|
||||
|
||||
// Test if the URI hash is correct for our current resource map. If it
|
||||
// is not, refuse to cache this resource. This avoids poisoning caches
|
||||
// and CDNs if we're getting a request for a new resource to an old node
|
||||
// shortly after a push.
|
||||
$is_cacheable = ($hash === $expect_hash) &&
|
||||
$this->isCacheableResourceType($type);
|
||||
if (AphrontRequest::getHTTPHeader('If-Modified-Since') && $is_cacheable) {
|
||||
// Return a "304 Not Modified". We don't care about the value of this
|
||||
// field since we never change what resource is served by a given URI.
|
||||
return $this->makeResponseCacheable(new Aphront304Response());
|
||||
}
|
||||
|
||||
$is_cacheable = (!$dev_mode) &&
|
||||
$this->isCacheableResourceType($type);
|
||||
|
||||
$cache = null;
|
||||
$data = null;
|
||||
if ($is_cacheable) {
|
||||
if ($is_cacheable && !$dev_mode) {
|
||||
$cache = PhabricatorCaches::getImmutableCache();
|
||||
|
||||
$request_path = $this->getRequest()->getPath();
|
||||
|
@ -61,8 +70,6 @@ abstract class CelerityResourceController extends PhabricatorController {
|
|||
}
|
||||
|
||||
if ($data === null) {
|
||||
$map = $this->getCelerityResourceMap();
|
||||
|
||||
if ($map->isPackageResource($path)) {
|
||||
$resource_names = $map->getResourceNamesForPackageName($path);
|
||||
if (!$resource_names) {
|
||||
|
@ -117,7 +124,11 @@ abstract class CelerityResourceController extends PhabricatorController {
|
|||
$response->addAllowOrigin('*');
|
||||
}
|
||||
|
||||
return $this->makeResponseCacheable($response);
|
||||
if ($is_cacheable) {
|
||||
$response = $this->makeResponseCacheable($response);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public static function getSupportedResourceTypes() {
|
||||
|
|
Loading…
Reference in a new issue