Skip to content

Dictionary enumeration order is relied upon in ConnectionInfo despite being undefined behaviour #719

Closed
@MattJamesChampion

Description

@MattJamesChampion

In ConnectionInfo.cs there are a number of attributes of type IDictionary that rely on the ordering that entities were added to the dictionary, even though the documentation for Dictionary states that the order in which the items are returned is undefined.

As a concrete example, take HostKeyAlgorithms. Modifying HostKeyAlgorithms as part of ConnectionInfo appears to be the method used to specify which algorithms are used and in what order. As an example, reversing the order that the collection contents are added in produces different host key fingerprints, which indicates that the order matters internally. As well as that, the Test_KeyExchangeInitMessage_Load test uses SequenceEqual assertions to check the contents of ServerHostKeyAlgorithms which imply that the order is expected to be consistent.

Since the order that Dictionary items are enumerated is undefined according to the documentation/standard, it's possible that a valid implementation could return values in a different order and therefore cause unexpected and inconsistent results. Using the above example, if a different host key algorithm is successfully negotiated first, the returned fingerprint would be different and could lead to the connection not being trusted (but only when the algorithms are enumerated in a certain order).

The OrderedDictionary class and the IOrderedDictionary interface are potential replacements since they guarantee a consistent order, although they aren't drop-in replacements.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions