Skip to content

Commit 5f00452

Browse files
authored
Convert image URLs to secure URLs in Xbox integration (home-assistant#157945)
1 parent 06a44de commit 5f00452

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

homeassistant/components/xbox/browse_media.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
from homeassistant.components.media_player import BrowseMedia, MediaClass, MediaType
2121

22+
from .entity import to_https
23+
2224

2325
class MediaTypeDetails(NamedTuple):
2426
"""Details for media type."""
@@ -151,5 +153,5 @@ def _find_media_image(images: list[Image]) -> str | None:
151153
if match := next(
152154
(image for image in images if image.image_purpose == purpose), None
153155
):
154-
return f"https:{match.uri}" if match.uri.startswith("/") else match.uri
156+
return to_https(match.uri)
155157
return None

homeassistant/components/xbox/entity.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ def check_deprecated_entity(
151151
return False
152152

153153

154+
def to_https(image_url: str) -> str:
155+
"""Convert image URLs to secure URLs."""
156+
157+
url = URL(image_url)
158+
if url.host == "images-eds.xboxlive.com":
159+
url = url.with_host("images-eds-ssl.xboxlive.com")
160+
return str(url.with_scheme("https"))
161+
162+
154163
def profile_pic(person: Person, _: Title | None = None) -> str | None:
155164
"""Return the gamer pic."""
156165

@@ -160,9 +169,4 @@ def profile_pic(person: Person, _: Title | None = None) -> str | None:
160169
# to point to the correct image, with the correct domain and certificate.
161170
# We need to also remove the 'mode=Padding' query because with it,
162171
# it results in an error 400.
163-
url = URL(person.display_pic_raw)
164-
if url.host == "images-eds.xboxlive.com":
165-
url = url.with_host("images-eds-ssl.xboxlive.com").with_scheme("https")
166-
query = dict(url.query)
167-
query.pop("mode", None)
168-
return str(url.with_query(query))
172+
return str(URL(to_https(person.display_pic_raw)).without_query_params("mode"))

homeassistant/components/xbox/media_source.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .binary_sensor import profile_pic
2323
from .const import DOMAIN
2424
from .coordinator import XboxConfigEntry
25+
from .entity import to_https
2526

2627
_LOGGER = logging.getLogger(__name__)
2728

@@ -655,6 +656,6 @@ def game_thumbnail(images: list[Image]) -> str | None:
655656
(i for i in images if i.type == img_type),
656657
None,
657658
):
658-
return match.url
659+
return to_https(match.url)
659660

660661
return None

homeassistant/components/xbox/sensor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
XboxBaseEntity,
3535
XboxBaseEntityDescription,
3636
check_deprecated_entity,
37+
to_https,
3738
)
3839

3940
PARALLEL_UPDATES = 0
@@ -142,8 +143,8 @@ def title_logo(_: Person, title: Title | None) -> str | None:
142143
"""Get the game logo."""
143144

144145
return (
145-
next((i.url for i in title.images if i.type == "Tile"), None)
146-
or next((i.url for i in title.images if i.type == "Logo"), None)
146+
next((to_https(i.url) for i in title.images if i.type == "Tile"), None)
147+
or next((to_https(i.url) for i in title.images if i.type == "Logo"), None)
147148
if title and title.images
148149
else None
149150
)

tests/components/xbox/snapshots/test_media_source.ambr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@
577577
'media_class': <MediaClass.GAME: 'game'>,
578578
'media_content_id': 'media-source://xbox/271958441785640/1297287135',
579579
'media_content_type': <MediaClass.GAME: 'game'>,
580-
'thumbnail': 'http://store-images.s-microsoft.com/image/apps.64736.65457035095819016.56f55216-1bb9-40aa-8796-068cf3075fc1.6491fb2f-52e7-4129-bcbd-d23a67117ae0',
580+
'thumbnail': 'https://store-images.s-microsoft.com/image/apps.64736.65457035095819016.56f55216-1bb9-40aa-8796-068cf3075fc1.6491fb2f-52e7-4129-bcbd-d23a67117ae0',
581581
'title': 'Blue Dragon',
582582
}),
583583
dict({
@@ -588,7 +588,7 @@
588588
'media_class': <MediaClass.GAME: 'game'>,
589589
'media_content_id': 'media-source://xbox/271958441785640/1560034050',
590590
'media_content_type': <MediaClass.GAME: 'game'>,
591-
'thumbnail': 'http://store-images.s-microsoft.com/image/apps.46246.63309362003335928.4079e21b-b00f-4446-a680-6bf9c0eb0158.c976135a-831a-4cf6-a39b-f01c633567bc',
591+
'thumbnail': 'https://store-images.s-microsoft.com/image/apps.46246.63309362003335928.4079e21b-b00f-4446-a680-6bf9c0eb0158.c976135a-831a-4cf6-a39b-f01c633567bc',
592592
'title': "Assassin's Creed® Syndicate",
593593
}),
594594
]),

tests/components/xbox/snapshots/test_sensor.ambr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@
790790
'attributes': ReadOnlyDict({
791791
'achievements': '2 / 43',
792792
'developer': 'Mistwalker / Artoon',
793-
'entity_picture': 'http://store-images.s-microsoft.com/image/apps.35072.13670972585585116.70570f0d-17aa-4f97-b692-5412fa183673.25a97451-9369-4f6b-b66b-3427913235eb',
793+
'entity_picture': 'https://store-images.s-microsoft.com/image/apps.35072.13670972585585116.70570f0d-17aa-4f97-b692-5412fa183673.25a97451-9369-4f6b-b66b-3427913235eb',
794794
'friendly_name': 'GSR Ae Now playing',
795795
'gamerscore': '10 / 1000',
796796
'genres': 'Role Playing',

0 commit comments

Comments
 (0)