Fixed webhook KeyError (Plex) and sanitize sensitive data in logs

This commit is contained in:
enoch85
2025-09-01 23:28:53 +02:00
committed by GitHub
parent f4957d4005
commit 9c46d1879e
2 changed files with 44 additions and 15 deletions

View File

@@ -503,7 +503,7 @@ class PlexLibraries(Resource):
logger.warning("No Plex server selected")
return {'data': []}
logger.info(f"Fetching Plex libraries from server: {server_url}")
logger.debug(f"Fetching Plex libraries from server: {sanitize_server_url(server_url)}")
headers = {
'X-Plex-Token': decrypted_token,
@@ -580,7 +580,7 @@ class PlexLibraries(Resource):
# The 'size' field contains the number of items in the library
actual_count = int(container.get('size', len(container.get('Metadata', []))))
logger.info(f"Library '{section.get('title')}' has {actual_count} items")
logger.debug(f"Library '{section.get('title')}' has {actual_count} items")
except Exception as e:
logger.warning(f"Failed to get count for library {section.get('title')}: {e}")

View File

@@ -11,6 +11,7 @@ from bs4 import BeautifulSoup as bso
from app.database import TableEpisodes, TableShows, TableMovies, database, select
from subtitles.mass_download import episode_download_subtitles, movies_download_subtitles
from app.logger import logger
from ..plex.security import sanitize_log_data
from ..utils import authenticate
@@ -27,22 +28,50 @@ class WebHooksPlex(Resource):
@authenticate
@api_ns_webhooks_plex.doc(parser=post_request_parser)
@api_ns_webhooks_plex.response(200, 'Success')
@api_ns_webhooks_plex.response(204, 'Unhandled event')
@api_ns_webhooks_plex.response(400, 'No GUID found')
@api_ns_webhooks_plex.response(204, 'Unhandled event or no processable data')
@api_ns_webhooks_plex.response(400, 'Bad request - missing required data')
@api_ns_webhooks_plex.response(401, 'Not Authenticated')
@api_ns_webhooks_plex.response(404, 'IMDB series/movie ID not found')
@api_ns_webhooks_plex.response(500, 'Internal server error')
def post(self):
"""Trigger subtitles search on play media event in Plex"""
args = self.post_request_parser.parse_args()
json_webhook = args.get('payload')
parsed_json_webhook = json.loads(json_webhook)
if 'Guid' not in parsed_json_webhook['Metadata']:
logger.debug('No GUID provided in Plex json payload. Probably a pre-roll video.')
return "No GUID found in JSON request body", 200
event = parsed_json_webhook['event']
if event not in ['media.play']:
return 'Unhandled event', 204
try:
args = self.post_request_parser.parse_args()
json_webhook = args.get('payload')
if not json_webhook:
logger.debug('PLEX WEBHOOK: No payload received')
return "No payload found in request", 400
parsed_json_webhook = json.loads(json_webhook)
# Check if this is a valid Plex webhook (should have 'event' field)
if 'event' not in parsed_json_webhook:
logger.debug('PLEX WEBHOOK: Invalid payload - missing "event" field')
return "Invalid webhook payload - missing event field", 400
event = parsed_json_webhook['event']
if event not in ['media.play']:
logger.debug('PLEX WEBHOOK: Ignoring unhandled event "%s"', event)
return 'Unhandled event', 204
# Check if Metadata key exists in the payload
if 'Metadata' not in parsed_json_webhook:
logger.debug('PLEX WEBHOOK: No Metadata in payload for event "%s"', event)
return "No Metadata found in JSON request body", 400
if 'Guid' not in parsed_json_webhook['Metadata']:
logger.debug('PLEX WEBHOOK: No GUID in Metadata for event "%s". Probably a pre-roll video.', event)
return "No GUID found in JSON request body", 204
except json.JSONDecodeError as e:
logger.debug('PLEX WEBHOOK: Failed to parse JSON. Error: %s. Payload: %s',
str(e), sanitize_log_data(json_webhook) if json_webhook else 'None')
return "Invalid JSON payload", 400
except Exception as e:
logger.error('PLEX WEBHOOK: Unexpected error: %s', str(e))
return "Unexpected error processing webhook", 500
media_type = parsed_json_webhook['Metadata']['type']
@@ -58,7 +87,7 @@ class WebHooksPlex(Resource):
if len(splitted_id) == 2:
ids.append({splitted_id[0]: splitted_id[1]})
if not ids:
return 'No GUID found', 400
return 'No GUID found', 204
if media_type == 'episode':
try: