dotnet / dotnet/runtime

ConfigurationManager should reject or handle sectionGroup names containing '/'

Open
#125,185 3 comments 1 reaction 2 assignees Claimed by @steveisok View on GitHub
area-System.Configuration
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Description

ConfigurationManager.OpenMappedExeConfiguration produces a Configuration object whose SectionGroups collection yields null entries when the config file contains a element with a name attribute containing a forward slash (/).

This causes a NullReferenceException for any code that enumerates config.SectionGroups, including the pattern shown in [Microsoft's own documentation example](https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationsectiongroup?view=windowsdesktop-10.0#examples).

### Reproduction Steps

```
using System.Configuration;

var xml = """






""";

var tmpFile = Path.GetTempFileName();
File.WriteAllText(tmpFile, xml);

try
{
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = tmpFile };
var config = ConfigurationManager.OpenMappedExeConfiguration(
fileMap, ConfigurationUserLevel.None);

Console.WriteLine($"SectionGroups.Count = {config.SectionGroups.Count}");

foreach (ConfigurationSectionGroup group in config.SectionGroups)
{
Console.WriteLine($"Group name: {group.Name}");
}
}
finally
{
File.Delete(tmpFile);
}
```
Project file
```


Exe
net10.0
enable



```

run with `dotnet run`.

### Expected behavior

Consistent info between SecionGroups.Count and non-null entries in the collection. The SectionGroups enumerator skips entries it cannot resolve rather than yielding null.

### Actual behavior

```
SectionGroups.Count = 1
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at Program.$(String[] args) in Program.cs:line 21
```

### Regression?

Unknown.

### Known Workarounds

Add a null check when enumerating section groups:
```
foreach (ConfigurationSectionGroup group in config.SectionGroups)
{
if (group is null) continue;
// ...
}
```

### Configuration

- .NET SDK 10.0.103
- System.Configuration.ConfigurationManager NuGet package 10.0.3
- Reproduced on Linux x64 (WSL2) and Windows x64

### Other information

The root cause appears to be that `ConfigurationManager` uses `/` as an internal path separator for section group hierarchies (e.g., `system.web.extensions/scripting/webServices`). When a `` element has a name containing `/`, the internal lookup by path fails to resolve the group record, returning `null` — but the count and enumerator still report it as present.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.