Skip to content

Commit 4cf0894

Browse files
committed
Force refetching source if our cached copy will expire
If we set the `expiresAfter` option in metarefresh's config, we effectively alter the inbound metadata. Thus when we're conditionally GETting, we need to be aware that we've introduced a validUntil attribute that the remote server may not know about. That means we might need to force a refresh of the source even if the remote server does not think it has been modified.
1 parent 1541e88 commit 4cf0894

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/MetaLoader.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace SimpleSAML\Module\metarefresh;
66

7+
use DateTimeImmutable;
78
use Exception;
89
use SimpleSAML\Configuration;
910
use SimpleSAML\Logger;
@@ -388,6 +389,23 @@ private function createContext(array $source): array
388389
if (array_key_exists($source['src'], $this->state)) {
389390
$sourceState = $this->state[$source['src']];
390391

392+
/*
393+
* If an expireAfter value exists, we effectively altered the metadata's validUntil ourselves
394+
* so we may need to refresh the metadata even if the source indicates it has not changed.
395+
*/
396+
if (isset($source['expireAfter']) && isset($sourceState['requested_at'])) {
397+
$requestedAt = (new DateTimeImmutable($sourceState['requested_at']))->getTimestamp();
398+
if ($requestedAt + $source['expireAfter'] <= time() - 3600) { // 1 hour based on default cron tags
399+
Logger::info(sprintf(
400+
'Cached metadata for %s expires soon - forcing refresh even if not changed',
401+
$source['src'],
402+
));
403+
unset($sourceState['last-modified']);
404+
unset($sourceState['etag']);
405+
$rawheader .= 'Cache-Control: max-age=0' . "\r\n";
406+
}
407+
}
408+
391409
if (isset($sourceState['last-modified'])) {
392410
$rawheader .= 'If-Modified-Since: ' . $sourceState['last-modified'] . "\r\n";
393411
}

src/MetaRefresh.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function runRefresh(string $crontag = null): void
111111
$source['whitelist'] = $whitelist;
112112
}
113113

114-
# Merge global and src specific attributewhitelists: cannot use array_unique for multi-dim.
114+
// Merge global and src specific attributewhitelists: cannot use array_unique for multi-dim.
115115
if (isset($source['attributewhitelist'])) {
116116
$source['attributewhitelist'] = array_merge($source['attributewhitelist'], $attributewhitelist);
117117
} else {
@@ -123,6 +123,11 @@ public function runRefresh(string $crontag = null): void
123123
$source['conditionalGET'] = $conditionalGET;
124124
}
125125

126+
// make our cache expiry available to the loader if we're conditionally GETting
127+
if ($source['conditionalGET'] && isset($expireAfter)) {
128+
$source['expireAfter'] = $expireAfter;
129+
}
130+
126131
Logger::debug('[metarefresh]: In set [' . $setkey . '] loading source [' . $source['src'] . ']');
127132
$metaloader->loadSource($source);
128133
}

0 commit comments

Comments
 (0)