fix(rules): read every copy of a duplicated array property - #88
Merged
Conversation
stringArrayContains and arrayMember stopped at the first member with a
given name, so a devcontainer.json that repeated a property was linted
against only its first copy. Since a JSON parser keeps the last one, a
document that plainly set an option was reported as not setting it:
{"securityOpt": [], "securityOpt": ["no-new-privileges"]}
{"runArgs": ["--init"], "runArgs": ["--cap-drop=ALL"]}
both produced a false positive from require-no-new-privileges and
require-cap-drop-all respectively.
Replace arrayMember with arrayMembers, an iterator over every array
member of that name, skipping copies whose value is not an array, and
scan them all. Reading what the document says beats guessing which copy
a parser discards.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Refactor array member lookup to handle duplicate JSON object members correctly. JSON parsers keep only the last copy of a duplicate member, but decolint now reads all copies to avoid incorrectly reporting settings as unset when they appear in earlier duplicates.
Key Changes
arrayMember()function witharrayMembers()iterator that yields all array members with a given name, using Go 1.22'siter.SeqtypestringArrayContains()to use the new iterator, checking all duplicate members instead of returning earlycheckRequireCapDropAll()andcheckRequireNoNewPrivileges()to iterate over all matching array membersrequire_no_new_privileges_test.goandrequire_cap_drop_all_test.goImplementation Details
The new
arrayMembers()function uses a closure-based iterator pattern to yield each array-valued member matching the given name. This allows rules to check all copies of a duplicated member rather than just the first one found. The iterator skips non-array members gracefully, making the code more robust to malformed JSON structures.Test cases verify that:
https://claude.ai/code/session_01Qzzd7Dgx9WfSJHWQzJs8nC