Skip to content

Commit 26e530a

Browse files
committed
Enhance image source inference and add tests for hostname validation
- Introduced a helper function to check if a hostname matches a given domain, improving the clarity of the `infer_source_from_url` function. - Updated the image source inference logic to utilize the new helper function for better maintainability. - Added a test case to ensure that spoofed hostnames do not match valid image sources, enhancing security and robustness of the inference logic. - Updated Docker Compose configuration to ensure the correct image is used for the backend service.
1 parent 565bb1d commit 26e530a

7 files changed

Lines changed: 5504 additions & 5485 deletions

File tree

backend/server/adventures/services/images/metadata.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,19 @@ class ImageSource:
2828
IMMICH = ContentImage.Source.IMMICH
2929

3030

31+
def _hostname_matches_domain(hostname: str, domain: str) -> bool:
32+
return hostname == domain or hostname.endswith(f'.{domain}')
33+
34+
3135
def infer_source_from_url(url: str) -> str:
3236
hostname = (urlparse(url).hostname or '').lower()
33-
if 'googleapis.com' in hostname or 'googleusercontent.com' in hostname:
37+
if _hostname_matches_domain(hostname, 'googleapis.com') or _hostname_matches_domain(
38+
hostname, 'googleusercontent.com'
39+
):
3440
return ImageSource.GOOGLE
35-
if 'wikimedia.org' in hostname or 'wikipedia.org' in hostname:
41+
if _hostname_matches_domain(hostname, 'wikimedia.org') or _hostname_matches_domain(
42+
hostname, 'wikipedia.org'
43+
):
3644
return ImageSource.WIKIPEDIA
3745
return ImageSource.URL
3846

backend/server/adventures/tests/test_image_metadata.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ def test_generic_url(self):
3636
ImageSource.URL,
3737
)
3838

39+
def test_spoofed_hostnames_are_not_matched(self):
40+
spoofed_urls = [
41+
'https://evil.googleapis.com.attacker.com/photo.jpg',
42+
'https://notgoogleusercontent.com/photo.jpg',
43+
'https://evil.wikimedia.org.attacker.com/photo.jpg',
44+
'https://notwikipedia.org/photo.jpg',
45+
]
46+
for url in spoofed_urls:
47+
with self.subTest(url=url):
48+
self.assertEqual(infer_source_from_url(url), ImageSource.URL)
49+
3950

4051
class ImageMetadataResolutionTests(TestCase):
4152
def test_explicit_source_overrides_url_inference(self):

docker/docker-compose.advanced.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ services:
4949
# context: ..
5050
# dockerfile: docker/Dockerfile
5151
# target: backend
52-
# image: ghcr.io/seanmorley15/adventurelog-backend:latest
52+
image: ghcr.io/seanmorley15/adventurelog-backend:latest
5353
container_name: adventurelog-backend
5454
restart: unless-stopped
5555
env_file: ../.env.advanced

frontend/src/locales/de.json

Lines changed: 1387 additions & 1387 deletions
Large diffs are not rendered by default.

frontend/src/locales/es.json

Lines changed: 1387 additions & 1387 deletions
Large diffs are not rendered by default.

frontend/src/locales/sk.json

Lines changed: 1387 additions & 1387 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)