A hosted scan splits a `gem` option whose value is an `if` or `unless` expression (`require: if … end`): `success`, then the Gemfile no longer parses and every `bundle` command fails

Offen Anfängerfreundlich
#2,517 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
76/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
ruby, rust
Bereich
backend, cli

Rechercherichtung

Lies crates/socket-patch-core/src/formats/gem/declarations.rs, insbesondere die Argumentprüfung um die Zeilen 262–345, sowie den Abschnitt „Gem hosted wiring“ in CLI_CONTRACT.md. Beginne mit dem im Issue gezeigten Unit-Test und ergänze anschließend, sofern es die Zeit erlaubt, den abschließenden Fall in tests/e2e_redirect_gem_build.rs. Als fertig gilt die Änderung, wenn Optionswerte, die mit bedingten Ausdrücken beginnen, sicher zurückgewiesen werden, sodass das Gemfile unverändert und installierbar bleibt; führe den gezielten Core-Test und den abschließenden Test aus.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

agent:triaged arch-audit bug priority:p1

[agent] Filed by the scheduled architecture refactor routine. Found by running the hosted Gemfile rewriter over 13,300 generated Gemfiles and asking real Bundler (4.0.18 and 2.5.22) what each result means. main at cd7c5c5.

Problem

scan --mode hosted gives a patched gem the Socket source by appending , source: "<url>" after the last argument of the project's own gem declaration. To find where the arguments end, the reader stops at a trailing if / unless / while / until / rescue, which it takes for a statement modifier (gem "rack" if ENV["X"]).

When that word is the value of an option, it is an expression and not a modifier:

gem "rack", "2.2.3", require: if ENV["RACK_EXT"] then "rack/ext" else "rack" end

The reader ends the arguments after require:, and the option is written between the key and its value:

gem "rack", "2.2.3", require:, source: "https://…/" if ENV["RACK_EXT"] then "rack/ext" else "rack" end

The scan exits 0 with status: success and applied: 1. The Gemfile is no longer Ruby, so every bundle command fails until the file is fixed by hand or rolled back.

The same if as a positional argument (gem "rack", if … end) is refused today with redirect_gem_unrecognized_declaration. So are case … end and begin … end in either position. The option-value position is the one that is missed.

Evidence on main

Through the binary, with real Bundler 4.0.18. The hosted gem capstone (e2e_redirect_gem_build) with one line changed: the Gemfile of gem_hosted_conditional_declaration_redirects_in_place_and_installs declares the gem with the option above instead of a trailing modifier, and the fixture prints what it sees before its own comparison.

scan exit 0, status "success", summary {"applied":1,"skipped":0,"failed":0,…}, warnings ["redirect_gem_no_checksums_section"]

Gemfile before:
gem "vuln-gem", require: if ENV["WITH_VULN"] != "0" then "vuln_gem" else false end

Gemfile after:
gem "vuln-gem", require:, source: "http://127.0.0.1:43501/patch-registry/gem/<token>/<uuid>/" if ENV["WITH_VULN"] != "0" then "vuln_gem" else false end

