| 147 | |
| 148 | |
| 149 | def _firefox_storage_state(cookies_db: Path, host_glob: str) -> dict: |
| 150 | # Copy to a temp file because Firefox holds a write lock on the live DB. |
| 151 | with tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) as tmp: |
| 152 | tmp_path = Path(tmp.name) |
| 153 | shutil.copy2(cookies_db, tmp_path) |
| 154 | try: |
| 155 | conn = sqlite3.connect(f"file:{tmp_path}?mode=ro", uri=True) |
| 156 | cur = conn.execute( |
| 157 | "SELECT name, value, host, path, expiry, isSecure, isHttpOnly, sameSite " |
| 158 | "FROM moz_cookies WHERE host LIKE ?", |
| 159 | (host_glob,), |
| 160 | ) |
| 161 | rows = cur.fetchall() |
| 162 | conn.close() |
| 163 | finally: |
| 164 | tmp_path.unlink(missing_ok=True) |
| 165 | samesite_map = {0: "None", 1: "Lax", 2: "Strict"} |
| 166 | cookies = [] |
| 167 | for name, value, host, path, expiry, is_secure, is_http_only, same_site in rows: |
| 168 | cookies.append({ |
| 169 | "name": name, |
| 170 | "value": value, |
| 171 | "domain": host if host.startswith(".") else "." + host, |
| 172 | "path": path or "/", |
| 173 | "expires": _normalize_expiry(expiry), |
| 174 | "httpOnly": bool(is_http_only), |
| 175 | "secure": bool(is_secure), |
| 176 | "sameSite": samesite_map.get(int(same_site or 0), "None"), |
| 177 | }) |
| 178 | return {"cookies": cookies, "origins": []} |
| 179 | |
| 180 | |
| 181 | def _normalize_expiry(raw: float | int | None) -> float: |