wallet: Fix detection of symlinks on Windows

bitcoin/bitcoin: PR #34603

1 minor finding · 3 suggestions

Earlier concerns neither republished nor resolved: 1 minor finding. See the Run archive for their assessments.

Findings

Link

[minor] · ListDatabases lists Windows file symlinks that GetWalletPath rejects

On GCC-built Windows binaries, a top-level BDB file symlink remains advertised for migration although GetWalletPath now rejects it; no current in-tree caller or fixture was found that creates this input.

Location: src/wallet/db.cpp:34 (head).

Details and suggested fix

For a top-level file symlink reported as regular, disable_recursion_pending() does not prevent the standalone-file branch from adding it as a BDB wallet, provided its name is neither wallet.dat nor a .bak file. The RPC attaches migration guidance to that entry, and the GUI offers a migration action. Migration then calls GetWalletPath, whose new !IsSymlink(wallet_path) condition returns Invalid -wallet path for the same name. Base accepted that name at validation. Cache the IsSymlink result and exclude links from the depth-zero regular-file branch. Keep the directory branch eligible so supported directory-link wallets remain discoverable, and add a Windows listing test using a link to a BDB fixture.


Optional improvements

Link

[suggestion] · GetWalletPath broadens file rejection from symlinks to all Windows reparse points

GetWalletPath now excludes non-link Windows reparse-point files from legacy compatibility, although the stated rationale addresses directory traversal; practical incidence remains unconfirmed because visibility of the affected non-link tags has not been established.

Location: src/wallet/walletutil.cpp:70 (head).

Details and suggested fix

The Windows implementation of IsSymlink tests only FILE_ATTRIBUTE_REPARSE_POINT, without examining the tag. Applying it to the regular-file exception therefore rejects any standalone legacy BDB file carrying that attribute, including non-links. Both migration and bitcoin-wallet dump return the path-validation error before opening the database. Ordinary loading is not newly affected because BDB loading already requires an explicit BERKELEY_RO request. Make this file-acceptance policy explicit: if rejecting every reparse-point file is intentional, document the rationale and replace the comment describing these files as symlinks. If only links should be rejected, use a tag-aware predicate for validation while retaining the broad traversal check.


Link

[suggestion] · Routine wallet-directory scans warn about supported symlink layouts

ListDatabases now generates warning-level messages for supported directory-symlink wallets during routine RPC and GUI scans, although non-recursion is their intended handling rather than a problem requiring administrator action.

Location: src/wallet/db.cpp:35 (head).

Details and suggested fix

GetWalletPath explicitly accepts directory symlinks, and the test exercises this layout with w7_symlink. Nevertheless, each listwalletdir call or opening of the GUI open or migrate wallet menu reaches LogWarning for such entries, subject to normal log rate limiting. On non-Windows systems, the iterator already avoids following directory symlinks. The logging guidance reserves warnings for problems the administrator should address. Use LogDebug(BCLog::WALLETDB, ...) or remove this message while retaining disable_recursion_pending().


Link

[suggestion] · IsSymlink discards the Windows error and unnecessarily converts the exception path

When GetFileAttributesW fails, IsSymlink replaces the native error with invalid_argument and rebuilds the exception path through a locale-dependent narrow-string constructor, obscuring diagnosis.

Location: src/util/fs_helpers.cpp:357 (head).

Details and suggested fix

ListDatabases catches this exception and logs e.what(), but the fixed error code cannot distinguish access failures from an entry disappearing during a scan. Passing fs::PathToString(path) also converts the Windows path to UTF-8 and then implicitly back through std::filesystem::path's narrow constructor, which can misdecode non-ASCII names. The separately logged path is unaffected. Capture GetLastError() immediately after the failure and throw fs::filesystem_error("Unable to get file attributes", path, std::error_code(static_cast<int>(err), std::system_category())) instead.


Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Ralph's inferences

The existing compatibility boundary treats a wallet directory, rather than an independently relocated data file, as the normal storage unit. A standalone data-file name directly within walletdir is a deliberately narrow legacy exception.

The base validator explicitly allows directory symlinks but excludes standalone file symlinks. Its diagnostic describes a directory holding both wallet.dat and database logs, while BDBDataFile describes the legacy file case as sharing its parent directory with log files. This supports an interpretation centered on keeping the database environment together, but does not establish the original safety argument or any encrypted-storage threat model.

Accepting a directory link as a wallet location and recursively discovering wallets beneath that link are separate policies.

At the head, ListDatabases disables recursion for paths identified as links or reparse points, but still checks whether that directory itself contains a wallet. GetWalletPath separately retains directory acceptance. Thus non-recursion is a discovery boundary, not a blanket prohibition on linked wallet directories. Avoiding traversal through redirected directory trees is consistent with this structure, although its original rationale was not retrieved.

The inspected evidence does not justify treating every Windows reparse point as semantically equivalent to a symbolic link. The historical-policy analogy is narrower for file rejection than for limiting discovery traversal.

The new Windows helper tests only FILE_ATTRIBUTE_REPARSE_POINT. ListDatabases uses that broad classification to stop recursion, whereas GetWalletPath uses it to exclude otherwise eligible regular files from the legacy compatibility exception. The existing directory-versus-file distinction explains where these checks fit, but does not establish that every non-symlink reparse-point file separates wallet data from its database environment or presents the same safety concern. Support for that broader rejection remains unanswered, rather than demonstrated by the existing symlink policy.