candiedyaml: honour !!str tag in resolveInterface to prevent YAML 1.1 bool coercion#39
Open
ivzhelev wants to merge 1 commit into
Open
Conversation
… bool coercion
When `resolveInterface` is called for a scalar that carries an explicit
`!!str` tag (e.g. `!!str off`), it currently ignores the tag and falls
through to the character-based type dispatch, where `'o'` matches the
`bools` byte set and `resolve_bool("off")` returns `bool(false)`.
The YAML specification requires that an explicit tag overrides implicit
type resolution. Add an early-return guard:
if string(event.tag) == yaml_STR_TAG {
return yaml_STR_TAG, val
}
This mirrors the existing guard for untagged non-implicit scalars
(line 367) and is consistent with how `resolve_string` already handles
typed decodes — it returns the raw string value whenever the target
Go type is `string`.
The `resolve()` function (typed decodes) is unaffected: it dispatches
on the target Go `reflect.Kind` before reaching `resolveInterface`, so
decoding a `!!str off` scalar into a `bool` field still produces the
correct error via `resolve_bool`.
Also adds a Ginkgo test context "Explicit !!str tag preserves string
(no bool coercion)" covering all 16 YAML 1.1 boolean tokens.
Fixes: mandelsoft#38
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.
Problem
resolveInterfaceinlegacy/candiedyaml/resolver.goignores an explicit!!strtag when dispatching scalar values. A scalar such as!!str offhasevent.tag = "tag:yaml.org,2002:str"andevent.implicit = false, but the existing guard at line 367:does not fire — because
len(event.tag) != 0. The function then falls through to the character-based type dispatch, where'o'matches theboolsbyte set andresolve_bool("off")succeeds, returning(yaml_BOOL_TAG, false).The result: any YAML emitter that writes
!!str off(the standard way to protect a YAML 1.1 boolean-colliding value) produces data that candiedyaml decodes asbool(false)instead ofstring("off"). Any downstream code relying on string semantics (jqpaths like.config.basic_auth_handling // "default", string comparisons) then silently fails.Fix
Add an early-return guard for the
!!strtag immediately after the existing implicit/no-tag guard:This is a 3-line change. All other type dispatch (int, float, bool, null, binary, timestamp) is unchanged.
The
resolve()function for typed decodes is unaffected — it dispatches on the target Goreflect.Kindbefore callingresolveInterface, soresolve_boolstill handles!!bool offand decoding!!str offinto aboolfield still returns the expected error.Tests
Adds a new Ginkgo context
"Explicit !!str tag preserves string (no bool coercion)"inresolver_test.gocovering all 16 YAML 1.1 boolean tokens (on,ON,off,OFF,yes,YES,no,NO,true,TRUE,false,FALSE,y,Y,n,N) tagged with!!str.Fixes #38