@fiduswriter/editor 0.1.9 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/collab/chat.js +1 -1
  2. package/dist/collab/chat.js.map +1 -1
  3. package/dist/collab/doc.js +8 -8
  4. package/dist/collab/doc.js.map +1 -1
  5. package/dist/dialogs/figure.js +1 -1
  6. package/dist/dialogs/figure.js.map +1 -1
  7. package/dist/document_template/exporter.js +1 -1
  8. package/dist/document_template/exporter.js.map +1 -1
  9. package/dist/document_template/index.js +2 -2
  10. package/dist/document_template/index.js.map +1 -1
  11. package/dist/e2ee/snapshot-manager.js +5 -5
  12. package/dist/e2ee/snapshot-manager.js.map +1 -1
  13. package/dist/exporter/native/copy.js +2 -2
  14. package/dist/exporter/native/copy.js.map +1 -1
  15. package/dist/footnotes/editor.js +1 -1
  16. package/dist/footnotes/editor.js.map +1 -1
  17. package/dist/images/edit_dialog/edit_dialog/index.js +1 -1
  18. package/dist/images/edit_dialog/edit_dialog/index.js.map +1 -1
  19. package/dist/index.js +7 -7
  20. package/dist/index.js.map +1 -1
  21. package/dist/menus/headerbar/model.js +4 -4
  22. package/dist/menus/headerbar/model.js.map +1 -1
  23. package/package.json +3 -3
  24. package/src/collab/chat.js +1 -1
  25. package/src/collab/doc.js +8 -8
  26. package/src/dialogs/figure.js +1 -1
  27. package/src/document_template/exporter.js +1 -1
  28. package/src/document_template/index.js +2 -2
  29. package/src/e2ee/snapshot-manager.js +5 -5
  30. package/src/exporter/native/copy.js +2 -2
  31. package/src/footnotes/editor.js +1 -1
  32. package/src/images/edit_dialog/edit_dialog/index.js +1 -1
  33. package/src/index.js +7 -7
  34. package/src/menus/headerbar/model.js +4 -4
  35. package/src/e2ee/encryptor.js +0 -228
  36. package/src/e2ee/key-manager.js +0 -202
  37. package/src/e2ee/passphrase-crypto.js +0 -606
  38. package/src/e2ee/passphrase-dialog.js +0 -655
  39. package/src/e2ee/passphrase-manager.js +0 -502
  40. package/src/e2ee/password-dialog.js +0 -652
@@ -1,5 +1,5 @@
1
1
  import {CheckableList, ContentMenu, Dialog, addAlert} from "fwtoolkit"
2
- import {E2EEEncryptor} from "../../../e2ee/encryptor.js"
2
+ import {E2EEEncryptor} from "fwtoolkit/e2ee/encryptor"
3
3
  import {imageEditModel} from "./model.js"
4
4
  import {imageEditTemplate} from "./templates.js"
5
5
  export class ImageEditDialog {
package/src/index.js CHANGED
@@ -20,18 +20,18 @@ import {EditorState, TextSelection} from "prosemirror-state"
20
20
  import {tableEditing} from "prosemirror-tables"
21
21
  import {EditorView} from "prosemirror-view"
22
22
  import {FeedbackTab} from "@fiduswriter/common"
23
- import {E2EEEncryptor} from "./e2ee/encryptor.js"
24
- import {E2EEKeyManager} from "./e2ee/key-manager.js"
23
+ import {E2EEEncryptor} from "fwtoolkit/e2ee/encryptor"
24
+ import {E2EEKeyManager} from "fwtoolkit/e2ee/key-manager"
25
25
  import {
26
26
  enterPassphraseDialog,
27
27
  setupPassphraseDialog,
28
28
  showRecoveryKeyDialog
29
- } from "./e2ee/passphrase-dialog.js"
30
- import {PassphraseManager} from "./e2ee/passphrase-manager.js"
29
+ } from "fwtoolkit/e2ee/passphrase-dialog"
30
+ import {PassphraseManager} from "fwtoolkit/e2ee/passphrase-manager"
31
31
  import {
32
32
  changePasswordDialog,
33
33
  createPasswordDialog
34
- } from "./e2ee/password-dialog.js"
34
+ } from "fwtoolkit/e2ee/password-dialog"
35
35
  import {E2EESnapshotManager} from "./e2ee/snapshot-manager.js"
