Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
dedddb6
feat(ai): wire OpenAI provider to the new server-orchestrated agent loop
MisterPapaye Sep 9, 2026
bf33e72
feat(ai): Claude-like turn UI with a connected tool-step timeline
MisterPapaye Sep 9, 2026
4b8a385
feat(ai): clickable card components for every list-shaped tool result
MisterPapaye Sep 9, 2026
dd66432
fix(ai): restore tool steps when reloading a saved agent conversation
MisterPapaye Sep 9, 2026
166bbdb
feat(ai): scroll-to-bottom button and textarea autofocus in OrgAI
MisterPapaye Sep 9, 2026
4fcda2a
fix(ai): read_documentation glob path had one extra ../, always empty
MisterPapaye Sep 9, 2026
f89ffaa
feat(ai): wire the 'gateway' provider (self-hosted Synco AI Gateway)
MisterPapaye Sep 9, 2026
bf2db0c
feat(ai): model selectors in AISettings — live for gateway, curated f…
MisterPapaye Sep 9, 2026
132dac9
feat(ai): drop the Ollama URL field — the gateway knows on its own
MisterPapaye Sep 9, 2026
b122e1d
docs: plan de chiffrement des sessions IA (provider gateway)
Sep 10, 2026
c1e3077
fix(tasks): include unassigned personal tasks in "Mes Tâches" view
Sep 10, 2026
dd80740
feat(crypto): AI session key wrapping + gcm1 field encryption primitives
MisterPapaye Sep 10, 2026
a2e86a9
feat(ai): AiSessionKeyService — per-org AI session key fetch/bootstra…
MisterPapaye Sep 10, 2026
ebc0baa
feat(ai): E2EE for gateway AI sessions in AIService.ts
MisterPapaye Sep 10, 2026
cf1a1f3
feat(ai): set the encrypted title on the first message of a gateway s…
MisterPapaye Sep 10, 2026
5dea67b
feat(ai): provision the AI session key when saving with provider 'gat…
MisterPapaye Sep 10, 2026
9f6c1c6
fix(ai): reopened gateway chats showed raw gcm1: ciphertext instead o…
MisterPapaye Sep 10, 2026
23ad808
perf(ai): fetch Keycloak token refresh and AI session key in parallel
MisterPapaye Sep 10, 2026
22dd52f
Delete E2EE_PLAN.md
MisterPapaye Sep 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/assets/utils/crypto.aiSession.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { describe, expect, test } from "bun:test";
import { decryptAiField, encryptAiField } from "./crypto";

/**
* Vecteur de test partagé avec `Synco_AI_Gateway`/E2EE_PLAN.md et `synco_api`/E2EE_PLAN.md §2 —
* vérifie l'interopérabilité du wire format sans faire tourner les 2 autres implémentations.
*
* NB: la ligne `key (hex, 32 octets)` du document E2EE_PLAN.md est tronquée d'un nibble
* ("...4081d791" au lieu de "...4081d7911", 63 caractères hex au lieu de 64) — on part donc de la
* valeur `key (base64)` du même document, qui elle décode bien en 32 octets et produit le
* `wire_value` attendu.
*/
const KEY_BASE64 = "2C8mRtERMnD1j6Pa9k3n6fZCUxI6Dsf9uqJONAgdeRE=";
const NONCE_HEX = "1024c8c26840d416f50b5072";
const SESSION_ID = "sess_test0000000000000000000001";
const MESSAGE_ID = "msg_test00000000000000000000001";
const FIELD_NAME = "content";
const PLAINTEXT = "Bonjour, ceci est un message de test pour Synco AI.";
const EXPECTED_WIRE = "gcm1:ECTIwmhA1Bb1C1ByL8IECZj8SkBl2TYmk9665e/jnrCQNjYdR+CMirRU75qIq5pxnrallgkaWQtrQreT8eCSCf6JAzUrY5mTb/Ez42WxSw==";

function hexToBytes(hex: string): Uint8Array {
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < hex.length; i += 2) bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16);
return bytes;
}

async function importTestKey(): Promise<CryptoKey> {
const rawKey = Uint8Array.from(atob(KEY_BASE64), c => c.charCodeAt(0));
return crypto.subtle.importKey("raw", rawKey, { name: "AES-GCM" }, false, ["encrypt", "decrypt"]);
}

describe("AI session field wire format (gcm1)", () => {

test("decrypts the fixed wire_value from the plan into the expected plaintext", async () => {
const key = await importTestKey();
const decrypted = await decryptAiField(key, SESSION_ID, MESSAGE_ID, FIELD_NAME, EXPECTED_WIRE);
expect(decrypted).toBe(PLAINTEXT);
});

test("encrypting with the vector's nonce reproduces the exact fixed wire_value", async () => {
const key = await importTestKey();

// encryptAiField génère son propre nonce aléatoire ; on reproduit ici le format à la main
// avec le nonce fixe du vecteur pour vérifier l'égalité byte-à-byte avec `wire_value`.
const iv = hexToBytes(NONCE_HEX);
const aad = new TextEncoder().encode(`${SESSION_ID}:${MESSAGE_ID}:${FIELD_NAME}`);
const ciphertext = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv: iv as BufferSource, additionalData: aad as BufferSource, tagLength: 128 },
key,
new TextEncoder().encode(PLAINTEXT)
);
const combined = new Uint8Array(iv.length + ciphertext.byteLength);
combined.set(iv, 0);
combined.set(new Uint8Array(ciphertext), iv.length);
const wire = "gcm1:" + btoa(String.fromCharCode(...combined));

expect(wire).toBe(EXPECTED_WIRE);
});

