sudo-project / sudo

Utility to execute a command as another user
https://www.sudo.ws
Other
1.2k stars 224 forks source link

cvtsudoers outputs invalid JSON (duplicate sha256 key) #370

Closed es-fabricemarie closed 7 months ago

es-fabricemarie commented 7 months ago

Using the following sudoers file in /etc/sudoers.d/test5:

Cmnd_Alias SH_TEST = sha256:hOtoe/iK6SlGg7w4BfZBBdSsXjUmTJ5+ts51yjh7vkM=, \
sha256:1IXHRCxXgSnIEnb+xBz4PAfWaPdXIBWKFF0QCwxJ5G4= /bin/sh
Cmnd_Alias EDIT = sudoedit /etc/motd

Using the command:

cvtsudoers --defaults=all --output-format=JSON --input-format=SUDOERS --output=- /etc/sudoers.d/test5

We get the following invalid JSON output:

{
    "Command_Aliases": {
        "EDIT": [
            { "command": "sudoedit /etc/motd" }
        ],
        "SH_TEST": [
            {
                "command": "/bin/sh",
                "sha256": "hOtoe/iK6SlGg7w4BfZBBdSsXjUmTJ5+ts51yjh7vkM=",
                "sha256": "1IXHRCxXgSnIEnb+xBz4PAfWaPdXIBWKFF0QCwxJ5G4="
            }
        ]
    }
}

The issue here is the duplicated sha256 key in the dict/hash. Potential results of parsing this with various library are:

Instead, it should probably output this:

{
    "Command_Aliases": {
        "EDIT": [
            { "command": "sudoedit /etc/motd" }
        ],
        "SH_TEST": [
            {
                "command": "/bin/sh",
                "sha256": [
                    "hOtoe/iK6SlGg7w4BfZBBdSsXjUmTJ5+ts51yjh7vkM=",
                    "1IXHRCxXgSnIEnb+xBz4PAfWaPdXIBWKFF0QCwxJ5G4="
                ]
            }
        ]
    }
}

I haven't tested other checksums, but the rest of them probably have the same issue.

millert commented 7 months ago

Yes, that makes sense. I've made that change in c429220. This no longer preserves the order of the digests but I don't think that is a problem as the order doesn't really matter.