36
36
 
37
37
  import {getSettings} from "@fiduswriter/document/schema/convert"
@@ -794,7 +794,7 @@ export class Editor {
794
794
  } else if (result.action === "recover") {
795
795
  // Recovery flow
796
796
  const {recoverWithKeyDialog} = await import(
797
- "./e2ee/passphrase-dialog.js"
797
+ "fwtoolkit/e2ee/passphrase-dialog"
798
798
  )
799
799
  const recoverResult = await new Promise(resolve => {
800
800
  recoverWithKeyDialog(resolve)
@@ -807,7 +807,7 @@ export class Editor {
807
807
  recoverResult.newPassphrase
808
808
  )
809
809
  const {showRecoveryKeyDialog} = await import(
810
- "./e2ee/passphrase-dialog.js"
810
+ "fwtoolkit/e2ee/passphrase-dialog"
811
811
  )
812
812
  await new Promise(resolve => {
813
813
  showRecoveryKeyDialog(newRecoveryKey, resolve)
@@ -10,9 +10,9 @@ import {DocumentAccessRightsDialog} from "../../documents/access_rights/index.js
10
10
  import {SaveCopy, SaveRevision} from "../../exporter/native/index.js"
11
11
  import {ExportFidusFile} from "../../exporter/native/file.js"
12
12
  import {LanguageDialog, RevisionDialog} from "../../dialogs/index.js"
13
- import {E2EEKeyManager} from "../../e2ee/key-manager.js"
14
- import {PassphraseManager} from "../../e2ee/passphrase-manager.js"
15
- import {changePasswordDialog} from "../../e2ee/password-dialog.js"
13
+ import {E2EEKeyManager} from "fwtoolkit/e2ee/key-manager"
14
+ import {PassphraseManager} from "fwtoolkit/e2ee/passphrase-manager"
15
+ import {changePasswordDialog} from "fwtoolkit/e2ee/password-dialog"
16
16
  import {
17
17
  KeyBindingsDialog,
18
18
  SearchReplaceDialog,
@@ -435,7 +435,7 @@ export const headerbarModel = () => ({
435
435
  editor.e2ee.encryptionIterations
436
436
  )
437
437
  const {E2EEEncryptor} = await import(
438
- "../../e2ee/encryptor.js"
438
+ "fwtoolkit/e2ee/encryptor"
439
439
  )
440
440
  const testValue = "test"
441
441
  const encryptedTest =
@@ -1,228 +0,0 @@
1
- /**
2
- * E2EE Encryptor - Handles encryption and decryption of document content
3
- * for end-to-end encrypted documents.
4
- *
5
- * Uses AES-GCM (256-bit) for all encryption operations. Each encryption
6
- * operation generates a random 12-byte IV (initialization vector) to
7
- * ensure that encrypting the same plaintext twice produces different
8
- * ciphertext.
9
- *
10
- * Encrypted data format (Appendix A of the E2EE plan):
11
- * [IV (12 bytes)][Ciphertext (variable length)][Auth Tag (16 bytes, implicit in AES-GCM)]
12
- *
13
- * When stored as a string (e.g., in JSON fields), the entire structure
14
- * is Base64-encoded.
15
- */
16
- export class E2EEEncryptor {
17
- /**
18
- * Encrypt a string with AES-GCM.
19
- *
20
- * @param {string} plaintext - The plaintext string to encrypt
21
- * @param {CryptoKey} key - An AES-GCM key (from E2EEKeyManager.deriveKey)
22
- * @returns {Promise<string>} Base64-encoded string (iv + ciphertext + auth tag)
23
- */
24
- static async encrypt(plaintext, key) {
25
- const iv = crypto.getRandomValues(new Uint8Array(12))
26
- const encoded = new TextEncoder().encode(plaintext)
27
- const ciphertext = await crypto.subtle.encrypt(
28
- {name: "AES-GCM", iv: iv},
29
- key,
30
- encoded
31
- )
32
- // Prepend IV to ciphertext for storage
33
- const combined = new Uint8Array(iv.length + ciphertext.byteLength)
34
- combined.set(iv, 0)
35
- combined.set(new Uint8Array(ciphertext), iv.length)
36
- return E2EEEncryptor._uint8ArrayToBase64(combined)
37
- }
38
-
39
- /**
40
- * Decrypt a Base64-encoded AES-GCM ciphertext.
41
- *
42
- * @param {string} ciphertextBase64 - Base64-encoded (iv + ciphertext + auth tag)
43
- * @param {CryptoKey} key - An AES-GCM key (from E2EEKeyManager.deriveKey)
44
- * @returns {Promise<string>} The decrypted plaintext string
45
- */
46
- static async decrypt(ciphertextBase64, key) {
47
- const combined = E2EEEncryptor._base64ToUint8Array(ciphertextBase64)
48
- const iv = combined.slice(0, 12)
49
- const ciphertext = combined.slice(12)
50
- const decrypted = await crypto.subtle.decrypt(
51
- {name: "AES-GCM", iv: iv},
52
- key,
53
- ciphertext
54
- )
55
- return new TextDecoder().decode(decrypted)
56
- }
57
-
58
- /**
59
- * Encrypt a JSON-serializable object.
60
- *
61
- * Serializes the object to JSON, then encrypts the JSON string.
62
- *
63
- * @param {Object} obj - A JSON-serializable object
64
- * @param {CryptoKey} key - An AES-GCM key
65
- * @returns {Promise<string>} Base64-encoded encrypted data
66
- */
67
- static encryptObject(obj, key) {
68
- return E2EEEncryptor.encrypt(JSON.stringify(obj), key)
69
- }
70
-
71
- /**
72
- * Decrypt to a JSON-serializable object.
73
- *
74
- * Decrypts the Base64-encoded ciphertext, then parses the result as JSON.
75
- *
76
- * @param {string} ciphertextBase64 - Base64-encoded encrypted data
77
- * @param {CryptoKey} key - An AES-GCM key
78
- * @returns {Promise<Object>} The decrypted and parsed object
79
- */
80
- static async decryptObject(ciphertextBase64, key) {
81
- const plaintext = await E2EEEncryptor.decrypt(ciphertextBase64, key)
82
- return JSON.parse(plaintext)
83
- }
84
-
85
- /**
86
- * Encrypt an ArrayBuffer (for images and other binary data).
87
- *
88
- * @param {ArrayBuffer} buffer - The binary data to encrypt
89
- * @param {CryptoKey} key - An AES-GCM key
90
- * @returns {Promise<string>} Base64-encoded encrypted data (iv + ciphertext)
91
- */
92
- static async encryptBuffer(buffer, key) {
93
- const iv = crypto.getRandomValues(new Uint8Array(12))
94
- const ciphertext = await crypto.subtle.encrypt(
95
- {name: "AES-GCM", iv: iv},
96
- key,
97
- buffer
98
- )
99
- // Prepend IV to ciphertext
100
- const combined = new Uint8Array(iv.length + ciphertext.byteLength)
101
- combined.set(iv, 0)
102
- combined.set(new Uint8Array(ciphertext), iv.length)
103
- return E2EEEncryptor._uint8ArrayToBase64(combined)
104
- }
105
-
106
- /**
107
- * Decrypt to an ArrayBuffer.
108
- *
109
- * @param {string} ciphertextBase64 - Base64-encoded encrypted data
110
- * @param {CryptoKey} key - An AES-GCM key
111
- * @returns {Promise<ArrayBuffer>} The decrypted binary data
112
- */
113
- static decryptBuffer(ciphertextBase64, key) {
114
- const combined = E2EEEncryptor._base64ToUint8Array(ciphertextBase64)
115
- const iv = combined.slice(0, 12)
116
- const ciphertext = combined.slice(12)
117
- return crypto.subtle.decrypt({name: "AES-GCM", iv: iv}, key, ciphertext)
118
- }
119
-
120
- /**
121
- * Encrypt an image File/Blob for upload.
122
- *
123
- * Reads the file as an ArrayBuffer, encrypts it, and returns
124
- * a Blob with application/octet-stream type (since the encrypted
125
- * data is opaque binary).
126
- *
127
- * @param {File|Blob} file - The image file to encrypt
128
- * @param {CryptoKey} key - An AES-GCM key
129
- * @returns {Promise<Blob>} An encrypted Blob with type application/octet-stream
130
- */
131
- static async encryptImage(file, key) {
132
- const buffer = await file.arrayBuffer()
133
- const iv = crypto.getRandomValues(new Uint8Array(12))
134
- const ciphertext = await crypto.subtle.encrypt(
135
- {name: "AES-GCM", iv: iv},
136
- key,
137
- buffer
138
- )
139
- // Prepend IV to ciphertext
140
- const combined = new Uint8Array(iv.length + ciphertext.byteLength)
141
- combined.set(iv, 0)
142
- combined.set(new Uint8Array(ciphertext), iv.length)
143
- return new Blob([combined], {type: "application/octet-stream"})
144
- }
145
-
146
- /**
147
- * Decrypt an encrypted image back to an ArrayBuffer.
148
- *
149
- * @param {string} ciphertextBase64 - Base64-encoded encrypted image data
150
- * @param {CryptoKey} key - An AES-GCM key
151
- * @returns {Promise<ArrayBuffer>} The decrypted image data
152
- */
153
- static decryptImage(ciphertextBase64, key) {
154
- return E2EEEncryptor.decryptBuffer(ciphertextBase64, key)
155
- }
156
-
157
- /**
158
- * Decrypt a Base64-encoded ciphertext and return as a Base64 string.
159
- *
160
- * Useful for decrypting encrypted images that need to be stored
161
- * as Base64 data URLs or re-exported.
162
- *
163
- * @param {string} ciphertextBase64 - Base64-encoded encrypted data
164
- * @param {CryptoKey} key - An AES-GCM key
165
- * @returns {Promise<string>} Base64-encoded decrypted data
166
- */
167
- static async decryptBufferToBase64(ciphertextBase64, key) {
168
- const buffer = await E2EEEncryptor.decryptBuffer(ciphertextBase64, key)
169
- const bytes = new Uint8Array(buffer)
170
- let binary = ""
171
- for (let i = 0; i < bytes.length; i++) {
172
- binary += String.fromCharCode(bytes[i])
173
- }
174
- return btoa(binary)
175
- }
176
-
177
- /**
178
- * Decrypt an encrypted image and return a blob URL for display.
179
- *
180
- * Fetches the encrypted image file from the given URL, decrypts it,
181
- * and creates a temporary object URL that can be used as an img src.
182
- *
183
- * @param {string} imageUrl - The URL of the encrypted image file
184
- * @param {CryptoKey} key - An AES-GCM key
185
- * @param {string} mimeType - The MIME type of the decrypted image
186
- * @returns {Promise<string>} A blob URL for the decrypted image
187
- */
188
- static async decryptImageToUrl(imageUrl, key, mimeType = "image/png") {
189
- const response = await fetch(imageUrl)
190
- const arrayBuffer = await response.arrayBuffer()
191
- const bytes = new Uint8Array(arrayBuffer)
192
- let binary = ""
193
- for (let i = 0; i < bytes.length; i++) {
194
- binary += String.fromCharCode(bytes[i])
195
- }
196
- const base64 = btoa(binary)
197
- const decrypted = await E2EEEncryptor.decryptBuffer(base64, key)
198
- const blob = new Blob([decrypted], {type: mimeType})
199
- return URL.createObjectURL(blob)
200
- }
201
-
202
- // --- Private helper methods ---
203
-
204
- /**
205
- * Convert a Uint8Array to a Base64-encoded string.
206
- * @private
207
- */
208
- static _uint8ArrayToBase64(bytes) {
209
- let binary = ""
210
- for (let i = 0; i < bytes.length; i++) {
211
- binary += String.fromCharCode(bytes[i])
212
- }
213
- return btoa(binary)
214
- }
215
-
216
- /**
217
- * Convert a Base64-encoded string to a Uint8Array.
218
- * @private
219
- */
220
- static _base64ToUint8Array(base64) {
221
- const binary = atob(base64)
222
- const bytes = new Uint8Array(binary.length)
223
- for (let i = 0; i < binary.length; i++) {
224
- bytes[i] = binary.charCodeAt(i)
225
- }
226
- return bytes
227
- }
228
- }
@@ -1,202 +0,0 @@
1
- /**
2
- * E2EE Key Manager - Handles key derivation and salt generation for
3
- * end-to-end encrypted documents.
4
- *
5
- * Uses PBKDF2 with SHA-256 to derive a 256-bit AES-GCM key from a
6
- * user-supplied password and a server-stored salt. The salt and
7
- * iteration count are stored on the server as part of the Document
8
- * model (Document.e2ee_salt and Document.e2ee_iterations),
9
- * so the user only needs the password to decrypt from any device.
10
- *
11
- * The key is marked as non-extractable for security — it cannot be
12
- * read back from the CryptoKey object once created.
13
- */
14
- export class E2EEKeyManager {
15
- /**
16
- * Derive an AES-GCM key from a password and salt using PBKDF2.
17
- *
18
- * @param {string} password - The user-supplied password
19
- * @param {Uint8Array} salt - The salt (16 bytes), fetched from the server
20
- * as part of the document data (get_doc_data or subscribe)
21
- * @param {number} [iterations=600000] - PBKDF2 iteration count
22
- * (OWASP 2023 recommendation for PBKDF2-SHA256)
23
- * @returns {Promise<CryptoKey>} A non-extractable AES-GCM 256-bit key
24
- */
25
- static async deriveKey(password, salt, iterations = 600000) {
26
- const encoder = new TextEncoder()
27
- const keyMaterial = await crypto.subtle.importKey(
28
- "raw",
29
- encoder.encode(password),
30
- "PBKDF2",
31
- false,
32
- ["deriveKey"]
33
- )
34
-
35
- const key = await crypto.subtle.deriveKey(
36
- {
37
- name: "PBKDF2",
38
- salt: salt,
39
- iterations: iterations,
40
- hash: "SHA-256"
41
- },
42
- keyMaterial,
43
- {name: "AES-GCM", length: 256},
44
- true, // extractable — required for sessionStorage caching
45
- ["encrypt", "decrypt"]
46
- )
47
-
48
- return key
49
- }
50
-
51
- /**
52
- * Store an AES-GCM key in sessionStorage for the current browser session.
53
- * The key is exported as raw bytes and Base64-encoded before storage.
54
- *
55
- * @param {number} documentId - The document ID
56
- * @param {CryptoKey} key - The AES-GCM key to store
57
- */
58
- static async storeKeyInSession(documentId, key) {
59
- const raw = await crypto.subtle.exportKey("raw", key)
60
- const base64 = btoa(String.fromCharCode(...new Uint8Array(raw)))
61
- sessionStorage.setItem(`e2ee_key_${documentId}`, base64)
62
- }
63
-
64
- /**
65
- * Retrieve an AES-GCM key from sessionStorage.
66
- *
67
- * @param {number} documentId - The document ID
68
- * @returns {Promise<CryptoKey|null>} The imported key, or null if not found
69
- */
70
- static getKeyFromSession(documentId) {
71
- const base64 = sessionStorage.getItem(`e2ee_key_${documentId}`)
72
- if (!base64) {
73
- return null
74
- }
75
- const binary = atob(base64)
76
- const raw = new Uint8Array(binary.length)
77
- for (let i = 0; i < binary.length; i++) {
78
- raw[i] = binary.charCodeAt(i)
79
- }
80
- return crypto.subtle.importKey(
81
- "raw",
82
- raw,
83
- {name: "AES-GCM", length: 256},
84
- true,
85
- ["encrypt", "decrypt"]
86
- )
87
- }
88
-
89
- /**
90
- * Remove a cached key from sessionStorage.
91
- *
92
- * @param {number} documentId - The document ID
93
- */
94
- static clearKeyFromSession(documentId) {
95
- sessionStorage.removeItem(`e2ee_key_${documentId}`)
96
- }
97
-
98
- /**
99
- * Clear all cached E2EE keys from sessionStorage.
100
- * Should be called on sign-out or session expiration.
101
- */
102
- static clearAllKeysFromSession() {
103
- for (let i = sessionStorage.length - 1; i >= 0; i--) {
104
- const key = sessionStorage.key(i)
105
- if (key && key.startsWith("e2ee_key_")) {
106
- sessionStorage.removeItem(key)
107
- }
108
- }
109
- }
110
-
111
- /**
112
- * Generate a new random salt (16 bytes).
113
- *
114
- * Used when creating a new E2EE document or changing the password.
115
- * The generated salt is sent to the server and stored in
116
- * Document.e2ee_salt. The salt is not a secret — its purpose
117
- * is to ensure that two documents with the same password produce
118
- * different derived keys (preventing rainbow table attacks).
119
- *
120
- * @returns {Uint8Array} A 16-byte random salt
121
- */
122
- static generateSalt() {
123
- return crypto.getRandomValues(new Uint8Array(16))
124
- }
125
-
126
- /**
127
- * Resolve a document password to an AES-GCM key.
128
- *
129
- * If the password is a valid base64/base64url-encoded 32-byte string
130
- * (43 or 44 characters), it is treated as a raw DEK and imported
131
- * directly without PBKDF2. Otherwise, the key is derived via PBKDF2.
132
- *
133
- * @param {string} password - The document password
134
- * @param {Uint8Array} salt - The salt (16 bytes)
135
- * @param {number} [iterations=600000] - PBKDF2 iteration count
136
- * @returns {Promise<CryptoKey>} The AES-GCM key
137
- */
138
- static resolvePasswordToKey(password, salt, iterations = 600000) {
139
- // Try to interpret as raw base64/base64url DEK first
140
- if (password.length === 44 || password.length === 43) {
141
- let decoded = null
142
- try {
143
- decoded = atob(password)
144
- } catch (_e) {
145
- // Try base64url with padding conversion
146
- try {
147
- let base64 = password.replace(/-/g, "+").replace(/_/g, "/")
148
- while (base64.length % 4) {
149
- base64 += "="
150
- }
151
- decoded = atob(base64)
152
- } catch (_e2) {
153
- // Not valid base64url either
154
- }
155
- }
156
- if (decoded && decoded.length === 32) {
157
- const raw = new Uint8Array(decoded.length)
158
- for (let i = 0; i < decoded.length; i++) {
159
- raw[i] = decoded.charCodeAt(i)
160
- }
161
- return crypto.subtle.importKey(
162
- "raw",
163
- raw,
164
- {name: "AES-GCM", length: 256},
165
- true,
166
- ["encrypt", "decrypt"]
167
- )
168
- }
169
- }
170
- // Fall back to PBKDF2 derivation
171
- return E2EEKeyManager.deriveKey(password, salt, iterations)
172
- }
173
-
174
- /**
175
- * Store the document password in sessionStorage.
176
- *
177
- * @param {number} documentId - The document ID
178
- * @param {string} password - The document password
179
- */
180
- static storePasswordInSession(documentId, password) {
181
- sessionStorage.setItem(`e2ee_password_${documentId}`, password)
182
- }
183
-
184
- /**
185
- * Retrieve the document password from sessionStorage.
186
- *
187
- * @param {number} documentId - The document ID
188
- * @returns {string|null} The password, or null if not found
189
- */
190
- static getPasswordFromSession(documentId) {
191
- return sessionStorage.getItem(`e2ee_password_${documentId}`)
192
- }
193
-
194
- /**
195
- * Remove a cached password from sessionStorage.
196
- *
197
- * @param {number} documentId - The document ID
198
- */
199
- static clearPasswordFromSession(documentId) {
200
- sessionStorage.removeItem(`e2ee_password_${documentId}`)
201
- }
202
- }