test("round-trips arbitrary plaintext through encryptAiField/decryptAiField", async () => {
const key = await importTestKey();
const text = "Un message plus long, avec des caractères accentués éàç et un peu de JSON: {\"a\":1}";
const wire = await encryptAiField(key, SESSION_ID, MESSAGE_ID, "tool_result", text);

expect(wire.startsWith("gcm1:")).toBe(true);

const decrypted = await decryptAiField(key, SESSION_ID, MESSAGE_ID, "tool_result", wire);
expect(decrypted).toBe(text);
});

test("fails to decrypt when the AAD (field_name) doesn't match — binds ciphertext to its field", async () => {
const key = await importTestKey();
const wire = await encryptAiField(key, SESSION_ID, MESSAGE_ID, "content", "secret");

await expect(
decryptAiField(key, SESSION_ID, MESSAGE_ID, "title", wire)
).rejects.toThrow();
});

});
112 changes: 112 additions & 0 deletions src/assets/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,118 @@ export function lockSecurity()
}


// For AI session E2EE (provider 'gateway' — clé de session partagée par organisation)

export async function generateAiSessionKey(): Promise<CryptoKey>
{
return await crypto.subtle.generateKey(
{ name: "AES-GCM", length: 256 },
true,
["encrypt", "decrypt"]
);
}

export async function wrapAiSessionKeyForMember(
rawKeyBytes: ArrayBuffer,
recipientPublicKeyJWK: string | object
): Promise<string>
{

const pubKey = await crypto.subtle.importKey(
"jwk",
typeof recipientPublicKeyJWK === 'string' ? JSON.parse(recipientPublicKeyJWK) : recipientPublicKeyJWK,
{ name: "RSA-OAEP", hash: "SHA-256" },
false,
["encrypt"]
);

const encryptedBuffer = await crypto.subtle.encrypt(
{ name: "RSA-OAEP" },
pubKey,
rawKeyBytes
);

return btoa(String.fromCharCode(...new Uint8Array(encryptedBuffer)));

}

export async function unwrapAiSessionKey(
encryptedKeyBase64: string,
myPrivateKey: CryptoKey
): Promise<ArrayBuffer>
{

const encryptedKeyBuffer = Uint8Array.from(atob(encryptedKeyBase64), c => c.charCodeAt(0));

return await crypto.subtle.decrypt(
{ name: "RSA-OAEP" },
myPrivateKey,
encryptedKeyBuffer
);

}

/**
* Wire format partagé avec `Synco_AI_Gateway` et `synco_api` (cf. E2EE_PLAN.md §2) :
* "gcm1:" + base64standard(nonce(12) || ciphertext||tag(16)), AAD = "{sessionId}:{messageId}:{fieldName}".
*/
export async function encryptAiField(
key: CryptoKey,
sessionId: string,
messageId: string,
fieldName: string,
plaintext: string
): Promise<string>
{

const encoder = new TextEncoder();
const iv = crypto.getRandomValues(new Uint8Array(12));
const aad = encoder.encode(`${sessionId}:${messageId}:${fieldName}`);

const ciphertext = await crypto.subtle.encrypt(
{ name: "AES-GCM", iv, additionalData: aad, tagLength: 128 },
key,
encoder.encode(plaintext)
);

const combined = new Uint8Array(iv.length + ciphertext.byteLength);
combined.set(iv, 0);
combined.set(new Uint8Array(ciphertext), iv.length);

return "gcm1:" + btoa(String.fromCharCode(...combined));

}

export async function decryptAiField(
key: CryptoKey,
sessionId: string,
messageId: string,
fieldName: string,
wireValue: string
): Promise<string>
{

if (!wireValue.startsWith("gcm1:")) {
throw new Error("Format de champ IA chiffré inconnu.");
}

const encoder = new TextEncoder();
const raw = Uint8Array.from(atob(wireValue.slice(5)), c => c.charCodeAt(0));
const iv = raw.slice(0, 12);
const ciphertext = raw.slice(12);
const aad = encoder.encode(`${sessionId}:${messageId}:${fieldName}`);

const decrypted = await crypto.subtle.decrypt(
{ name: "AES-GCM", iv, additionalData: aad, tagLength: 128 },
key,
ciphertext
);

return new TextDecoder().decode(decrypted);

}


export const generateSalt = (): string => {
const array = new Uint8Array(16);

Expand Down
Loading
Loading