Repository navigation
fix: SafeLoad must refuse, and "deny everything" must be expressible - #33
Merged
Merged
Conversation
WithPermittedClasses(names ...string) did `append(o.permittedClasses, names...)` and the guard downstream was `if o.permittedClasses != nil`. With ZERO names that appends nothing and leaves the slice nil, so WithPermittedClasses() -- "permit no class", the most restrictive request the API can express -- arrived at the same nil as "no option passed" and took the no-policy branch, materialising every class in the document. The fix deletes that state rather than making it merely distinguishable. SafeLoad now ALWAYS restricts, and resolve() is the single constructor of an options value, so the allow-list is a non-nil set everywhere. Load is the unrestricted entry point, as Psych.unsafe_load is. This matches Psych, whose safe_load signature is `permitted_classes: []`: Restricted seeds its allow-list from that list alone (psych/class_loader.rb:78-82), so absent and empty are the same policy there, and that policy refuses everything. SafeLoad also no longer DEGRADES an unpermitted object into its bare ivar mapping. A caller that asked for a refusal got a *Map -- type confusion in place of an error, and silent, because the document had "loaded". It now returns a *DisallowedClassError carrying the class name, with Psych::DisallowedClass's own message so a host binding surfaces it verbatim. The restriction is widened to what Psych's class loader actually gates: Symbol (in values AND in keys), time.Time, Range, Regexp, and the class NAMED by a !ruby/class / !ruby/module tag. Refusing tagged objects while interning Symbols and building Times was half a policy. WithPermittedSymbols covers permitted_symbols:, including the asymmetry that an EMPTY symbol list narrows nothing while an empty class list permits nothing (Restricted#symbolize short-circuits on @symbols.empty?). Aliases are refused by default, as Psych does. The parser cannot be asked after the fact -- a resolved scalar alias is indistinguishable from a repeated literal -- so the loader now REPORTS alias use, and SafeLoad applies the policy: *AliasesNotEnabledError without WithAliases(true), and *AnchorNotDefinedError for an alias whose anchor the document never defines (which previously resolved to a silent nil). Every expected value in the new tests was measured against ruby 4.0.5's bundled Psych. Three tests that asserted the old behaviour are replaced by the positive invariant, because they were assertions of the defect: one required an unpermitted object to come back as a *Map, and one required a missing allow-list to keep every object. Coverage stays at 100% under -race. The gate named one branch this change introduced as unreachable -- a nil check in WithPermittedClasses -- and it was: resolve establishes the invariant before any Option runs. It is gone rather than registered, since an unreachable "just in case" nil is how this defect existed in the first place. Paired with go-embedded-ruby/ruby#775. rbgo does NOT need a release of this library to be fixed: it loads with Load and enforces Psych's restriction in its own Psych binding. Once this is tagged, that binding can delegate here and drop its copy of the walk. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
What this fixes
WithPermittedClasses(names ...string)did:and the guard downstream was
if o.permittedClasses != nil.With zero names,
appendadds nothing and the slice staysnil. SoWithPermittedClasses()— permit no class, the most restrictive request thisAPI can express — arrived at the same
nilas no option passed at all, andtook the no-policy branch.
SafeLoadthen materialised every class thedocument named.
A second defect sat behind it: when the allow-list did apply,
SafeLoadrewrote an unpermitted
!ruby/object:into its bare ivar mapping instead ofreporting it. A caller who asked for a refusal got a
*Map— type confusion inplace of an error, and silent, because the document had "loaded successfully".
MRI source: what the default permitted set actually is
Ruby 4.0.5, bundled Psych.
Psych.safe_load's signature (psych.rb:323):Psych::ClassLoader::Restricted(psych/class_loader.rb:77-103):@classesis seeded frompermitted_classesand from nothing else. So inMRI the restricted default set is empty, and "no keyword" and "the empty
list" are the same policy — refuse everything.
psych.rb:278-287does documentTrueClass / FalseClass / NilClass / Integer / Float / String / Array / Hashas permitted by default, and that is easy tomisread as a list
Restrictedholds. It holds none of their names. They loadbecause
Psych::Visitors::ToRubybuilds those shapes without consulting theclass loader at all. Everything that does reach it —
Symbol,Time,Date,Range,Regexp,Struct,BigDecimal, every!ruby/object:tag —is refused until named.
Message, from
psych/exception.rb:23-27:The change
The ambiguous state is deleted, not merely made distinguishable.
SafeLoadnow always restricts;
resolveis the only constructor of anoptionsvalueand seeds a non-nil allow-list, so there is no
nil-vs-empty question left toget wrong.
Loadis the unrestricted entry point, asPsych.unsafe_loadis.*DisallowedClassErrorcarrying the name,with Psych's own message, so a host binding can surface it verbatim. No more
degradation to a mapping.
!ruby/object::Symbol(in values and in keys),time.Time,Range(and its bounds),
Regexp, and the class named by a!ruby/class/!ruby/moduletag. Refusing tagged objects while interning Symbols andbuilding Times was half a policy.
WithPermittedSymbolscoverspermitted_symbols:, including MRI'sasymmetry: an empty symbol list narrows nothing
(
Restricted#symbolizeshort-circuits on@symbols.empty?,class_loader.rb:84-92), while an empty class list permits nothing. Narrowingis never a substitute for the class check — MRI's
symbolizestill routesthrough
find("Symbol"), sopermitted_symbolswithoutSymbolinpermitted_classesrefuses every symbol. Measured, not inferred.after the fact — a resolved scalar alias is indistinguishable from a repeated
literal — so the loader now reports alias use, and
SafeLoadapplies thepolicy:
*AliasesNotEnabledErrorwithoutWithAliases(true), and*AnchorNotDefinedErrorfor an alias whose anchor the document never defines(which previously resolved to a silent
nil).Verification
bundled Psych, not inferred from the docs.
-race(the repo's gate).go vetclean,gofmt -lempty,GOOS=wasip1andGOOS=js(GOARCH=wasm)both build.
Three existing tests asserted the old behaviour and are replaced by the
positive invariant, because they were assertions of the defect — fixing the
library turned them red, which is the point:
TestSafeLoadrequired an unpermitted object to come back as a*Map(
"unpermitted Secret should be a Map"), and required a missing allow-list tokeep every object (
"no allow-list should keep objects").TestSafeLoadRangeRestrictrequired a range bound to be degraded.The 100% gate also named one branch this change introduced as unreachable — a
nilcheck insideWithPermittedClasses— and it was:resolveestablishesthe invariant before any
Optionruns. It is gone rather than registered,since an unreachable "just in case"
nilis how this defect existed in thefirst place.
Paired PR
Paired with go-embedded-ruby/ruby#786 (rbgo's
YAML.safe_load, fixing go-embedded-ruby/ruby#775).rbgo does not need a tagged release of this library to be fixed: its PR
loads with
Loadand enforces Psych's restriction in its own Psych binding,where the Psych-specific semantics belong. Once this merges and is tagged, that
binding can delegate here and drop its copy of the walk — a clean follow-up,
not a blocker.
🤖 Generated with Claude Code