The recursive internet scanner for hackers. 🧡
Python asmattack-surface-managementautomationbugbountyclieasmhackingneo4josintosint-frameworkpentestingpythonreconrecursionscannersubdomain-enumerationsubdomain-scannersubdomainsthreat-intelligencethreatintel
Respect `cache_hrs` in `download()` freshness check (#3563)
Paul Mueller8e177fa0711e
diff --git a/bbot/core/helpers/cache.py b/bbot/core/helpers/cache.pyindex 3a70fbd24..3a4e5d73d 100644--- a/bbot/core/helpers/cache.py+++ b/bbot/core/helpers/cache.py@@ -1,6 +1,6 @@-import osimport timeimport logging+from pathlib import Pathfrom .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.pyindex dade8c723..ef3d78f98 100644--- a/bbot/core/helpers/helper.py+++ b/bbot/core/helpers/helper.py@@ -78,7 +78,7 @@ class ConfigAwareHelper:from . import regexesfrom . import validatorsfrom .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_freshfrom .command import run, run_live, _spawn_proc, _prepare_command_kwargsdef __init__(self, preset):diff --git a/bbot/core/helpers/web/web.py b/bbot/core/helpers/web/web.pyindex 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 = Trueelse:try:diff --git a/bbot/test/test_step_1/test_web.py b/bbot/test/test_step_1/test_web.pyindex 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 timefrom types import SimpleNamespacefrom 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+# beautifulsoupdownload_content = """<div>