Skip to content

Commit 4a24a14

Browse files
committed
Get rid of etags
Etags are meant for transient usage in browsers and are not meant for any long term usage. Fixes: #321 Signed-off-by: Hritik Vijay <hritikxx8@gmail.com>
1 parent 90fd963 commit 4a24a14

2 files changed

Lines changed: 4 additions & 60 deletions

File tree

vulnerabilities/helpers.py

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from typing import List
2828
from typing import Optional
2929
from typing import Tuple
30+
from unittest.mock import MagicMock
3031

3132
import requests
3233
import saneyaml
@@ -67,32 +68,8 @@ def fetch_yaml(url):
6768
return saneyaml.load(response.content)
6869

6970

70-
# FIXME: this is NOT how etags work .
71-
# We should instead send the proper HTTP header
72-
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match
73-
# and integrate this finely in the processing as this typically needs to use
74-
# streaming=True requests, and proper handling of the HTTP return code
75-
# In all cases this ends up being a single request, not a HEADD followed
76-
# by another real request
77-
def create_etag(data_src, url, etag_key):
78-
"""
79-
Etags are like hashes of web responses. For a data source `data_src`,
80-
we maintain (url, etag) mappings in the DB. `create_etag` creates
81-
(`url`, etag) pair. If a (`url`, etag) already exists then the code
82-
skips processing the response further to avoid duplicate work.
83-
84-
`etag_key` is the name of header which contains the etag for the url.
85-
"""
86-
etag = requests.head(url).headers.get(etag_key)
87-
if not etag:
88-
return True
89-
90-
elif url in data_src.config.etags:
91-
if data_src.config.etags[url] == etag:
92-
return False
93-
94-
data_src.config.etags[url] = etag
95-
return True
71+
# FIXME: Remove this entirely after complete importer-improver migration
72+
create_etag = MagicMock()
9673

9774

9875
def contains_alpha(string):

vulnerabilities/tests/test_helpers.py

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,10 @@
2020
# VulnerableCode is a free software tool from nexB Inc. and others.
2121
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
2222

23-
import dataclasses
2423
from unittest import TestCase
2524
from unittest.mock import patch
2625
from unittest.mock import MagicMock
2726

28-
from vulnerabilities.data_source import DataSource
29-
from vulnerabilities.helpers import create_etag
30-
31-
32-
@dataclasses.dataclass
33-
class DummyDataSourceConfiguration:
34-
etags: dict
35-
36-
37-
class DummyDataSource(DataSource):
38-
CONFIG_CLASS = DummyDataSourceConfiguration
39-
4027

4128
class TestHelpers(TestCase):
42-
@classmethod
43-
def setUpClass(cls):
44-
data_source_cfg = {"etags": {}}
45-
cls.data_source = DummyDataSource(config=data_source_cfg)
46-
47-
def test_create_etag(self):
48-
assert self.data_source.config.etags == {}
49-
50-
mock_response = MagicMock()
51-
mock_response.headers = {"ETag": "0x1234"}
52-
53-
with patch("vulnerabilities.helpers.requests.head", return_value=mock_response):
54-
assert (
55-
create_etag(data_src=self.data_source, url="https://example.org", etag_key="ETag")
56-
is True
57-
)
58-
assert self.data_source.config.etags == {"https://example.org": "0x1234"}
59-
assert (
60-
create_etag(data_src=self.data_source, url="https://example.org", etag_key="ETag")
61-
is False
62-
)
29+
...

0 commit comments

Comments
 (0)