bundle install ok: false
[!] There was an error parsing `Gemfile`: syntax error, unexpected `then', expecting end-of-input - . Bundler cannot continue.

socket-patch rollback pkg:gem/[email protected] exits 0 and returns the Gemfile and the lock byte for byte, so that is the way out today. Vendored mode refuses each of these lines ("conditional declaration").

Which spellings. The rewriter (rewrite_registry_redirect, the function the scan calls) over hand-written declarations, each result loaded by Bundler::Dsl:

Declaration Result Bundler on the result
gem "rack", "2.2.3", require: if ENV["X"] then "rack/x" else "rack" end rewritten syntax error
gem "rack", "2.2.3", require: unless ENV["X"] then false end rewritten syntax error
gem "rack", "2.2.3", :require => if ENV["X"] then "rack/x" else "rack" end rewritten syntax error (:require =>, source: …)
gem "rack", "2.2.3", platforms: if RUBY_ENGINE == "jruby" then [:jruby] else [:ruby] end rewritten syntax error
the first row with the if body on lines of its own (require: if ENV["X"] ↵ "rack/x" ↵ else ↵ "rack" ↵ end) rewritten syntax error
the first row after a line break (gem "rack", "2.2.3", ↵ require: if … end) rewritten syntax error
gem "rack", "2.2.3", require: (if true then false end) rewritten, option after the ) loads, same dependency
gem "rack", "2.2.3", require: ENV["X"] ? "a" : "b" rewritten loads, same dependency
gem "rack", "2.2.3", require: !ENV["X"] if true (a real modifier) rewritten, option before the if loads, same dependency
gem "rack", if ENV["RACK"] then ENV["RACK"] else "2.2.3" end refused, redirect_gem_unrecognized_declaration —
gem "rack", case RUBY_ENGINE when "jruby" then "2.2.3" else "2.2.3" end refused —
gem "rack", "2.2.3", require: begin; false; end refused —

How wide. 13,300 generated Gemfiles (three gems; the patched one in many spellings: quote styles, parentheses, continuation lines, modifiers, ;, blocks, heredoc and =begin decoys, CRLF; locks with and without CHECKSUMS). 10,160 were rewritten and 3,140 refused. Of the rewritten ones, 76 no longer parse, and all 76 carry an if expression as an option value. The other 10,084 load under Bundler with every other dependency unchanged and the gem on the Socket source, and pass Bundler's own frozen check (Definition#ensure_equivalent_gemfile_and_lockfile). A second run over each output changed nothing.

A unit test next to the reader (formats/gem/declarations.rs, using the module's own one and with_source_option):

#[test]
fn a_keyword_that_starts_an_option_value_is_no_modifier() {
    // Control: the positional form is refused today.
    assert!(one("gem \"rack\", if ENV[\"X\"] then \"1\" else \"2\" end\n").problem.is_some());
    let mut edited = Vec::new();
    for text in [
        "gem \"rack\", require: if ENV[\"X\"] then \"rack/x\" else \"rack\" end\n",
        "gem \"rack\", \"2.2.3\", :require => if ENV[\"X\"] then \"rack/x\" else \"rack\" end\n",
        "gem \"rack\", \"2.2.3\", require: unless ENV[\"X\"] then false end\n",
        "gem \"rack\", \"2.2.3\", require: if ENV[\"X\"]\n  \"rack/x\"\nelse\n  \"rack\"\nend\n",
    ] {
        let decl = one(text);
        if decl.problem.is_none() {
            edited.push(with_source_option(text, &decl, "URL"));
        }
    }
    assert!(edited.is_empty(), "edited in the middle of an option:\n{}", edited.join(""));
}
$ cargo test -p socket-patch-core --lib a_keyword_that_starts_an_option_value
edited in the middle of an option:
gem "rack", require:, source: "URL" if ENV["X"] then "rack/x" else "rack" end
gem "rack", "2.2.3", :require =>, source: "URL" if ENV["X"] then "rack/x" else "rack" end
gem "rack", "2.2.3", require:, source: "URL" unless ENV["X"] then false end
gem "rack", "2.2.3", require:, source: "URL" if ENV["X"]
  "rack/x"
else
  "rack"
end
test result: FAILED. 0 passed; 1 failed

Where

  • The rule: [formats/gem/declarations.rs#L319-L345](https://github.com/SocketDev/socket-patch/blob/cd7c5c56e3e0049a5c06260c354a597b8f07658b/crates/socket-patch-core/src/formats/gem/declarations.rs#L319-L345). complete is true once the current argument has any text, so require: counts as a complete argument and the if after it ends the call.
  • Thirty lines up, the same reader knows that a label or an operator leaves an expression open, for a line break: [#L262-L279](https://github.com/SocketDev/socket-patch/blob/cd7c5c56e3e0049a5c06260c354a597b8f07658b/crates/socket-patch-core/src/formats/gem/declarations.rs#L262-L279) ("its last line ends in the middle of an expression").
  • The contract: CLI_CONTRACT.md, "Gem hosted wiring" (the option goes "after its last argument", "a trailing if / unless / while / until / rescue modifier (the option goes before the modifier)").

Why it is in the charter

  • A mechanism bug users can see, and do no harm. A successful scan leaves a Gemfile that no bundle command can read.
  • Structure that keeps causing bugs. #340 was this class (a hosted rewrite that broke a multi-line declaration and dropped a trailing modifier). Its fix, and the reader that replaced it in #2052, tell a modifier from a hash label (if::ENV, if:), refuse the positional if, and refuse a line that ends on a label. The value position on the same line was left out.

Planned fix

In the word branch of the argument scan, a modifier word counts as a modifier only when the argument before it is a complete expression. When the argument's text so far ends in a label (key:, "key":), a => or one of the operators the line-break branch already lists, the word starts a value: record a problem ("an if expression is among its arguments") and stop, so the declaration is refused with redirect_gem_unrecognized_declaration like the positional form. The label and operator test is shared with the line-break branch. About 15 production lines in one function. Tests: the unit test above, and a capstone row beside the conditional one (refused, nothing written, the project still installs).

Tried on a build (21 lines in declarations.rs, not pushed). Three of the batches again (13,000 Gemfiles) and the hand-written rows: no result fails to parse. 79 results changed from rewritten to refused, every one a declaration with an if expression as an option value; 6 more were refused before and are refused under this code now; nothing else changed. The unit test above passes, and so do the core crate's 718 other tests with gem in their name.

No contract change: the refusal code and its wording exist, and docs/ecosystems.md lists the declarations it covers; one more example there.

Not opened as a PR now: three of this routine's PRs await review.

Not run

  • get <uuid> --mode hosted (the same engine), gems.rb, Bundler 1.17 to 2.4 and 2.6 to 2.7.
  • while / until as an option value, which no Gemfile would hold.
The fixtures

The generator (Ruby, seeded). A source line in five spellings; constants a declaration may use; zero to two decoys (a commented gem "rack" line, a =begin block, two heredocs, a regex, %q{…}, a string); then rack, rake and puma in random order. Each declaration: the name in nine spellings, zero to two requirements in twenty spellings, options in about thirty (among them require: if true then false else true end), commas with or without line breaks, comments and \, a call with or without parentheses, a dozen endings (comments, modifiers, ;), and one of seventeen places (top level, group / platforms / source / install_if blocks, if / unless / case / begin, one-line do … end and { … } blocks). CRLF in one file of twelve. Fifteen in a hundred leave rack undeclared, with puma depending on it in the lock.

The lock is a template (GEM with the three specs, PLATFORMS, BUNDLED WITH 2.5.22, or with CHECKSUMS and 2.6.2) whose DEPENDENCIES are Bundler::Dependency#to_lock of what Bundler::Dsl reads from the Gemfile. A Gemfile Bundler does not load is dropped.

The rewriter runs from an example in the core crate (rewrite_registry_redirect is public): the Gemfile, the Gemfile.lock and one rubygems-compact-index override for rack 2.2.3 in, the changed files and the warnings out.

The oracle (one Ruby process per batch, gem "bundler", "<version>"): Bundler::Dsl#eval_gemfile on the input and on the output, each dependency's name, requirement, source remotes, groups, platforms and autorequire compared; the patched gem's source must be the Socket index; Bundler::Definition.build(…).ensure_equivalent_gemfile_and_lockfile(true) under BUNDLE_FROZEN=true must accept the input pair (else the case is skipped) and the output pair.

The capstone row. In tests/e2e_redirect_gem_build.rs, the Gemfile of Driver::ScanVexConditionalDeclaration becomes gem "{DEP}", require: if ENV["WITH_VULN"] != "0" then "vuln_gem" else false end; before the fixture compares the Gemfile it prints the envelope, runs bundle install and the suite's rollback. cargo test -p socket-patch-cli --test e2e_redirect_gem_build -- --ignored gem_hosted_conditional_declaration_redirects_in_place_and_installs.


Generated by Claude Code

Vorherrschende Sprache
Rust
Sterne
9
Forks
2
Ø Merge
1 T. 2 Std.
Gemergte PRs (30 T.)
213

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus SocketDev/socket-patch

Alle Issues in SocketDev/socket-patch

Ähnliche Issues

Weitere Issues zu Rust

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.