Repository navigation
Validates package root URIs #10096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Validates package root URIs #10096
Changes from all commits
bed25d3
c94e2e6
42b33a6
351c413
daff89e
6593b7b
df0612a
b73fd27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,19 @@ part of '../server_api.dart'; | |
| /// A namespace for VM service related server request handlers. | ||
| @visibleForTesting | ||
| extension VmServiceHandler on Never { | ||
| /// File name of a Dart or Flutter package manifest. | ||
| static const _pubspecYamlFileName = 'pubspec.yaml'; | ||
|
|
||
| /// Name of the Dart tooling metadata directory in a package root. | ||
| static const _dartToolDirName = '.dart_tool'; | ||
|
|
||
| /// File name of the package configuration file inside [_dartToolDirName]. | ||
| static const _packageConfigJsonFileName = 'package_config.json'; | ||
|
|
||
| /// Environment variables holding the user's home directory on POSIX (`HOME`) | ||
| /// and Windows (`USERPROFILE`). | ||
| static const _homeEnvVarKeys = ['HOME', 'USERPROFILE']; | ||
|
|
||
| /// Stores the calculated package roots for VM service connections that are | ||
| /// initiated in [handleNotifyForVmServiceConnection]. | ||
| /// | ||
|
|
@@ -47,6 +60,138 @@ extension VmServiceHandler on Never { | |
| _packageRootsForVmServiceConnections.clear(); | ||
| } | ||
|
|
||
| /// Validates that [rootPackageUri] is a local `file://` URI pointing to an | ||
| /// existing Dart or Flutter package directory, and not the filesystem root or | ||
| /// the user's home directory (or an ancestor of it). | ||
| /// | ||
| /// Returns the symlink-resolved [Uri] if [rootPackageUri] is valid, or an | ||
| /// error message if it must be rejected. | ||
| @visibleForTesting | ||
| static ({Uri? resolvedUri, String? error}) validateRootPackageUri( | ||
| Uri rootPackageUri, { | ||
| Map<String, String>? environment, | ||
| }) { | ||
| if (!isValidLocalFileUri(rootPackageUri)) { | ||
| return ( | ||
| resolvedUri: null, | ||
| error: | ||
| 'Invalid package root URI ($rootPackageUri): ' | ||
| '$invalidLocalFileUriMessage', | ||
| ); | ||
| } | ||
|
|
||
| final dirPath = p.normalize(rootPackageUri.toFilePath()); | ||
| final dir = Directory(dirPath); | ||
| if (!dir.existsSync()) { | ||
| return ( | ||
| resolvedUri: null, | ||
| error: | ||
| 'Invalid package root URI ($rootPackageUri): directory does not ' | ||
| 'exist.', | ||
| ); | ||
| } | ||
|
|
||
| final String resolvedDirPath; | ||
| try { | ||
| resolvedDirPath = p.normalize(dir.resolveSymbolicLinksSync()); | ||
| } on FileSystemException catch (e) { | ||
| return ( | ||
| resolvedUri: null, | ||
| error: | ||
| 'Invalid package root URI ($rootPackageUri): failed to resolve ' | ||
| 'symbolic links ($e).', | ||
| ); | ||
| } | ||
|
|
||
| if (p.equals(resolvedDirPath, p.rootPrefix(resolvedDirPath))) { | ||
| return ( | ||
| resolvedUri: null, | ||
| error: | ||
| 'Invalid package root URI ($rootPackageUri): cannot be the ' | ||
| 'filesystem root.', | ||
| ); | ||
| } | ||
|
|
||
| if (_isUserHomeOrAncestorDirectory( | ||
| resolvedDirPath, | ||
| environment: environment ?? Platform.environment, | ||
| )) { | ||
| return ( | ||
| resolvedUri: null, | ||
| error: | ||
| 'Invalid package root URI ($rootPackageUri): cannot be the user ' | ||
| 'home directory or an ancestor of it.', | ||
| ); | ||
| } | ||
|
|
||
| final hasPubspec = File( | ||
| p.join(resolvedDirPath, _pubspecYamlFileName), | ||
| ).existsSync(); | ||
| final hasPackageConfig = File( | ||
| p.join(resolvedDirPath, _dartToolDirName, _packageConfigJsonFileName), | ||
| ).existsSync(); | ||
| if (!hasPubspec && !hasPackageConfig) { | ||
| return ( | ||
| resolvedUri: null, | ||
| error: | ||
| 'Invalid package root URI ($rootPackageUri): directory does not ' | ||
| 'contain $_pubspecYamlFileName or ' | ||
| '$_dartToolDirName/$_packageConfigJsonFileName.', | ||
| ); | ||
| } | ||
|
|
||
| return (resolvedUri: Uri.file(resolvedDirPath), error: null); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Windows, |
||
| } | ||
|
|
||
| /// Returns whether [resolvedDirPath] matches or is an ancestor of the | ||
| /// current user's home directory from `HOME` or `USERPROFILE`. | ||
| static bool _isUserHomeOrAncestorDirectory( | ||
| String resolvedDirPath, { | ||
| required Map<String, String> environment, | ||
| }) { | ||
| for (final envKey in _homeEnvVarKeys) { | ||
| final homePath = environment[envKey]; | ||
| if (homePath == null || homePath.isEmpty) continue; | ||
| final normalizedHome = p.normalize(homePath); | ||
| final resolvedHome = | ||
| _tryResolveSymbolicLinks(Directory(normalizedHome)) ?? normalizedHome; | ||
| if (p.equals(resolvedDirPath, resolvedHome) || | ||
| p.isWithin(resolvedDirPath, resolvedHome)) { | ||
| return true; | ||
| } | ||
| } | ||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: |
||
| 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. | ||
| /// | ||
| /// Expects [queryParams] to contain [apiParameterValueKey] (the VM service | ||
| /// URI) and [apiParameterVmServiceConnected] (`'true'` or `'false'`). | ||
| /// | ||
| /// When the DevTools server owns the Dart Tooling Daemon (DevTools was | ||
| /// launched outside of the IDE, and [dtd] has both a [DtdInfo.localUri] and a | ||
| /// [DtdInfo.secret]), this registers or unregisters the VM service with DTD, | ||
| /// resolves and validates the app's package root via | ||
| /// [detectRootPackageForVmService], and updates DTD's IDE workspace roots via | ||
| /// [updateDtdWorkspaceRoots]. | ||
| /// | ||
| /// Returns a [ServerApi.success] no-op response via [api] if [dtd] is | ||
| /// unavailable or has no secret, [ServerApi.badRequest] if required | ||
| /// parameters are missing or invalid, or [ServerApi.serverError] if package | ||
| /// root detection or DTD communication fails. | ||
| static Future<shelf.Response> handleNotifyForVmServiceConnection( | ||
| ServerApi api, | ||
| Map<String, String> queryParams, | ||
|
|
@@ -171,7 +316,21 @@ extension VmServiceHandler on Never { | |
| uri: null, | ||
| ); | ||
| } | ||
| rootPackageUri = Uri.parse(root); | ||
| final parsedRoot = Uri.tryParse(root); | ||
| if (parsedRoot == null) { | ||
| return ( | ||
| success: false, | ||
| message: | ||
| 'Invalid package root URI ($root): ' | ||
| '$invalidLocalFileUriMessage', | ||
| uri: null, | ||
| ); | ||
| } | ||
| final validationResult = validateRootPackageUri(parsedRoot); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: we can destructure the record and check final (:resolvedUri, :error) = validateRootPackageUri(parsedRoot);
if (resolvedUri == null) {
return (success: false, message: error, uri: null);
}
rootPackageUri = resolvedUri; |
||
| if (validationResult.error != null) { | ||
| return (success: false, message: validationResult.error, uri: null); | ||
| } | ||
| rootPackageUri = validationResult.resolvedUri!; | ||
| if (cacheResult) { | ||
| _packageRootsForVmServiceConnections[normalizedUriAsString] = | ||
| rootPackageUri; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,3 +100,40 @@ String _assertUriFormatAndReturn(String uriString) { | |
| ); | ||
| return uriString; | ||
| } | ||
|
|
||
| /// Error description for invalid local `file://` URIs. | ||
| const invalidLocalFileUriMessage = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: do we want |
||
| 'must be an absolute file:// URI with no host, query, or fragment.'; | ||
|
|
||
| /// 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 | ||
| /// [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 || | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still lets Windows UNC paths slip through via four leading slashes (e.g. Can we also reject |
||
| uri.hasQuery || | ||
| uri.hasFragment) { | ||
| return false; | ||
| } | ||
| try { | ||
| final filePath = uri.toFilePath(); | ||
| return !filePath.contains('\x00'); | ||
| } catch (_) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /// Returns whether [uriString] is a `file://` URI string that parses as a valid | ||
| /// local `file:` URI via [isValidLocalFileUri]. | ||
| bool isValidLocalFileUriString(String? uriString) { | ||
| if (uriString == null || !uriString.startsWith(_fileUriPrefix)) { | ||
| return false; | ||
| } | ||
| final uri = Uri.tryParse(uriString); | ||
| return uri != null && isValidLocalFileUri(uri); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.