b00da43ab0
- PIN unlock: device-local 4-12 digit shortcut, DPAPI-wrapped vault
key. Three modes (state.unlockMode): pw / pin / pw+pin. PIN
derives a wrap key via PBKDF2(pin, salt, 100k) and unwraps the
stored vault key (mirrors the Quick Unlock blob shape).
Anti-brute-force: 5 wrong attempts wipes the blob. Setup gated by
master-pw reauth so an unattended unlocked laptop can't be
backdoored. Master pw rotation clears the PIN blob (key drift).
loadServerSettings post-sync demotes pin/both -> pw when the local
blob is missing, so a wiped device re-syncs the correct mode up.
New unit PM.PinUnlock.pas + cmd://pin/{store,get,clear,status}.
- Table column picker: ⚙ in topbar (table view only), checkbox menu
for Site/Username/Folder/Updated. Site also drives showSiteOnCards
so the existing "Show site / URL" toggle in Settings stays in
sync. NAME column auto-widths (180px min, content max, +32px
right padding) so column hugs the next one without truncating.
- Editor position chooser (Appearance setting): Slide-over right /
left / Centered modal. Scoped to #slideover + #settingsPanel so
the click-outside / pointer-events logic doesn't accidentally
trap the modal-style empty viewport.
- Confirm before discarding unsaved edits: state.confirmOnUnsaved
setting (default ON), prompts on X / Esc / click-outside / switch-
to-other-entry. Also gates Lock vault / Sign out actions when the
editor is dirty; auto-lock and system-lock paths bypass to avoid
blocking on an unattended machine.
- Open-in-browser button added to the actions cell of the table
view (was card-only).
- Entry templates pass folder customization + template id through
duplicate / export / import / auto-backup roundtrips.
- Folder color + icon now persisted across export/import: payload.
folders carries name/color/icon; import creates missing folders
additively (existing local customisation kept).
- Bulk move-to-folder, batch add-tag, single add-tag now re-ship
the full entry payload so partial PUTs don't silently wipe
TOTP / custom_fields / kind / template.
- FireDAC: switched ftString -> ftMemo for icon_b64 / custom_fields
/ TOTP / template params and replaced .AsString with .Value so a
large (~200 KB) DeepSeek favicon no longer gets truncated at the
default ANSI 4000-char cap.
- Unicode filenames: attachment INSERT now uses ftWideString +
.AsWideString so non-ANSI filenames round-trip instead of being
mangled to "?".
- HandleSetEntryIcon cap raised 256 KB -> 512 KB chars to accept
base64 data URIs produced by max-raw favicon fetches.
- promptDialog + askReauth support inline `error` line + retry-
with-count loops on doExport reauth and auto-backup password
setup (5 attempts cap before bailing).
- Recently used moved from Tools to Vault section in the sidebar.
- Auth screen passkey button hidden (Delphi backend stubs WebAuthn).
- Sensitive cmd://favicon/refresh-style buttons in Settings now
stopPropagation so the document-level "close panel" handler
doesn't dismiss Settings mid-async during DOM reparenting.
- TEST_PLAN.md: +PIN unlock section.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
447 lines
13 KiB
ObjectPascal
447 lines
13 KiB
ObjectPascal
unit PM.Handler.Attachments;
|
|
|
|
(*
|
|
Encrypted file attachments per entry.
|
|
|
|
GET /entries/{id}/attachments -> [{id, filename, mime, size_bytes, created_at}, ...]
|
|
POST /entries/{id}/attachments body {filename, mime, encrypted_blob, iv, size_bytes}
|
|
-> {id, filename, mime, size_bytes, created_at}
|
|
GET /attachments/{id} -> {id, filename, mime, size_bytes, encrypted_blob, iv}
|
|
DELETE /attachments/{id} -> {message}
|
|
|
|
encrypted_blob is the base64-encoded AES-GCM ciphertext of the raw file
|
|
bytes, produced by the JS client with the per-user vault key. Server
|
|
never sees plaintext.
|
|
|
|
Per-attachment cap: ~10 MB of ciphertext-as-base64 (≈ 7.5 MB raw file).
|
|
Heavier attachments aren't appropriate for SQLite TEXT storage anyway.
|
|
*)
|
|
|
|
interface
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.SysUtils, System.JSON,
|
|
Data.DB,
|
|
FireDAC.Comp.Client, FireDAC.Stan.Param,
|
|
IdCustomHTTPServer,
|
|
PM.Router, PM.JSON, PM.Database, PM.Session, PM.Audit;
|
|
|
|
const
|
|
MAX_ATTACHMENT_B64 = 10 * 1024 * 1024; // 10 MB of base64 text
|
|
|
|
function GetClientIP(ARequest: TIdHTTPRequestInfo): string;
|
|
begin
|
|
Result := ARequest.RemoteIP;
|
|
if Result = '' then Result := '127.0.0.1';
|
|
end;
|
|
|
|
// Ownership check: returns True iff the entry exists and belongs to LUserId.
|
|
function EntryBelongsToUser(LEntryId, LUserId: Integer): Boolean;
|
|
var
|
|
LQ: TFDQuery;
|
|
begin
|
|
LQ := TFDQuery.Create(nil);
|
|
try
|
|
LQ.Connection := DB.Connection;
|
|
LQ.SQL.Text :=
|
|
'SELECT 1 FROM vault_entries WHERE id = :id AND user_id = :uid';
|
|
LQ.ParamByName('id').AsInteger := LEntryId;
|
|
LQ.ParamByName('uid').AsInteger := LUserId;
|
|
LQ.Open;
|
|
Result := not LQ.Eof;
|
|
finally
|
|
LQ.Free;
|
|
end;
|
|
end;
|
|
|
|
// ===== GET /entries/{id}/attachments =========================================
|
|
|
|
procedure HandleListAttachments(ARequest: TIdHTTPRequestInfo;
|
|
AResponse: TIdHTTPResponseInfo; const AParams: TArray<string>);
|
|
var
|
|
LUserId, LEntryId: Integer;
|
|
LQ: TFDQuery;
|
|
LArr: TJSONArray;
|
|
LObj: TJSONObject;
|
|
begin
|
|
try
|
|
LUserId := Authenticate(ARequest, AResponse);
|
|
except
|
|
on ESessionRejected do Exit;
|
|
end;
|
|
|
|
LEntryId := StrToIntDef(AParams[0], 0);
|
|
if LEntryId = 0 then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 400, 'Invalid entry id');
|
|
Exit;
|
|
end;
|
|
|
|
LArr := TJSONArray.Create;
|
|
DB.Lock;
|
|
try
|
|
if not EntryBelongsToUser(LEntryId, LUserId) then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 404, 'Entry not found');
|
|
LArr.Free;
|
|
Exit;
|
|
end;
|
|
LQ := TFDQuery.Create(nil);
|
|
try
|
|
LQ.Connection := DB.Connection;
|
|
LQ.SQL.Text :=
|
|
'SELECT id, filename, mime, size_bytes, created_at ' +
|
|
'FROM entry_attachments WHERE entry_id = :eid AND user_id = :uid ' +
|
|
'ORDER BY created_at DESC';
|
|
LQ.ParamByName('eid').AsInteger := LEntryId;
|
|
LQ.ParamByName('uid').AsInteger := LUserId;
|
|
LQ.Open;
|
|
while not LQ.Eof do
|
|
begin
|
|
LObj := TJSONObject.Create;
|
|
LObj.AddPair('id', TJSONNumber.Create(LQ.FieldByName('id').AsInteger));
|
|
LObj.AddPair('filename', LQ.FieldByName('filename').AsString);
|
|
LObj.AddPair('mime', LQ.FieldByName('mime').AsString);
|
|
LObj.AddPair('size_bytes', TJSONNumber.Create(LQ.FieldByName('size_bytes').AsInteger));
|
|
LObj.AddPair('created_at',
|
|
FormatDateTime('yyyy-mm-dd"T"hh:nn:ss', LQ.FieldByName('created_at').AsDateTime));
|
|
LArr.Add(LObj);
|
|
LQ.Next;
|
|
end;
|
|
finally
|
|
LQ.Free;
|
|
end;
|
|
finally
|
|
DB.Unlock;
|
|
end;
|
|
TJSONHelper.SendJSON(AResponse, LArr);
|
|
end;
|
|
|
|
// ===== POST /entries/{id}/attachments ========================================
|
|
|
|
procedure HandleCreateAttachment(ARequest: TIdHTTPRequestInfo;
|
|
AResponse: TIdHTTPResponseInfo; const AParams: TArray<string>);
|
|
var
|
|
LUserId, LEntryId, LNewId: Integer;
|
|
LBody: TJSONObject;
|
|
LFilename, LMime, LBlob, LIv: string;
|
|
LSize: Integer;
|
|
LQ: TFDQuery;
|
|
LObj: TJSONObject;
|
|
begin
|
|
try
|
|
LUserId := Authenticate(ARequest, AResponse);
|
|
RequireCSRF(ARequest, AResponse, LUserId);
|
|
except
|
|
on ESessionRejected do Exit;
|
|
end;
|
|
|
|
LEntryId := StrToIntDef(AParams[0], 0);
|
|
if LEntryId = 0 then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 400, 'Invalid entry id');
|
|
Exit;
|
|
end;
|
|
|
|
LBody := TJSONHelper.ReadBody(ARequest);
|
|
try
|
|
LFilename := Trim(LBody.GetValue<string>('filename', ''));
|
|
LMime := LBody.GetValue<string>('mime', '');
|
|
LBlob := LBody.GetValue<string>('encrypted_blob', '');
|
|
LIv := LBody.GetValue<string>('iv', '');
|
|
LSize := LBody.GetValue<Integer>('size_bytes', 0);
|
|
finally
|
|
LBody.Free;
|
|
end;
|
|
|
|
if (LFilename = '') or (LBlob = '') or (LIv = '') then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 400, 'Missing required fields');
|
|
Exit;
|
|
end;
|
|
if Length(LBlob) > MAX_ATTACHMENT_B64 then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 413, 'Attachment too large (max ~7.5 MB raw)');
|
|
Exit;
|
|
end;
|
|
|
|
DB.Lock;
|
|
try
|
|
if not EntryBelongsToUser(LEntryId, LUserId) then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 404, 'Entry not found');
|
|
Exit;
|
|
end;
|
|
LQ := TFDQuery.Create(nil);
|
|
try
|
|
LQ.Connection := DB.Connection;
|
|
LQ.SQL.Text :=
|
|
'INSERT INTO entry_attachments ' +
|
|
'(user_id, entry_id, filename, mime, size_bytes, encrypted_blob, iv) ' +
|
|
'VALUES (:uid, :eid, :name, :mime, :sz, :blob, :iv)';
|
|
LQ.ParamByName('uid').AsInteger := LUserId;
|
|
LQ.ParamByName('eid').AsInteger := LEntryId;
|
|
// Force ftWideString / ftMemo so unicode filenames (Arabic,
|
|
// Chinese, emoji…) survive the round-trip. The default ftString
|
|
// inferred from .AsString maps to ANSI on SQLite and replaces
|
|
// anything outside the local codepage with '?'.
|
|
LQ.ParamByName('name').DataType := ftWideString;
|
|
LQ.ParamByName('name').AsWideString := LFilename;
|
|
LQ.ParamByName('mime').DataType := ftWideString;
|
|
LQ.ParamByName('mime').AsWideString := LMime;
|
|
LQ.ParamByName('sz').AsInteger := LSize;
|
|
LQ.ParamByName('blob').DataType := ftMemo;
|
|
LQ.ParamByName('blob').Value := LBlob;
|
|
LQ.ParamByName('iv').AsString := LIv;
|
|
LQ.ExecSQL;
|
|
LNewId := DB.Connection.GetLastAutoGenValue('entry_attachments');
|
|
finally
|
|
LQ.Free;
|
|
end;
|
|
finally
|
|
DB.Unlock;
|
|
end;
|
|
|
|
LogAudit(LUserId, 'add_attachment', GetClientIP(ARequest));
|
|
LObj := TJSONObject.Create;
|
|
LObj.AddPair('id', TJSONNumber.Create(LNewId));
|
|
LObj.AddPair('filename', LFilename);
|
|
LObj.AddPair('mime', LMime);
|
|
LObj.AddPair('size_bytes', TJSONNumber.Create(LSize));
|
|
LObj.AddPair('created_at', FormatDateTime('yyyy-mm-dd"T"hh:nn:ss', Now));
|
|
TJSONHelper.SendJSON(AResponse, LObj);
|
|
end;
|
|
|
|
// ===== GET /attachments/{id} =================================================
|
|
|
|
procedure HandleGetAttachment(ARequest: TIdHTTPRequestInfo;
|
|
AResponse: TIdHTTPResponseInfo; const AParams: TArray<string>);
|
|
var
|
|
LUserId, LId: Integer;
|
|
LQ: TFDQuery;
|
|
LObj: TJSONObject;
|
|
begin
|
|
try
|
|
LUserId := Authenticate(ARequest, AResponse);
|
|
except
|
|
on ESessionRejected do Exit;
|
|
end;
|
|
|
|
LId := StrToIntDef(AParams[0], 0);
|
|
if LId = 0 then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 400, 'Invalid id');
|
|
Exit;
|
|
end;
|
|
|
|
DB.Lock;
|
|
try
|
|
LQ := TFDQuery.Create(nil);
|
|
try
|
|
LQ.Connection := DB.Connection;
|
|
LQ.SQL.Text :=
|
|
'SELECT id, filename, mime, size_bytes, encrypted_blob, iv ' +
|
|
'FROM entry_attachments WHERE id = :id AND user_id = :uid';
|
|
LQ.ParamByName('id').AsInteger := LId;
|
|
LQ.ParamByName('uid').AsInteger := LUserId;
|
|
LQ.Open;
|
|
if LQ.Eof then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 404, 'Not found');
|
|
Exit;
|
|
end;
|
|
LObj := TJSONObject.Create;
|
|
LObj.AddPair('id', TJSONNumber.Create(LQ.FieldByName('id').AsInteger));
|
|
LObj.AddPair('filename', LQ.FieldByName('filename').AsString);
|
|
LObj.AddPair('mime', LQ.FieldByName('mime').AsString);
|
|
LObj.AddPair('size_bytes', TJSONNumber.Create(LQ.FieldByName('size_bytes').AsInteger));
|
|
LObj.AddPair('encrypted_blob', LQ.FieldByName('encrypted_blob').AsString);
|
|
LObj.AddPair('iv', LQ.FieldByName('iv').AsString);
|
|
TJSONHelper.SendJSON(AResponse, LObj);
|
|
finally
|
|
LQ.Free;
|
|
end;
|
|
finally
|
|
DB.Unlock;
|
|
end;
|
|
end;
|
|
|
|
// ===== GET /attachments/all ==================================================
|
|
// Lightweight listing of every attachment id+iv for the current user.
|
|
// Used by the master-pw rotation flow to enumerate what needs re-encryption.
|
|
// No blob shipped — fetched per-id only when the client is ready to re-encrypt.
|
|
|
|
procedure HandleListAllAttachments(ARequest: TIdHTTPRequestInfo;
|
|
AResponse: TIdHTTPResponseInfo; const AParams: TArray<string>);
|
|
var
|
|
LUserId: Integer;
|
|
LQ: TFDQuery;
|
|
LArr: TJSONArray;
|
|
LObj: TJSONObject;
|
|
begin
|
|
try
|
|
LUserId := Authenticate(ARequest, AResponse);
|
|
except
|
|
on ESessionRejected do Exit;
|
|
end;
|
|
|
|
LArr := TJSONArray.Create;
|
|
DB.Lock;
|
|
try
|
|
LQ := TFDQuery.Create(nil);
|
|
try
|
|
LQ.Connection := DB.Connection;
|
|
LQ.SQL.Text :=
|
|
'SELECT id, entry_id FROM entry_attachments ' +
|
|
'WHERE user_id = :uid';
|
|
LQ.ParamByName('uid').AsInteger := LUserId;
|
|
LQ.Open;
|
|
while not LQ.Eof do
|
|
begin
|
|
LObj := TJSONObject.Create;
|
|
LObj.AddPair('id', TJSONNumber.Create(LQ.FieldByName('id').AsInteger));
|
|
LObj.AddPair('entry_id', TJSONNumber.Create(LQ.FieldByName('entry_id').AsInteger));
|
|
LArr.Add(LObj);
|
|
LQ.Next;
|
|
end;
|
|
finally
|
|
LQ.Free;
|
|
end;
|
|
finally
|
|
DB.Unlock;
|
|
end;
|
|
TJSONHelper.SendJSON(AResponse, LArr);
|
|
end;
|
|
|
|
// ===== PUT /attachments/{id} =================================================
|
|
// Update only the ciphertext + iv. Used by master-pw rotation to swap to
|
|
// the new vault key. Filename/mime/size stay untouched.
|
|
|
|
procedure HandleUpdateAttachmentBlob(ARequest: TIdHTTPRequestInfo;
|
|
AResponse: TIdHTTPResponseInfo; const AParams: TArray<string>);
|
|
var
|
|
LUserId, LId: Integer;
|
|
LBody: TJSONObject;
|
|
LBlob, LIv: string;
|
|
LQ: TFDQuery;
|
|
begin
|
|
try
|
|
LUserId := Authenticate(ARequest, AResponse);
|
|
RequireCSRF(ARequest, AResponse, LUserId);
|
|
except
|
|
on ESessionRejected do Exit;
|
|
end;
|
|
|
|
LId := StrToIntDef(AParams[0], 0);
|
|
if LId = 0 then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 400, 'Invalid id');
|
|
Exit;
|
|
end;
|
|
|
|
LBody := TJSONHelper.ReadBody(ARequest);
|
|
try
|
|
LBlob := LBody.GetValue<string>('encrypted_blob', '');
|
|
LIv := LBody.GetValue<string>('iv', '');
|
|
finally
|
|
LBody.Free;
|
|
end;
|
|
|
|
if (LBlob = '') or (LIv = '') then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 400, 'Missing encrypted_blob / iv');
|
|
Exit;
|
|
end;
|
|
if Length(LBlob) > MAX_ATTACHMENT_B64 then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 413, 'Attachment too large');
|
|
Exit;
|
|
end;
|
|
|
|
DB.Lock;
|
|
try
|
|
LQ := TFDQuery.Create(nil);
|
|
try
|
|
LQ.Connection := DB.Connection;
|
|
LQ.SQL.Text :=
|
|
'UPDATE entry_attachments SET encrypted_blob = :blob, iv = :iv ' +
|
|
'WHERE id = :id AND user_id = :uid';
|
|
LQ.ParamByName('id').AsInteger := LId;
|
|
LQ.ParamByName('uid').AsInteger := LUserId;
|
|
LQ.ParamByName('blob').AsString := LBlob;
|
|
LQ.ParamByName('iv').AsString := LIv;
|
|
LQ.ExecSQL;
|
|
if LQ.RowsAffected = 0 then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 404, 'Not found');
|
|
Exit;
|
|
end;
|
|
finally
|
|
LQ.Free;
|
|
end;
|
|
finally
|
|
DB.Unlock;
|
|
end;
|
|
TJSONHelper.SendOK(AResponse, 'Updated');
|
|
end;
|
|
|
|
// ===== DELETE /attachments/{id} ==============================================
|
|
|
|
procedure HandleDeleteAttachment(ARequest: TIdHTTPRequestInfo;
|
|
AResponse: TIdHTTPResponseInfo; const AParams: TArray<string>);
|
|
var
|
|
LUserId, LId: Integer;
|
|
LQ: TFDQuery;
|
|
begin
|
|
try
|
|
LUserId := Authenticate(ARequest, AResponse);
|
|
RequireCSRF(ARequest, AResponse, LUserId);
|
|
except
|
|
on ESessionRejected do Exit;
|
|
end;
|
|
|
|
LId := StrToIntDef(AParams[0], 0);
|
|
if LId = 0 then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 400, 'Invalid id');
|
|
Exit;
|
|
end;
|
|
|
|
DB.Lock;
|
|
try
|
|
LQ := TFDQuery.Create(nil);
|
|
try
|
|
LQ.Connection := DB.Connection;
|
|
LQ.SQL.Text :=
|
|
'DELETE FROM entry_attachments WHERE id = :id AND user_id = :uid';
|
|
LQ.ParamByName('id').AsInteger := LId;
|
|
LQ.ParamByName('uid').AsInteger := LUserId;
|
|
LQ.ExecSQL;
|
|
if LQ.RowsAffected = 0 then
|
|
begin
|
|
TJSONHelper.SendError(AResponse, 404, 'Not found');
|
|
Exit;
|
|
end;
|
|
finally
|
|
LQ.Free;
|
|
end;
|
|
finally
|
|
DB.Unlock;
|
|
end;
|
|
|
|
LogAudit(LUserId, 'delete_attachment', GetClientIP(ARequest));
|
|
TJSONHelper.SendOK(AResponse, 'Deleted');
|
|
end;
|
|
|
|
initialization
|
|
Router.Register('GET', '/entries/(\d+)/attachments', HandleListAttachments);
|
|
Router.Register('POST', '/entries/(\d+)/attachments', HandleCreateAttachment);
|
|
Router.Register('GET', '/attachments/all', HandleListAllAttachments);
|
|
Router.Register('GET', '/attachments/(\d+)', HandleGetAttachment);
|
|
Router.Register('PUT', '/attachments/(\d+)', HandleUpdateAttachmentBlob);
|
|
Router.Register('DELETE', '/attachments/(\d+)', HandleDeleteAttachment);
|
|
|
|
end.
|