feat: MFA tools, single-instance, tray polish, prefs persistence
Session highlights:
- feat(prefs): DPAPI-backed key/value store (PM.UserPrefs) — fixes
rememberedUsername being lost across reboots due to the random
ephemeral HTTP port changing the localStorage origin every launch.
Bridge cmd://prefs/{get,set} round-trips through Delphi.
- feat(tray): icon visible from startup (NIM_ADD at constructor, not
at first minimize). Tray context menu themed via uxtheme!135
SetPreferredAppMode so it follows the app's dark/light setting.
- feat(single-instance): named mutex + RegisterWindowMessage broadcast.
Second launch posts WM_PMSHOW to HWND_BROADCAST and exits; the
running bridge restores the window from tray. Mutex lives in Local\
namespace so distinct Windows users can still each run one.
- feat(mfa): Authenticator sidebar view (live TOTP codes for every
entry with a secret) + standalone TOTP generator modal (paste
base32 / otpauth:// URI, or generate a random 20-byte secret).
- feat(sidebar): Folders / Tags / Tools sections collapsible with
chevron toggle. Badge counts stay visible when collapsed. State
persisted in settings_json (synced across devices).
- feat(autofill): hotkey when vault is locked now restores the app
and focuses the master password input instead of no-op'ing
silently. Cleaner UX for the common "I hit Ctrl+Shift+L but the
vault was locked" path.
- feat(quick-unlock): when enabled, skip lockVault on Windows lock /
sleep. Rationale: the DPAPI blob already gates access via the
Windows account, so re-locking on top of the OS lock is redundant.
Idle auto-lock still fires (separate opt-in).
- fix(quick-unlock): re-sync state.quickUnlockEnabled from DPAPI
source-of-truth at boot, instead of trusting (now-volatile)
localStorage.
- docs: CLAUDE.md updated with all new modules, bridge commands,
and the port-ephemeral pitfall.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
unit PM.HTTPServer;
|
||||
unit PM.HTTPServer;
|
||||
|
||||
{
|
||||
Indy TIdHTTPServer wrapper.
|
||||
@@ -12,8 +12,10 @@ interface
|
||||
|
||||
uses
|
||||
System.SysUtils, System.Classes, System.IOUtils,
|
||||
IdHTTPServer, IdContext, IdCustomHTTPServer, IdSocketHandle,
|
||||
PM.Router, PM.JSON, PM.Database, PM.StaticFiles, PM.EmbeddedAssets;
|
||||
Winapi.Windows,
|
||||
IdHTTPServer, IdContext, IdCustomHTTPServer, IdSocketHandle, IdTCPConnection,
|
||||
PM.Router, PM.JSON, PM.Database, PM.StaticFiles, PM.EmbeddedAssets,
|
||||
PM.Crypto, PM.ProcessLockdown;
|
||||
|
||||
type
|
||||
TLogProc = reference to procedure(const AMsg: string);
|
||||
@@ -22,6 +24,10 @@ type
|
||||
private
|
||||
FServer: TIdHTTPServer;
|
||||
FOnLog: TLogProc;
|
||||
FAccessToken: string;
|
||||
FRequireAccessToken: Boolean;
|
||||
FRequireProcessCheck: Boolean;
|
||||
FBoundPort: Integer;
|
||||
procedure HandleCommand(AContext: TIdContext;
|
||||
ARequest: TIdHTTPRequestInfo; AResponse: TIdHTTPResponseInfo);
|
||||
procedure HandleCommandOther(AContext: TIdContext;
|
||||
@@ -34,13 +40,23 @@ type
|
||||
AResponse: TIdHTTPResponseInfo);
|
||||
procedure Log(const AMsg: string);
|
||||
function GetActive: Boolean;
|
||||
function ValidateAccessToken(ARequest: TIdHTTPRequestInfo;
|
||||
AResponse: TIdHTTPResponseInfo): Boolean;
|
||||
function ValidateConnectingProcess(AContext: TIdContext;
|
||||
AResponse: TIdHTTPResponseInfo): Boolean;
|
||||
public
|
||||
constructor Create;
|
||||
destructor Destroy; override;
|
||||
procedure Start(APort: Integer);
|
||||
procedure Start(APort: Integer; SameFolder: Boolean;
|
||||
ARequireAccessToken: Boolean = True;
|
||||
ARequireProcessCheck: Boolean = True);
|
||||
procedure Stop;
|
||||
property Active: Boolean read GetActive;
|
||||
property OnLog: TLogProc read FOnLog write FOnLog;
|
||||
property AccessToken: string read FAccessToken;
|
||||
property RequireAccessToken: Boolean read FRequireAccessToken;
|
||||
property RequireProcessCheck: Boolean read FRequireProcessCheck;
|
||||
property BoundPort: Integer read FBoundPort;
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -96,16 +112,34 @@ begin
|
||||
if Assigned(FOnLog) then FOnLog(AMsg);
|
||||
end;
|
||||
|
||||
procedure TPMHTTPServer.Start(APort: Integer);
|
||||
procedure TPMHTTPServer.Start(APort: Integer; SameFolder: Boolean;
|
||||
ARequireAccessToken: Boolean; ARequireProcessCheck: Boolean);
|
||||
const
|
||||
EphemeralPortMin = 49152;
|
||||
EphemeralPortMax = 65535;
|
||||
var
|
||||
LBinding: TIdSocketHandle;
|
||||
LDBPath, LWebRoot: string;
|
||||
LDBPath, LWebRoot, LResolvedPort: string;
|
||||
LRequestedPort: Integer;
|
||||
begin
|
||||
if FServer.Active then Exit;
|
||||
|
||||
// Resolve vault.db AND the web root (parent of the exe = Z:\password-manager\)
|
||||
LDBPath := TPath.GetFullPath(TPath.Combine(ExtractFilePath(ParamStr(0)), '..\vault.db'));
|
||||
LWebRoot := TPath.GetFullPath(TPath.Combine(ExtractFilePath(ParamStr(0)), '..\'));
|
||||
FRequireAccessToken := ARequireAccessToken;
|
||||
FRequireProcessCheck := ARequireProcessCheck;
|
||||
if FRequireAccessToken then
|
||||
FAccessToken := PM.Crypto.RandomHex(32)
|
||||
else
|
||||
FAccessToken := '';
|
||||
|
||||
LRequestedPort := APort;
|
||||
if LRequestedPort = 0 then
|
||||
LRequestedPort := EphemeralPortMin + Random(EphemeralPortMax - EphemeralPortMin);
|
||||
|
||||
var pathParent := '..\';
|
||||
if SameFolder then
|
||||
pathParent := '';
|
||||
LDBPath := TPath.GetFullPath(TPath.Combine(ExtractFilePath(ParamStr(0)), pathParent+'vault.db'));
|
||||
LWebRoot := TPath.GetFullPath(TPath.Combine(ExtractFilePath(ParamStr(0)), pathParent));
|
||||
Log('Opening database: ' + LDBPath);
|
||||
InitDatabase(LDBPath);
|
||||
Log('Database ready.');
|
||||
@@ -115,10 +149,15 @@ begin
|
||||
FServer.Bindings.Clear;
|
||||
LBinding := FServer.Bindings.Add;
|
||||
LBinding.IP := '127.0.0.1';
|
||||
LBinding.Port := APort;
|
||||
LBinding.Port := LRequestedPort;
|
||||
|
||||
FServer.Active := True;
|
||||
Log('Server started on http://127.0.0.1:' + IntToStr(APort));
|
||||
FBoundPort := LRequestedPort;
|
||||
LResolvedPort := IntToStr(FBoundPort);
|
||||
Log(Format('Server started on http://127.0.0.1:%s (token:%s process_check:%s)',
|
||||
[LResolvedPort,
|
||||
BoolToStr(FRequireAccessToken, True),
|
||||
BoolToStr(FRequireProcessCheck, True)]));
|
||||
end;
|
||||
|
||||
procedure TPMHTTPServer.Stop;
|
||||
@@ -176,12 +215,70 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function TPMHTTPServer.ValidateConnectingProcess(AContext: TIdContext;
|
||||
AResponse: TIdHTTPResponseInfo): Boolean;
|
||||
var
|
||||
Binding: TIdSocketHandle;
|
||||
ConnectingPid: DWORD;
|
||||
begin
|
||||
if not FRequireProcessCheck then Exit(True);
|
||||
Result := False;
|
||||
|
||||
Binding := AContext.Binding;
|
||||
if Binding = nil then
|
||||
begin
|
||||
AResponse.ResponseNo := 404;
|
||||
AResponse.ContentText := '';
|
||||
Exit;
|
||||
end;
|
||||
|
||||
ConnectingPid := GetPidOfTcpConnection(Word(Binding.PeerPort), Word(Binding.Port));
|
||||
if (ConnectingPid <> 0) and IsDescendantOfCurrentProcess(ConnectingPid) then
|
||||
Exit(True);
|
||||
|
||||
Log(Format('Rejected request from foreign PID %d (%s %s)',
|
||||
[ConnectingPid, AContext.Connection.Socket.Binding.PeerIP, '']));
|
||||
AResponse.ResponseNo := 404;
|
||||
AResponse.ContentText := '';
|
||||
end;
|
||||
|
||||
function TPMHTTPServer.ValidateAccessToken(ARequest: TIdHTTPRequestInfo;
|
||||
AResponse: TIdHTTPResponseInfo): Boolean;
|
||||
const
|
||||
CookieName = 'pm_token';
|
||||
QueryParamName = 'pmt';
|
||||
SetCookieHeader = 'Set-Cookie';
|
||||
var
|
||||
CookieHeader, QueryToken: string;
|
||||
begin
|
||||
if not FRequireAccessToken then Exit(True);
|
||||
|
||||
CookieHeader := ARequest.RawHeaders.Values['Cookie'];
|
||||
if (CookieHeader <> '') and
|
||||
(Pos(CookieName + '=' + FAccessToken, CookieHeader) > 0) then
|
||||
Exit(True);
|
||||
|
||||
QueryToken := ARequest.Params.Values[QueryParamName];
|
||||
if QueryToken = FAccessToken then
|
||||
begin
|
||||
AResponse.CustomHeaders.AddValue(SetCookieHeader,
|
||||
CookieName + '=' + FAccessToken +
|
||||
'; Path=/; HttpOnly; SameSite=Strict');
|
||||
Exit(True);
|
||||
end;
|
||||
|
||||
AResponse.ResponseNo := 404;
|
||||
AResponse.ContentText := '';
|
||||
Result := False;
|
||||
end;
|
||||
|
||||
procedure TPMHTTPServer.HandleCommand(AContext: TIdContext;
|
||||
ARequest: TIdHTTPRequestInfo; AResponse: TIdHTTPResponseInfo);
|
||||
begin
|
||||
ApplySecurityHeaders(ARequest, AResponse);
|
||||
if not ValidateConnectingProcess(AContext, AResponse) then Exit;
|
||||
if not ValidateAccessToken(ARequest, AResponse) then Exit;
|
||||
try
|
||||
// Order: API route → embedded resource (production) → disk static (dev) → 404
|
||||
if Router.DispatchRequest(ARequest, AResponse) then Exit;
|
||||
if TryServeEmbedded(ARequest, AResponse) then Exit;
|
||||
if Assigned(StaticServer) and StaticServer.TryServe(ARequest, AResponse) then Exit;
|
||||
@@ -199,14 +296,14 @@ procedure TPMHTTPServer.HandleCommandOther(AContext: TIdContext;
|
||||
ARequest: TIdHTTPRequestInfo; AResponse: TIdHTTPResponseInfo);
|
||||
begin
|
||||
ApplySecurityHeaders(ARequest, AResponse);
|
||||
// OPTIONS preflight
|
||||
if SameText(ARequest.Command, 'OPTIONS') then
|
||||
begin
|
||||
AResponse.ResponseNo := 204;
|
||||
AResponse.ContentText := '';
|
||||
Exit;
|
||||
end;
|
||||
// Routes for PUT / DELETE go through here in Indy
|
||||
if not ValidateConnectingProcess(AContext, AResponse) then Exit;
|
||||
if not ValidateAccessToken(ARequest, AResponse) then Exit;
|
||||
try
|
||||
if not Router.DispatchRequest(ARequest, AResponse) then
|
||||
TJSONHelper.SendError(AResponse, 404, 'Not found');
|
||||
|
||||
Reference in New Issue
Block a user