Skip to content

Commit 83825d3

Browse files
committed
validate that the path is absolute
1 parent c2ca807 commit 83825d3

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

dsc/tests/dsc_extension_discover.ps1

+41-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Describe 'Discover extension tests' {
55
BeforeAll {
66
$oldPath = $env:PATH
77
$separator = [System.IO.Path]::PathSeparator
8-
$env:PATH = "$PSScriptRoot$separator$oldPath"
8+
$toolPath = Resolve-Path -Path "$PSScriptRoot/../../extensions/test/discover"
9+
$env:PATH = "$toolPath$separator$oldPath"
910
}
1011

1112
AfterAll {
@@ -50,4 +51,43 @@ Describe 'Discover extension tests' {
5051
$out.results[1].type | Should -BeExactly 'Test/DiscoveredTwo'
5152
$out.results[1].result.actualState.Output | Should -BeExactly 'Hello Two'
5253
}
54+
55+
It 'Relative path from discovery will fail' {
56+
$extension_json = @'
57+
{
58+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
59+
"type": "Test/DiscoverRelative",
60+
"version": "0.1.0",
61+
"description": "Test discover resource",
62+
"discover": {
63+
"executable": "pwsh",
64+
"args": [
65+
"-NoLogo",
66+
"-NonInteractive",
67+
"-NoProfile",
68+
"-Command",
69+
"./discover.ps1",
70+
"-RelativePath"
71+
]
72+
}
73+
}
74+
'@
75+
Set-Content -Path "$TestDrive/test.dsc.extension.json" -Value $extension_json
76+
Copy-Item -Path "$toolPath/discover.ps1" -Destination $TestDrive | Out-Null
77+
Copy-Item -Path "$toolPath/resources" -Destination $TestDrive -Recurse | Out-Null
78+
$env:DSC_RESOURCE_PATH = $TestDrive
79+
try {
80+
$out = dsc extension list | ConvertFrom-Json
81+
$out.Count | Should -Be 1
82+
$out.type | Should -Be 'Test/DiscoverRelative'
83+
$out = dsc resource list 2> $TestDrive/error.log
84+
write-verbose -verbose (Get-Content -Path "$TestDrive/error.log" -Raw)
85+
$LASTEXITCODE | Should -Be 0
86+
$out | Should -BeNullOrEmpty
87+
$errorMessage = Get-Content -Path "$TestDrive/error.log" -Raw
88+
$errorMessage | Should -BeLike '*is not an absolute path*'
89+
} finally {
90+
$env:DSC_RESOURCE_PATH = $null
91+
}
92+
}
5393
}

dsc_lib/locales/en-us.toml

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ resourceManifestSchemaDescription = "Defines the JSON Schema the resource manife
169169

170170
[extensions.dscextension]
171171
discoverNoResults = "No results returned for discovery extension '%{extension}'"
172+
discoverNotAbsolutePath = "Resource path from extension '%{extension}' is not an absolute path: %{path}"
172173

173174
[extensions.extension_manifest]
174175
extensionManifestSchemaTitle = "Extension manifest schema URI"

dsc_lib/src/dscerror.rs

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub enum DscError {
2929
#[error("{t} {0} {t2} '{1}'", t = t!("dscerror.commandOperation"), t2 = t!("dscerror.forExecutable"))]
3030
CommandOperation(String, String),
3131

32+
#[error("{0}")]
33+
Extension(String),
34+
3235
#[error("{t} '{0}' {t2}: {1}", t = t!("dscerror.function"), t2 = t!("dscerror.error"))]
3336
Function(String, String),
3437

dsc_lib/src/extensions/dscextension.rs

+3
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ impl DscExtension {
105105
return Err(DscError::Json(err));
106106
}
107107
};
108+
if !Path::new(&discover_result.resource_manifest_path).is_absolute() {
109+
return Err(DscError::Extension(t!("extensions.dscextension.discoverNotAbsolutePath", extension = self.type_name.clone(), path = discover_result.resource_manifest_path.clone()).to_string()));
110+
}
108111
let manifest_path = Path::new(&discover_result.resource_manifest_path);
109112
// Currently we don't support extensions discovering other extensions
110113
if let ManifestResource::Resource(resource) = load_manifest(manifest_path)? {

extensions/test/discover/discover.ps1

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4+
[CmdletBinding()]
5+
param(
6+
[Parameter()]
7+
[switch]$RelativePath
8+
)
9+
410
Get-ChildItem -Path $PSScriptRoot/resources/*.json | ForEach-Object {
511
$resource = [pscustomobject]@{
6-
resourceManifestPath = $_.FullName
12+
resourceManifestPath = if ($RelativePath) {
13+
Resolve-Path -Path $_.FullName -Relative
14+
} else {
15+
$_.FullName
16+
}
717
}
818
$resource | ConvertTo-Json -Compress
919
}

0 commit comments

Comments
 (0)