Skip to content

Commit eb3ec7e

Browse files
Protect reserved context configuration
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 0b36e9b commit eb3ec7e

7 files changed

Lines changed: 64 additions & 2 deletions

src/functions/private/Auth/Set-DomeneshopDefaultContext.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function Set-DomeneshopDefaultContext {
3535
# The name of an existing Domeneshop context.
3636
[Parameter(Mandatory)]
3737
[ValidateNotNullOrEmpty()]
38+
[ValidateScript({ Test-DomeneshopContextName -Context $_ })]
3839
[string] $Context
3940
)
4041

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function Test-DomeneshopContextName {
2+
<#
3+
.SYNOPSIS
4+
Validate a Domeneshop credential context name.
5+
6+
.DESCRIPTION
7+
Reject names reserved for internal Domeneshop module configuration.
8+
9+
.EXAMPLE
10+
Test-DomeneshopContextName -Context 'production'
11+
12+
Confirm that production can be used as a credential context name.
13+
14+
.INPUTS
15+
None
16+
17+
You can't pipe objects to Test-DomeneshopContextName.
18+
19+
.OUTPUTS
20+
System.Boolean
21+
22+
True when the context name is available for credentials.
23+
24+
.NOTES
25+
Throws when the context name is reserved.
26+
#>
27+
[OutputType([bool])]
28+
[CmdletBinding()]
29+
param(
30+
# The proposed credential context name.
31+
[Parameter(Mandatory)]
32+
[ValidateNotNullOrEmpty()]
33+
[string] $Context
34+
)
35+
36+
if ($Context -eq '__Domeneshop.Config') {
37+
throw "Context name [$Context] is reserved for Domeneshop module configuration."
38+
}
39+
40+
$true
41+
}

src/functions/public/Auth/Connect-DomeneshopAccount.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ function Connect-DomeneshopAccount {
66
Stores Domeneshop API credentials in a secure context.
77
88
.DESCRIPTION
9-
Stores Domeneshop API credentials using the Context module and optionally sets the context as default.
9+
Stores Domeneshop API credentials using the Context module. The first stored context becomes the
10+
default automatically, and Default replaces an existing default.
1011
1112
.EXAMPLE
1213
Connect-DomeneshopAccount -Token 'my-token' -Secret (Read-Host -AsSecureString)
@@ -49,9 +50,10 @@ function Connect-DomeneshopAccount {
4950
# The name used to store and retrieve this credential context.
5051
[Parameter()]
5152
[ValidateNotNullOrEmpty()]
53+
[ValidateScript({ Test-DomeneshopContextName -Context $_ })]
5254
[string] $Context = 'default',
5355

54-
# Set this context as the default for commands that omit Context.
56+
# Replace the default context; the first stored context becomes the default automatically.
5557
[Parameter()]
5658
[switch] $Default,
5759

src/functions/public/Auth/Get-DomeneshopContext.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function Get-DomeneshopContext {
4040
# The name of a specific stored context.
4141
[Parameter(Mandatory, ParameterSetName = 'Get named')]
4242
[ValidateNotNullOrEmpty()]
43+
[ValidateScript({ Test-DomeneshopContextName -Context $_ })]
4344
[string] $Context,
4445

4546
# List every stored Domeneshop credential context.

tests/Connect-DomeneshopAccount.Tests.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@ Describe 'Connect-DomeneshopAccount' {
4848
{ Connect-DomeneshopAccount -Token 'token' -Secret 42 -Context 'demo' } |
4949
Should -Throw '*Secret must be a SecureString or String value*'
5050
}
51+
52+
It 'rejects the reserved module configuration context name' {
53+
{ Connect-DomeneshopAccount -Token 'token' -Secret 'secret' -Context '__Domeneshop.Config' } |
54+
Should -Throw '*reserved for Domeneshop module configuration*'
55+
56+
Should -Invoke Set-Context -Times 0 -Exactly
57+
}
5158
}

tests/Domeneshop.Private.Tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ Describe 'Domeneshop private helpers' {
4343
Should -Throw '*invalid API base URI*'
4444
}
4545

46+
It 'rejects the reserved module configuration name as a default context' {
47+
{ Set-DomeneshopDefaultContext -Context '__Domeneshop.Config' -Confirm:$false } |
48+
Should -Throw '*reserved for Domeneshop module configuration*'
49+
}
50+
4651
It 'uses basic authentication and terminating transport errors' {
4752
Mock Invoke-RestMethod { [pscustomobject]@{ ok = $true } }
4853

tests/Get-DomeneshopContext.Tests.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ Describe 'Get-DomeneshopContext' {
3434
{ Get-DomeneshopContext } | Should -Throw '*No default Domeneshop context found*'
3535
}
3636

37+
It 'rejects an explicit request for the reserved module configuration context' {
38+
{ Get-DomeneshopContext -Context '__Domeneshop.Config' } |
39+
Should -Throw '*reserved for Domeneshop module configuration*'
40+
}
41+
3742
It 'excludes the module configuration when listing contexts' {
3843
Mock Get-Context {
3944
@(

0 commit comments

Comments
 (0)