Skip to content

armgit / bbot

Public Imported from GitHub
0

The recursive internet scanner for hackers. 🧡

Respect `cache_hrs` in `download()` freshness check (#3563)

Paul Mueller8e177fa0711e
diff --git a/bbot/core/helpers/cache.py b/bbot/core/helpers/cache.py
index 3a70fbd24..3a4e5d73d 100644
--- a/bbot/core/helpers/cache.py
+++ b/bbot/core/helpers/cache.py
@@ -1,6 +1,6 @@
-import os
import time
import logging
+from pathlib import Path
from .misc import sha1
@@ -41,12 +41,13 @@ def cache_put(self, key, content):
f.write(content)
+def is_fresh(self, path, cache_hrs=24 * 7):
+ path = Path(path)
+ return path.is_file() and path.stat().st_mtime > time.time() - cache_hrs * 3600
+
+
def is_cached(self, key, cache_hrs=24 * 7):
- filename = self.cache_filename(key)
- if filename.is_file():
- (m, i, d, n, u, g, sz, atime, mtime, ctime) = os.stat(filename)
- return mtime > time.time() - cache_hrs * 3600
- return False
+ return self.is_fresh(self.cache_filename(key), cache_hrs)
def cache_filename(self, key):
diff --git a/bbot/core/helpers/helper.py b/bbot/core/helpers/helper.py
index dade8c723..ef3d78f98 100644
--- a/bbot/core/helpers/helper.py
+++ b/bbot/core/helpers/helper.py
@@ -78,7 +78,7 @@ class ConfigAwareHelper:
from . import regexes
from . import validators
from .files import tempfile, feed_pipe, _feed_pipe, tempfile_tail
- from .cache import cache_get, cache_put, cache_filename, is_cached
+ from .cache import cache_get, cache_put, cache_filename, is_cached, is_fresh
from .command import run, run_live, _spawn_proc, _prepare_command_kwargs
def __init__(self, preset):
diff --git a/bbot/core/helpers/web/web.py b/bbot/core/helpers/web/web.py
index 53a4e864c..4e338dbcf 100644
--- a/bbot/core/helpers/web/web.py
+++ b/bbot/core/helpers/web/web.py
@@ -464,8 +464,8 @@ class WebHelper:
max_size = self.parent_helper.human_to_bytes(max_size)
cache_hrs = float(kwargs.pop("cache_hrs", -1))
- if cache_hrs > 0 and self.parent_helper.is_cached(url):
- log.debug(f"{url} is cached at {self.parent_helper.cache_filename(url)}")
+ if cache_hrs > 0 and self.parent_helper.is_fresh(filename, cache_hrs):
+ log.debug(f"{url} is cached at {filename}")
success = True
else:
try:
diff --git a/bbot/test/test_step_1/test_web.py b/bbot/test/test_step_1/test_web.py
index 589c1fc97..e79252cb8 100644
--- a/bbot/test/test_step_1/test_web.py
+++ b/bbot/test/test_step_1/test_web.py
@@ -1,4 +1,5 @@
import re
+import time
from types import SimpleNamespace
from blasthttp import HTTPStatusError
@@ -215,6 +216,24 @@ async def test_web_helpers(bbot_scanner, bbot_httpserver, blasthttp_mock):
with open(filename2) as f:
assert f.read() == download_content
+ # download respects cache_hrs when checking freshness
+ cached_file = scan1.helpers.cache_filename(url)
+ cached_file.write_text("stale")
+ two_days_ago = time.time() - 48 * 3600
+ os.utime(cached_file, (two_days_ago, two_days_ago))
+ filename = await scan1.helpers.download(url, cache_hrs=72)
+ assert Path(filename).read_text() == "stale"
+ filename = await scan1.helpers.download(url, cache_hrs=24)
+ assert Path(filename).read_text() == download_content
+
+ # with a custom filename, freshness is checked on that file, not the url's cache entry
+ assert scan1.helpers.is_cached(url, cache_hrs=24)
+ custom_file = BBOT_TEST_DIR / "bbot_download_cache_hrs_test_file"
+ custom_file.unlink(missing_ok=True)
+ filename = await scan1.helpers.download(url, filename=custom_file, cache_hrs=24)
+ assert filename == custom_file
+ assert custom_file.read_text() == download_content
+
# beautifulsoup
download_content = """
<div>