Skip to content

Validates package root URIs - #10096

Merged
johnpryan merged 9 commits into
masterfrom
harden-dtd-workspace-roots
Dec 1, 2026
Merged

johnpryan merged 9 commits into
masterfrom
harden-dtd-workspace-roots

Conversation

@johnpryan

@johnpryan johnpryan commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

This change validates and resolves symlinks for the detected directory in VmServiceHandler before caching the resolved URI or updating DTD, requiring that it:

  • Is a local file:// URI with no authority, query, fragment, or null bytes that converts cleanly to a local file path
  • Does not point or resolve (via symlinks) to the filesystem root, the user's home directory (HOME / USERPROFILE), or an ancestor of it, and exists on disk
  • Contains either pubspec.yaml or .dart_tool/package_config.json.

It also extracts shared isValidLocalFileUri, isValidLocalFileUriString, and invalidLocalFileUriMessage helpers in file_utils.dart (reused by ExtensionsManager and the extensions API handler) and adds unit and server API tests covering valid and symlink-resolved roots, invalid URI forms, filesystem/home/home-ancestor root rejection, missing package markers, and end-to-end /api/notifyForVmServiceConnection handling.

Makes progress towards:

This change validates the detected directory in VmServiceHandler before caching
it or updating DTD, requiring that it:

is a local file:// URI with no authority, query, fragment, or dot-segment
traversal (. / ..), does not point or resolve (via symlinks) to the filesystem
root or the user's home directory (HOME / USERPROFILE), and exists on disk and
contains either pubspec.yaml or .dart_tool/package_config.json.

It also extracts shared isValidLocalFileUri and isValidLocalFileUriString
helpers in file_utils.dart (reused by ExtensionsManager and the extensions API
handler) and adds unit and server API tests covering valid roots, invalid URI
forms, filesystem/home root rejection, missing package markers, and end-to-end
/api/notifyForVmServiceConnection handling.

Makes progress towards:
* flutter/dash-devexp-internal#88
* flutter/dash-devexp-internal#93

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces package root validation in VmServiceHandler before caching or updating Dart Tooling Daemon (DTD) workspace roots, utilizing new local file URI validation helpers to prevent path traversal and reject invalid roots (such as the filesystem root or user home directory). The review feedback highlights a critical logical issue where validating the package root during disconnect flows can prevent proper cleanup of DTD workspace roots if the directory or its package markers were deleted during the session. It is recommended to skip validation when connected is false to ensure stale roots are successfully removed.

@johnpryan
johnpryan requested a review from a team as a code owner November 18, 2026 23:30
@johnpryan
johnpryan requested review from srawlins and removed request for a team November 18, 2026 23:30
Comment thread packages/devtools_shared/CHANGELOG.md Resolved
… validation.

* Reject encoded slashes (`/` / `%2f`) in `isValidLocalFileUri` path segments and validate raw `root` strings with `isValidLocalFileUriString` before `Uri.parse` in `detectRootPackageForVmService`, with new `%2f` test coverage.
* Simplify `_validateRootPackageUri` to check `dir.existsSync()` first, resolve symlinks once (failing closed on `FileSystemException`), and compare only the resolved path against the filesystem root and resolved home directory.
* Remove duplicate `_validateRootPackageUri` validation from `updateDtdWorkspaceRoots` and move its rejection tests to standalone `isValidRootPackageUri` unit tests without DTD.
* Extract the repeated `runWithRetry` polling blocks in `detectRootPackageForVmService` tests into a shared `detectRootWithRetry` helper.
* Update invalid-URI error messages in `_vm_service.dart` and `_devtools_extensions.dart`, add doc comments to `VmServiceHandler` constants, remove redundant `@visibleForTesting`, and document the new helpers and stricter URI checks in `packages/devtools_shared/CHANGELOG.md`.
- Move `_invalidLocalFileUriMessage` to a library-private constant in `server_api.dart` and reuse it in `_devtools_extensions.dart` and `_vm_service.dart` so invalid local file URI error messages stay in sync.
- Expose `VmServiceHandler.validateRootPackageUri` as `@visibleForTesting`, remove `isValidRootPackageUri`, and assert on specific rejection reason messages in `general_api_test.dart`.
- Trim the invalid local file URI test in `general_api_test.dart` to a single query URI case since `file_utils_test.dart` already covers all invalid URI components.
- Restore plain `Uri.parse` fixtures in `updateDtdWorkspaceRoots` tests and remove the unnecessary temporary directory setup and deleted-directory test.
- Document `detectRootWithRetry`'s default `isReady` behavior and simplify its `isReady` callbacks to use null-aware `contains`.
…e-roots

# Conflicts:
#	packages/devtools_shared/CHANGELOG.md
#	packages/devtools_shared/lib/src/server/handlers/_devtools_extensions.dart
#	packages/devtools_shared/lib/src/server/handlers/_vm_service.dart
#	packages/devtools_shared/test/server/general_api_test.dart

@bkonyi bkonyi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments on URI validation and symlink handling.

(Note: This review was posted by an AI agent on behalf of @bkonyi.)

@johnpryan

Copy link
Copy Markdown
Contributor Author

@kenzieschmoll @bkonyi this is ready for another look

@kenzieschmoll kenzieschmoll left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, though I'd recommend waiting for @bkonyi's approval before landing.

@bkonyi bkonyi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@johnpryan
johnpryan merged commit 3c2319c into master Dec 1, 2026
54 checks passed
@johnpryan
johnpryan deleted the harden-dtd-workspace-roots branch December 1, 2026 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants