Skip to content

Commit f256272

Browse files
erik-huang-devContributorWendong-Fan
authored
fix(cookie_manager): robustness — None-safe value, search_cookies domain, temp DB cleanup (#1098)
Co-authored-by: Contributor <contributor@example.com> Co-authored-by: Wendong-Fan <133094783+Wendong-Fan@users.noreply.github.com> Co-authored-by: Wendong-Fan <w3ndong.fan@gmail.com>
1 parent d619c99 commit f256272

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

backend/app/utils/cookie_manager.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import sqlite3
1616
import os
17-
from typing import List, Dict, Optional
17+
from typing import Any, List, Dict, Optional
1818
import logging
1919
import shutil
2020
from datetime import datetime
@@ -52,14 +52,25 @@ def _get_cookies_connection(self) -> Optional[sqlite3.Connection]:
5252
logger.warning(f"Cookies database not found: {self.cookies_db_path}")
5353
return None
5454

55+
temp_db_path = self.cookies_db_path + ".tmp"
56+
conn = None
5557
try:
56-
temp_db_path = self.cookies_db_path + ".tmp"
5758
shutil.copy2(self.cookies_db_path, temp_db_path)
5859
conn = sqlite3.connect(temp_db_path)
5960
conn.row_factory = sqlite3.Row
6061
return conn
6162
except Exception as e:
6263
logger.error(f"Error connecting to cookies database: {e}")
64+
if conn is not None:
65+
try:
66+
conn.close()
67+
except Exception:
68+
pass
69+
try:
70+
if os.path.exists(temp_db_path):
71+
os.remove(temp_db_path)
72+
except OSError:
73+
pass
6374
return None
6475

6576
def _cleanup_temp_db(self):
@@ -71,7 +82,7 @@ def _cleanup_temp_db(self):
7182
except Exception as e:
7283
logger.debug(f"Error cleaning up temp database: {e}")
7384

74-
def get_cookie_domains(self) -> List[Dict[str, any]]:
85+
def get_cookie_domains(self) -> List[Dict[str, Any]]:
7586
"""Get list of all domains with cookies"""
7687
conn = self._get_cookies_connection()
7788
if not conn:
@@ -146,10 +157,17 @@ def get_cookies_for_domain(self, domain: str) -> List[Dict[str, str]]:
146157

147158
cookies = []
148159
for row in rows:
160+
raw_value = row['value']
161+
if raw_value is None:
162+
value_str = ""
163+
elif len(raw_value) > 50:
164+
value_str = raw_value[:50] + "..."
165+
else:
166+
value_str = raw_value
149167
cookies.append({
150168
'domain': row['host_key'],
151169
'name': row['name'],
152-
'value': row['value'][:50] + '...' if len(row['value']) > 50 else row['value'],
170+
'value': value_str,
153171
'path': row['path'],
154172
'secure': bool(row['is_secure']),
155173
'httponly': bool(row['is_httponly'])
@@ -240,11 +258,11 @@ def delete_all_cookies(self) -> bool:
240258
logger.error(f"Error deleting all cookies: {e}")
241259
return False
242260

243-
def search_cookies(self, keyword: str) -> List[Dict[str, any]]:
261+
def search_cookies(self, keyword: str) -> List[Dict[str, Any]]:
244262
"""Search cookies by domain keyword"""
245263
domains = self.get_cookie_domains()
246264
keyword_lower = keyword.lower()
247265
return [
248266
domain for domain in domains
249-
if keyword_lower in domain['domain'].lower()
267+
if keyword_lower in (domain['domain'] or '').lower()
250268
]

0 commit comments

Comments
 (0)