[agent] Address PR review feedback on UNC URI validation and symlink checks
diff --git a/packages/devtools_shared/CHANGELOG.md b/packages/devtools_shared/CHANGELOG.md index 50ec8d1..5e14030 100644 --- a/packages/devtools_shared/CHANGELOG.md +++ b/packages/devtools_shared/CHANGELOG.md
@@ -41,10 +41,10 @@ `api/serveAvailableExtensions`. * Add `VmServicePackageRootExtension.rootPackageDirectoryForMainIsolate` to `devtools_shared.dart`. -* Add `isValidLocalFileUri`, `isValidLocalFileUriString`, and - `invalidLocalFileUriMessage` helpers to validate local `file://` URIs without - authorities, queries, fragments, or null bytes, and apply them in - `api/extensionEnabledState`, `VmServiceHandler`, and `ExtensionsManager`. +* Add `isValidLocalFileUri` and `isValidLocalFileUriString` helpers to validate + local `file://` URIs without authorities, queries, fragments, or null bytes, + and apply them in `api/extensionEnabledState`, `VmServiceHandler`, and + `ExtensionsManager`. * Validate and resolve symlinks for detected package roots in `VmServiceHandler` before caching or updating DTD IDE workspace roots.
diff --git a/packages/devtools_shared/lib/devtools_shared.dart b/packages/devtools_shared/lib/devtools_shared.dart index a9e9e3b..3815e81 100644 --- a/packages/devtools_shared/lib/devtools_shared.dart +++ b/packages/devtools_shared/lib/devtools_shared.dart
@@ -13,7 +13,7 @@ export 'src/service_utils.dart'; export 'src/sse/sse_shim.dart'; export 'src/utils/compare.dart'; -export 'src/utils/file_utils.dart'; +export 'src/utils/file_utils.dart' hide invalidLocalFileUriMessage; export 'src/utils/retry.dart'; export 'src/utils/semantic_version.dart'; export 'src/utils/serialization.dart';
diff --git a/packages/devtools_shared/lib/src/server/handlers/_vm_service.dart b/packages/devtools_shared/lib/src/server/handlers/_vm_service.dart index c0271ed..126d8c8 100644 --- a/packages/devtools_shared/lib/src/server/handlers/_vm_service.dart +++ b/packages/devtools_shared/lib/src/server/handlers/_vm_service.dart
@@ -103,6 +103,16 @@ ); } + final resolvedUri = Uri.file(resolvedDirPath); + if (!isValidLocalFileUri(resolvedUri)) { + return ( + resolvedUri: null, + error: + 'Invalid package root URI ($rootPackageUri): ' + '$invalidLocalFileUriMessage', + ); + } + if (p.equals(resolvedDirPath, p.rootPrefix(resolvedDirPath))) { return ( resolvedUri: null, @@ -140,7 +150,7 @@ ); } - return (resolvedUri: Uri.file(resolvedDirPath), error: null); + return (resolvedUri: resolvedUri, error: null); } /// Returns whether [resolvedDirPath] matches or is an ancestor of the @@ -153,8 +163,14 @@ final homePath = environment[envKey]; if (homePath == null || homePath.isEmpty) continue; final normalizedHome = p.normalize(homePath); - final resolvedHome = - _tryResolveSymbolicLinks(Directory(normalizedHome)) ?? normalizedHome; + String resolvedHome; + try { + resolvedHome = p.normalize( + Directory(normalizedHome).resolveSymbolicLinksSync(), + ); + } on FileSystemException { + resolvedHome = normalizedHome; + } if (p.equals(resolvedDirPath, resolvedHome) || p.isWithin(resolvedDirPath, resolvedHome)) { return true; @@ -163,17 +179,6 @@ return false; } - /// Resolves symbolic links for [directory] if it exists, returning `null` if - /// it does not exist or resolution fails. - static String? _tryResolveSymbolicLinks(Directory directory) { - if (!directory.existsSync()) return null; - try { - return p.normalize(directory.resolveSymbolicLinksSync()); - } on FileSystemException { - return null; - } - } - /// Handles an [apiNotifyForVmServiceConnection] request notifying the /// DevTools server that a DevTools client connected to or disconnected from a /// VM service. @@ -326,11 +331,11 @@ uri: null, ); } - final validationResult = validateRootPackageUri(parsedRoot); - if (validationResult.error != null) { - return (success: false, message: validationResult.error, uri: null); + final (:resolvedUri, :error) = validateRootPackageUri(parsedRoot); + if (resolvedUri == null) { + return (success: false, message: error, uri: null); } - rootPackageUri = validationResult.resolvedUri!; + rootPackageUri = resolvedUri; if (cacheResult) { _packageRootsForVmServiceConnections[normalizedUriAsString] = rootPackageUri;
diff --git a/packages/devtools_shared/lib/src/utils/file_utils.dart b/packages/devtools_shared/lib/src/utils/file_utils.dart index b22569c..449e6c9 100644 --- a/packages/devtools_shared/lib/src/utils/file_utils.dart +++ b/packages/devtools_shared/lib/src/utils/file_utils.dart
@@ -108,14 +108,16 @@ /// Returns whether [uri] is a valid local `file:` URI without an authority, /// query, fragment, or null bytes, and can be converted to a local file path. /// -/// Requiring an empty authority rejects remote hosts, ports, user info, and -/// Windows UNC paths (e.g. `file://server/share/...`). Checking +/// Requiring an empty authority and disallowing a leading `//` in the path +/// rejects remote hosts, ports, user info, and Windows UNC paths (e.g. +/// `file://server/share/...` or `file:////server/share/...`). Checking /// [Uri.toFilePath] ensures callers can convert [uri] without triggering an /// [UnsupportedError] or [FormatException]. bool isValidLocalFileUri(Uri uri) { if (uri.scheme != 'file' || uri.authority.isNotEmpty || !uri.hasAbsolutePath || + uri.path.startsWith('//') || uri.hasQuery || uri.hasFragment) { return false;
diff --git a/packages/devtools_shared/test/utils/file_utils_test.dart b/packages/devtools_shared/test/utils/file_utils_test.dart index 85e6114..c397d11 100644 --- a/packages/devtools_shared/test/utils/file_utils_test.dart +++ b/packages/devtools_shared/test/utils/file_utils_test.dart
@@ -190,23 +190,27 @@ ); }); - test('rejects non-empty authorities, queries, and fragments', () { - for (final invalid in const [ - 'file://server/share/my_app', - 'file://localhost/Users/me/my_app', - 'file://127.0.0.1/Users/me/my_app', - 'file://user@/Users/me/my_app', - 'file:///Users/me/my_app?query=1', - 'file:///Users/me/my_app#fragment', - ]) { - expect(isValidLocalFileUriString(invalid), isFalse, reason: invalid); - expect( - isValidLocalFileUri(Uri.parse(invalid)), - isFalse, - reason: invalid, - ); - } - }); + test( + 'rejects non-empty authorities, UNC paths, queries, and fragments', + () { + for (final invalid in const [ + 'file://server/share/my_app', + 'file:////server/share/my_app', + 'file://localhost/Users/me/my_app', + 'file://127.0.0.1/Users/me/my_app', + 'file://user@/Users/me/my_app', + 'file:///Users/me/my_app?query=1', + 'file:///Users/me/my_app#fragment', + ]) { + expect(isValidLocalFileUriString(invalid), isFalse, reason: invalid); + expect( + isValidLocalFileUri(Uri.parse(invalid)), + isFalse, + reason: invalid, + ); + } + }, + ); test('rejects encoded slashes, null bytes, and invalid file paths', () { for (final invalid in [