Skip to content

Commit 46e46b2

Browse files
committed
implements searchbase validation
1 parent 1245a2e commit 46e46b2

File tree

7 files changed

+41
-10
lines changed

7 files changed

+41
-10
lines changed

docs/en-US/Get-ADEffectiveAccess.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ Specifies the AD DS instance to connect to. Accepts:
159159
- Fully qualified domain name
160160
- NetBIOS name
161161
- Directory server name (with optional port, e.g. `myDC01:636`)
162-
- Global Catalog (e.g. `GC://myChildDomain`)
163162

164163
Defaults to the current domain if not specified.
165164

module/ADEffectiveAccess.Format.ps1xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
<Alignment>Left</Alignment>
1919
</TableColumnHeader>
2020
<TableColumnHeader>
21-
<Label>ActiveDirectoryRights</Label>
21+
<Label>ObjectType</Label>
2222
<Alignment>Left</Alignment>
2323
</TableColumnHeader>
2424
<TableColumnHeader>
25-
<Label>ObjectType</Label>
25+
<Label>InheritedObjectType</Label>
2626
<Alignment>Left</Alignment>
2727
</TableColumnHeader>
2828
<TableColumnHeader>
29-
<Label>InheritedObjectType</Label>
29+
<Label>ActiveDirectoryRights</Label>
3030
<Alignment>Left</Alignment>
3131
</TableColumnHeader>
3232
</TableHeaders>
@@ -36,15 +36,15 @@
3636
<TableColumnItem>
3737
<PropertyName>IdentityReference</PropertyName>
3838
</TableColumnItem>
39-
<TableColumnItem>
40-
<PropertyName>ActiveDirectoryRights</PropertyName>
41-
</TableColumnItem>
4239
<TableColumnItem>
4340
<PropertyName>ObjectTypeToString</PropertyName>
4441
</TableColumnItem>
4542
<TableColumnItem>
4643
<PropertyName>InheritedObjectTypeToString</PropertyName>
4744
</TableColumnItem>
45+
<TableColumnItem>
46+
<PropertyName>ActiveDirectoryRights</PropertyName>
47+
</TableColumnItem>
4848
</TableColumnItems>
4949
</TableRowEntry>
5050
</TableRowEntries>

src/ADEffectiveAccess/DirectoryEntryBuilder.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal DirectoryEntryBuilder(
3131
_password = credential?.GetNetworkCredential().Password;
3232
_authenticationTypes = authenticationTypes;
3333
DomainEntry = Create(server: server);
34-
SearchBase = searchBase is null ? DomainEntry : Create(searchBase: searchBase);
34+
SearchBase = ResolveSearchBase(searchBase);
3535
}
3636

3737
internal DirectoryEntry Create(string? server = null, string? searchBase = null)
@@ -47,7 +47,31 @@ internal DirectoryEntry Create(string? server = null, string? searchBase = null)
4747
if (path is not null && !path.Contains("://"))
4848
path = $"LDAP://{path}";
4949

50-
return new DirectoryEntry(path, _username, _password, _authenticationTypes);
50+
DirectoryEntry entry = new(path, _username, _password, _authenticationTypes);
51+
_ = entry.NativeObject; // force bind
52+
return entry;
53+
}
54+
55+
private DirectoryEntry ResolveSearchBase(string? searchBase)
56+
{
57+
if (searchBase is null) return DomainEntry;
58+
59+
if (!searchBase.Contains("="))
60+
throw new ArgumentException(
61+
$"SearchBase '{searchBase}' is not a valid DistinguishedName. " +
62+
"It must follow the format 'OU=Name,DC=domain,DC=com' for an Organizational Unit or Container.",
63+
nameof(searchBase));
64+
65+
try
66+
{
67+
return Create(searchBase: searchBase);
68+
}
69+
catch (Exception exception)
70+
{
71+
throw new ArgumentException(
72+
$"SearchBase '{searchBase}' could not be found in '{DomainDistinguishedName}'.",
73+
nameof(searchBase), innerException: exception);
74+
}
5175
}
5276

5377
public void Dispose()

src/ADEffectiveAccess/Extensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ internal static string ToFilter(this string identity)
5858
}
5959

6060
internal static T GetProperty<T>(this SearchResult search, string property)
61-
=> LanguagePrimitives.ConvertTo<T>(search.Properties[property][0]);
61+
=> TryGetProperty(search, property, out T? value) ? value
62+
: throw new ArgumentNullException(
63+
$"Attribute '{property}' is null or empty for path '{search.Path}'.");
64+
6265

6366
internal static bool TryGetProperty<T>(
6467
this SearchResult search,

src/ADEffectiveAccess/GetADEffectiveAccessComand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ protected override void BeginProcessing()
8888
}
8989
catch (Exception exception)
9090
{
91+
GuidResolver.ClearFromTLS();
9192
exception.ThrowGuidResolverError(this);
9293
}
9394
}

src/ADEffectiveAccess/GuidResolver.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ private GuidResolver() { }
2222

2323
internal static GuidResolver GetFromTLS() => _state.GetFromTLS();
2424

25+
internal static void ClearFromTLS() => _state.ClearFromTLS();
26+
2527
internal void SetContext(string? server, DirectoryEntryBuilder builder)
2628
{
2729
using DirectoryEntry rootDSE = builder.Create(server, "RootDSE");

src/ADEffectiveAccess/RunspaceSpecificStorage.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ internal sealed class RunspaceSpecificStorage<T>(Func<T> factory)
1717

1818
internal T GetForRunspace(Runspace runspace)
1919
=> _map.GetValue(runspace, _ => new Lazy<T>(() => _factory(), _mode)).Value;
20+
21+
internal void ClearFromTLS() => _map.Remove(Runspace.DefaultRunspace);
2022
}

0 commit comments

Comments
 (0)