From 8551738f5b213901c6ebd2b2b2e716f587bf232c Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Sun, 15 Nov 2026 09:38:14 +0800 Subject: [PATCH] Refuse catalog YAML that safe loading rejects as an invalid document Catalog.parse rescued only Psych::SyntaxError. YAML.safe_load refuses a tag naming a class outside the permitted list (Psych::DisallowedClass) and an alias to an undefined anchor (Psych::BadAlias) with errors that are not syntax errors, so a document such as `--- !ruby/object:Object {}` escaped ScenarioCatalogImport as a 500 from the catalog import and preview endpoints, and as an internal error from the catalogs_import and catalogs_preview MCP tools. The rescue is now Psych::Exception, their common parent, as ScenarioParser and the CLI credentials reader already do, so these documents get the same "the catalog is not valid YAML" 422 as a syntax error. The permitted classes and aliases are unchanged. --- CHANGELOG.md | 14 +++++++++ actionagent/test/mcp_dashboard_tools_test.rb | 6 ++++ .../test/scenario_catalogs_api_test.rb | 13 ++++++++ lib/active_agent/evals/catalog.rb | 7 +++-- test/evals/catalog_test.rb | 31 +++++++++++++++++++ 5 files changed, 69 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50e736f6..69a52ecf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- **A catalog document YAML refuses to load is an invalid document** + (`activeagent`, `actionagent`). `Catalog.parse` turned only a syntax + error into `Catalog::InvalidDocument`, while safe loading refuses a tag + naming a Ruby class (`!ruby/object:...`) or an alias to an undefined + anchor with other `Psych` errors. Those escaped the dashboard's catalog + import and preview as a 500, and the `catalogs_import` and + `catalogs_preview` tools as an internal error. They are now refused like + any other invalid catalog: 422 with "the catalog is not valid YAML: ...". + What safe loading permits is unchanged. + ## [1.9.8] - 2026-11-12 The dashboard holds a run to its catalog's pass bars: a product's and a diff --git a/actionagent/test/mcp_dashboard_tools_test.rb b/actionagent/test/mcp_dashboard_tools_test.rb index c65e5829..07870d43 100644 --- a/actionagent/test/mcp_dashboard_tools_test.rb +++ b/actionagent/test/mcp_dashboard_tools_test.rb @@ -535,6 +535,12 @@ def structured(body) assert_equal true, body.dig("result", "isError") assert_match(/product has no key/, body.dig("result", "content", 0, "text")) assert_equal(-32602, call_tool("catalogs_import").dig("error", "code")) + + %w[catalogs_import catalogs_preview].each do |tool| + body = call_tool(tool, { document: "--- !ruby/object:Object {}\n" }) + assert_equal true, body.dig("result", "isError"), "#{tool} answers a refused document as a tool error" + assert_match(/not valid YAML/, body.dig("result", "content", 0, "text")) + end end test "catalog_set_run materializes the set as an evaluation and queues its run" do diff --git a/actionagent/test/scenario_catalogs_api_test.rb b/actionagent/test/scenario_catalogs_api_test.rb index ae210c79..69ddeab0 100644 --- a/actionagent/test/scenario_catalogs_api_test.rb +++ b/actionagent/test/scenario_catalogs_api_test.rb @@ -163,6 +163,19 @@ def body assert_match(/product has no key/, body["errors"].first) end + test "a document naming a Ruby class is refused as invalid YAML, on import and on preview" do + document = "--- !ruby/object:Object {}\n" + + post "/activeagents/api/scenario_catalogs", params: { document: document }, as: :json + assert_response :unprocessable_entity + assert_match(/not valid YAML: Tried to load unspecified class: Object/, body["errors"].first) + + post "/activeagents/api/scenario_catalogs/preview", params: { document: document }, as: :json + assert_response :unprocessable_entity + assert_match(/not valid YAML/, body["errors"].first) + assert_equal 0, ActionAgent::ScenarioCatalog.count + end + test "imports every catalog file of a connected repository at a ref" do ActionAgent::GithubConnection.create!( access_token: "gho_test", github_user_id: 42, login: "octocat", diff --git a/lib/active_agent/evals/catalog.rb b/lib/active_agent/evals/catalog.rb index 4c14ba46..bb0a9a3c 100644 --- a/lib/active_agent/evals/catalog.rb +++ b/lib/active_agent/evals/catalog.rb @@ -81,13 +81,16 @@ def self.load(*paths, name: nil) end # Parses YAML text. Raises InvalidDocument for YAML that is not a - # document (a list, a scalar) or that YAML cannot read. + # document (a list, a scalar) or that YAML cannot read: a syntax error, + # and what safe loading refuses — a tag naming a Ruby class other than + # Symbol (Psych::DisallowedClass) or an alias to an undefined anchor + # (Psych::BadAlias), which are not Psych::SyntaxError. def self.parse(text, name: nil) document = YAML.safe_load(text.to_s, aliases: true, permitted_classes: [ Symbol ]) raise InvalidDocument, "a scenario catalog is a YAML mapping" unless document.is_a?(Hash) new([ document ], name: name) - rescue Psych::SyntaxError => e + rescue Psych::Exception => e raise InvalidDocument, "the catalog is not valid YAML: #{e.message}" end diff --git a/test/evals/catalog_test.rb b/test/evals/catalog_test.rb index 7648f6d2..59a55caa 100644 --- a/test/evals/catalog_test.rb +++ b/test/evals/catalog_test.rb @@ -351,6 +351,37 @@ def gated_catalog(*overlays) assert_raises(ActiveAgent::Evals::Catalog::NotFound) { ActiveAgent::Evals::Catalog.load(File.join(FIXTURES, "missing.yml")) } end + # The document is read with YAML.safe_load, which refuses a tag naming a + # Ruby class and an alias to an anchor the document never defines with + # errors of its own, apart from Psych::SyntaxError. + test "refuses YAML that safe loading will not read, as an invalid document" do + { + "--- !ruby/object:Object {}\n" => /Tried to load unspecified class: Object/, + "catalog: x\nproducts: [{ key: p, at: !ruby/object:OpenStruct { a: 1 } }]\n" => /unspecified class: OpenStruct/, + "catalog: x\nproducts: *undefined\n" => /undefined/ + }.each do |document, reason| + error = assert_raises(ActiveAgent::Evals::Catalog::InvalidDocument, document) { ActiveAgent::Evals::Catalog.parse(document) } + assert_match(/\Athe catalog is not valid YAML: /, error.message) + assert_match reason, error.message + end + end + + test "keeps reading the symbols and aliases a catalog may use" do + catalog = ActiveAgent::Evals::Catalog.parse(<<~YAML) + catalog: x + products: + - key: p + judge: :rules + sets: + - key: s + scenarios: + - &first { key: a, prompt: Which tickets are open? } + - *first + YAML + + assert_equal [ "a" ], catalog.scenarios.map(&:key).uniq + end + test "refuses malformed criteria, naming the product or set and the list" do refused = { "catalog: x\nproducts: [{ key: p, criteria: oops }]" => /criteria of product "p" are not a list or a mapping of lists/,