Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

browserImport.ts

163 lines6.5 KB

1/**
2 * Browser import - pulling cookies from a browser already installed on the
3 * machine into a T3 Code browser profile.
4 *
5 * Only cookies are imported. They carry the logged-in sessions, which is what
6 * makes an imported profile useful; saved passwords are deliberately out of
7 * scope because Electron exposes no password store to put them in.
8 *
9 * Availability is per source and per platform, and the reasons are modelled
10 * explicitly: some are a permission the user can grant, one is a limitation
11 * no amount of consent works around. The UI needs to tell those apart.
12 *
13 * @module BrowserImport
14 */
15import * as Schema from "effect/Schema";
16import { TrimmedNonEmptyString } from "./baseSchemas.ts";
17import { BrowserProfileId } from "./browserProfile.ts";
18​
19export const BROWSER_IMPORT_SOURCE_IDS = [
20 "chrome",
21 "edge",
22 "brave",
23 "vivaldi",
24 "opera",
25 "arc",
26 "helium",
27 "firefox",
28] as const;
29​
30export const BrowserImportSourceId = Schema.Literals(BROWSER_IMPORT_SOURCE_IDS);
31export type BrowserImportSourceId = typeof BrowserImportSourceId.Type;
32​
33/**
34 * Why a detected source cannot be imported right now.
35 *
36 * `needsKeychainApproval` and `browserRunning` are recoverable — the user
37 * grants access or quits the browser. `unsupportedPlatform` is not: it covers
38 * cases like Chrome on Windows, whose App-Bound Encryption is designed to stop
39 * exactly this, and which we will not work around.
40 */
41export const BrowserImportUnavailableReason = Schema.Literals([
42 "notInstalled",
43 "needsKeychainApproval",
44 "keychainItemMissing",
45 "browserRunning",
46 "unsupportedPlatform",
47]);
48export type BrowserImportUnavailableReason = typeof BrowserImportUnavailableReason.Type;
49​
50/**
51 * Why an import that was actually attempted failed.
52 *
53 * A superset of the unavailable reasons: a source can pass the pre-flight
54 * check and still fail, most often because the user declined the keychain
55 * prompt the read triggers.
56 */
57export const BrowserImportFailureReason = Schema.Literals([
58 ...BrowserImportUnavailableReason.literals,
59 /** The operating system's keyring or its bundled reader is unavailable. */
60 "keychainUnavailable",
61 /** No source registered under the requested id. */
62 "unknownSource",
63 /** The requested profile directory is not one the source reported. */
64 "unknownSourceProfile",
65 /** The target profile's Electron session could not be opened. */
66 "sessionUnavailable",
67 /**
68 * The cookies were written, but the new profile could not be saved to
69 * settings, so its partition was cleared again rather than left orphaned.
70 */
71 "profileNotSaved",
72 /**
73 * The cookies were written, but the profile count reached its cap while the
74 * import ran, so the new profile was not saved and its partition was cleared.
75 */
76 "profileLimitReached",
77 /** Anything else: a corrupt database, a failed decrypt, a vanished file. */
78 "readFailed",
79]);
80export type BrowserImportFailureReason = typeof BrowserImportFailureReason.Type;
81​
82/** A profile inside the source browser, e.g. Chromium's "Default" directory. */
83export const BrowserImportSourceProfile = Schema.Struct({
84 /** Directory name under the source's user-data dir. */
85 directory: TrimmedNonEmptyString,
86 /** The name the source browser shows for it. */
87 name: TrimmedNonEmptyString,
88 /**
89 * How many cookies the profile holds. Counted without decrypting, so it is
90 * cheap; absent when the store could not be read yet (Safari before Full
91 * Disk Access is granted).
92 */
93 cookieCount: Schema.optional(Schema.Int),
94});
95export type BrowserImportSourceProfile = typeof BrowserImportSourceProfile.Type;
96​
97export const BrowserImportSource = Schema.Struct({
98 id: BrowserImportSourceId,
99 name: TrimmedNonEmptyString,
100 profiles: Schema.Array(BrowserImportSourceProfile),
101 /** Absent when the source is importable. */
102 unavailable: Schema.optional(BrowserImportUnavailableReason),
103});
104export type BrowserImportSource = typeof BrowserImportSource.Type;
105​
106export const BrowserImportInput = Schema.Struct({
107 sourceId: BrowserImportSourceId,
108 sourceProfileDirectory: TrimmedNonEmptyString,
109 /** T3 Code profile the cookies are written into. */
110 targetProfileId: BrowserProfileId,
111});
112export type BrowserImportInput = typeof BrowserImportInput.Type;
113​
114/** IPC payload: the import input plus the environment the partition belongs to. */
115export const DesktopPreviewImportCookiesInputSchema = Schema.Struct({
116 environmentId: TrimmedNonEmptyString,
117 sourceId: BrowserImportSourceId,
118 sourceProfileDirectory: TrimmedNonEmptyString,
119 targetProfileId: BrowserProfileId,
120});
121​
122export const BrowserImportResult = Schema.Struct({
123 /** Cookies successfully written into the target partition. */
124 imported: Schema.Int,
125 /**
126 * Cookies read but not written — expired, rejected as malformed, or held
127 * under a key we could not use. Surfaced rather than hidden so a
128 * mostly-failed import doesn't look like a success.
129 */
130 skipped: Schema.Int,
131 /**
132 * The distinct hosts those skipped cookies belonged to, so the user can be
133 * told what didn't come over rather than just how many. Capped, since a
134 * broken key can skip thousands across many sites.
135 */
136 skippedDomains: Schema.Array(Schema.String),
137});
138export type BrowserImportResult = typeof BrowserImportResult.Type;
139​
140export const BROWSER_IMPORT_UNAVAILABLE_COPY: Readonly<
141 Record<BrowserImportUnavailableReason, string>
142> = {
143 notInstalled: "Not installed on this machine.",
144 needsKeychainApproval: "Needs Keychain access to read its cookies.",
145 keychainItemMissing:
146 "No encryption key in your Keychain — sign in to that browser once, then retry.",
147 browserRunning: "Quit the browser first so its cookie database can be read.",
148 unsupportedPlatform: "Importing from this browser isn't possible on this platform.",
149};
150​
151/** What to tell the user when an attempted import fails. */
152export const BROWSER_IMPORT_FAILURE_COPY: Readonly<Record<BrowserImportFailureReason, string>> = {
153 ...BROWSER_IMPORT_UNAVAILABLE_COPY,
154 keychainUnavailable:
155 "The system keyring could not be accessed. Make sure your desktop keyring is running and unlocked, then retry.",
156 unknownSource: "That browser is no longer available to import from.",
157 unknownSourceProfile: "That browser profile no longer exists.",
158 sessionUnavailable: "The target profile could not be opened.",
159 profileNotSaved: "The cookies were imported, but the new profile couldn't be saved. Try again.",
160 profileLimitReached:
161 "You've reached the profile limit. Delete a profile or import into an existing one.",
162 readFailed: "The browser's cookie database could not be read.",
163};