|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off - Builds an encrypted Chromium cookie database fixture. |
| 2 | +import * as NodeCrypto from "node:crypto"; |
| 3 | +import * as NodeSqlite from "node:sqlite"; |
1 | 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
2 | 5 | import { assert, describe, it } from "@effect/vitest"; |
| 6 | +import { BrowserImportSource } from "@t3tools/contracts"; |
3 | 7 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 8 | +import type { Session } from "electron"; |
4 | 9 | import * as Effect from "effect/Effect"; |
5 | 10 | import * as FileSystem from "effect/FileSystem"; |
6 | 11 | import * as Layer from "effect/Layer"; |
| 12 | +import * as Schema from "effect/Schema"; |
7 | 13 |
|
8 | 14 | import * as BrowserSession from "../BrowserSession.ts"; |
9 | 15 | import * as BrowserImport from "./BrowserImport.ts"; |
| 16 | +import { LinuxBrowserSecretPath } from "./LinuxBrowserSecret.ts"; |
10 | 17 | import { BROWSER_IMPORT_SOURCES, sourcePathContext } from "./Sources.ts"; |
11 | 18 | import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; |
12 | 19 |
|
13 | 20 | const helium = BROWSER_IMPORT_SOURCES.find((source) => source.id === "helium")!; |
| 21 | +const decodeImportSource = Schema.decodeEffect(BrowserImportSource); |
14 | 22 |
|
15 | 23 | const cookie = { |
16 | 24 | url: "https://rejected.example/path", |
@@ -63,6 +71,114 @@ const withImporter = Effect.fnUntraced(function* () { |
63 | 71 | }); |
64 | 72 |
|
65 | 73 | describe("BrowserImport.importCookies", () => { |
| 74 | + it.effect.each(["Cookies", "Network/Cookies"])( |
| 75 | + "discovers and imports Linux Chromium cookies from %s", |
| 76 | + (cookiePath) => |
| 77 | + Effect.gen(function* () { |
| 78 | + const fileSystem = yield* FileSystem.FileSystem; |
| 79 | + const home = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-chromium-" }); |
| 80 | + const root = `${home}/.config/chromium`; |
| 81 | + yield* fileSystem.makeDirectory(`${root}/Default/Network`, { recursive: true }); |
| 82 | + yield* fileSystem.writeFileString( |
| 83 | + `${root}/Local State`, |
| 84 | + JSON.stringify({ profile: { info_cache: { Default: { name: "Personal" } } } }), |
| 85 | + ); |
| 86 | + yield* Effect.sync(() => { |
| 87 | + const key = NodeCrypto.pbkdf2Sync("peanuts", "saltysalt", 1, 16, "sha1"); |
| 88 | + const cipher = NodeCrypto.createCipheriv("aes-128-cbc", key, Buffer.alloc(16, 0x20)); |
| 89 | + const value = Buffer.concat([ |
| 90 | + Buffer.from("v10"), |
| 91 | + cipher.update("fixture-session"), |
| 92 | + cipher.final(), |
| 93 | + ]); |
| 94 | + using database = new NodeSqlite.DatabaseSync(`${root}/Default/${cookiePath}`); |
| 95 | + database.exec(` |
| 96 | + create table meta (key text primary key, value text not null); |
| 97 | + insert into meta values ('version', '23'); |
| 98 | + create table cookies ( |
| 99 | + host_key text, name text, value text, encrypted_value blob, path text, |
| 100 | + expires_utc integer, is_secure integer, is_httponly integer, samesite integer, |
| 101 | + top_frame_site_key text not null default '' |
| 102 | + ); |
| 103 | + `); |
| 104 | + database |
| 105 | + .prepare("insert into cookies values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") |
| 106 | + .run("chromium.example", "session", "", value, "/", 0, 1, 1, 1, ""); |
| 107 | + }); |
| 108 | + |
| 109 | + const written: Electron.CookiesSetDetails[] = []; |
| 110 | + let flushed = false; |
| 111 | + const session = { |
| 112 | + cookies: { |
| 113 | + set: (cookie: Electron.CookiesSetDetails) => { |
| 114 | + written.push(cookie); |
| 115 | + return Promise.resolve(); |
| 116 | + }, |
| 117 | + flushStore: () => { |
| 118 | + flushed = true; |
| 119 | + return Promise.resolve(); |
| 120 | + }, |
| 121 | + }, |
| 122 | + } as unknown as Session; |
| 123 | + const importer = yield* BrowserImport.BrowserImport.pipe( |
| 124 | + Effect.provide( |
| 125 | + BrowserImport.layer.pipe( |
| 126 | + Layer.provide( |
| 127 | + Layer.succeed(BrowserSession.BrowserSession, { |
| 128 | + getPartition: () => Effect.die("getPartition must not be reached"), |
| 129 | + isPartition: () => false, |
| 130 | + getSession: () => Effect.succeed(session), |
| 131 | + clearCookies: () => Effect.die("clearCookies must not be reached"), |
| 132 | + clearCache: () => Effect.die("clearCache must not be reached"), |
| 133 | + }), |
| 134 | + ), |
| 135 | + Layer.provide(Layer.succeed(HostProcess.Environment, { HOME: home })), |
| 136 | + Layer.provide(Layer.succeed(HostProcess.Platform, "linux")), |
| 137 | + Layer.provide(Layer.succeed(HostProcess.ExecutablePath, "/fixture/t3code")), |
| 138 | + Layer.provide(NodeServices.layer), |
| 139 | + ), |
| 140 | + ), |
| 141 | + ); |
| 142 | + const source = (yield* importer.listSources).find( |
| 143 | + (candidate) => candidate.name === "Chromium", |
| 144 | + ); |
| 145 | + assert.isDefined(source); |
| 146 | + assert.deepEqual(yield* decodeImportSource(source!), { |
| 147 | + id: "chromium", |
| 148 | + name: "Chromium", |
| 149 | + profiles: [{ directory: "Default", name: "Personal", cookieCount: 1 }], |
| 150 | + }); |
| 151 | + assert.deepEqual( |
| 152 | + yield* importer.importCookies({ |
| 153 | + input: { |
| 154 | + sourceId: "chromium", |
| 155 | + sourceProfileDirectory: "Default", |
| 156 | + targetProfileId: "default", |
| 157 | + }, |
| 158 | + scope: "persist:t3code-preview-test", |
| 159 | + persistent: true, |
| 160 | + }), |
| 161 | + { imported: 1, skipped: 0, skippedDomains: [] }, |
| 162 | + ); |
| 163 | + assert.deepEqual(written, [ |
| 164 | + { |
| 165 | + url: "https://chromium.example/", |
| 166 | + name: "session", |
| 167 | + value: "fixture-session", |
| 168 | + path: "/", |
| 169 | + secure: true, |
| 170 | + httpOnly: true, |
| 171 | + sameSite: "lax", |
| 172 | + }, |
| 173 | + ]); |
| 174 | + assert.isTrue(flushed); |
| 175 | + }).pipe( |
| 176 | + Effect.provideService(LinuxBrowserSecretPath, undefined), |
| 177 | + Effect.provide(NodeServices.layer), |
| 178 | + Effect.scoped, |
| 179 | + ), |
| 180 | + ); |
| 181 | + |
66 | 182 | it.effect("rejects a source profile the browser never reported", () => |
67 | 183 | Effect.gen(function* () { |
68 | 184 | const fileSystem = yield* FileSystem.FileSystem; |
|
0 commit comments