waiting-for-dev/front_matter_parser

Failures when front matter contains dates

cjavilla-stripe opened this issue ยท 4 comments

When I try to parse a doc that looks like this:

---
title: Another test
date: 2022-06-04
---

Test

It fails with:

Psych::DisallowedClass in PostsController#index
Tried to load unspecified class: Date

Full trace:

[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/class_loader.rb:97:in `find'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/class_loader.rb:28:in `load'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/class_loader.rb:39:in `block (2 levels) in <class:ClassLoader>'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/scalar_scanner.rb:60:in `tokenize'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:60:in `deserialize'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:123:in `visit_Psych_Nodes_Scalar'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:16:in `visit'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:6:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:32:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:340:in `block in revive_hash'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:338:in `each'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:338:in `each_slice'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:338:in `revive_hash'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:162:in `visit_Psych_Nodes_Mapping'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:16:in `visit'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:6:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:32:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:313:in `visit_Psych_Nodes_Document'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:16:in `visit'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/visitor.rb:6:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:32:in `accept'](http://localhost:3000/posts/#)
[/Users/cjavilla/.rbenv/versions/2.7.2/lib/ruby/2.7.0/psych.rb:360:in `safe_load'](http://localhost:3000/posts/#)
[front_matter_parser (1.0.1) lib/front_matter_parser/loader/yaml.rb:22:in `call'](http://localhost:3000/posts/#)
[front_matter_parser (1.0.1) lib/front_matter_parser/parser.rb:67:in `call'](http://localhost:3000/posts/#)
[front_matter_parser (1.0.1) lib/front_matter_parser/parser.rb:25:in `block in parse_file'](http://localhost:3000/posts/#)
[front_matter_parser (1.0.1) lib/front_matter_parser/parser.rb:24:in `open'](http://localhost:3000/posts/#)
[front_matter_parser (1.0.1) lib/front_matter_parser/parser.rb:24:in `parse_file'](http://localhost:3000/posts/#)
[app/controllers/posts_controller.rb:8:in `block in index'](http://localhost:3000/posts/#)
[app/controllers/posts_controller.rb:7:in `map'](http://localhost:3000/posts/#)
[app/controllers/posts_controller.rb:7:in `index'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/abstract_controller/base.rb:220:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/rendering.rb:53:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/abstract_controller/callbacks.rb:249:in `block in process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/callbacks.rb:118:in `block in run_callbacks'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actiontext/lib/action_text/rendering.rb:20:in `with_renderer'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actiontext/lib/action_text/engine.rb:71:in `block (4 levels) in <class:Engine>'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/callbacks.rb:127:in `instance_exec'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/callbacks.rb:127:in `block in run_callbacks'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/callbacks.rb:138:in `run_callbacks'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/abstract_controller/callbacks.rb:248:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/rescue.rb:22:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/instrumentation.rb:67:in `block in process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/notifications.rb:206:in `block in instrument'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/notifications/instrumenter.rb:58:in `instrument'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/notifications.rb:206:in `instrument'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/instrumentation.rb:66:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal/params_wrapper.rb:259:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activerecord/lib/active_record/railties/controller_runtime.rb:27:in `process_action'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/abstract_controller/base.rb:156:in `process'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionview/lib/action_view/rendering.rb:39:in `process'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal.rb:204:in `dispatch'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_controller/metal.rb:267:in `dispatch'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/routing/route_set.rb:49:in `dispatch'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/routing/route_set.rb:32:in `serve'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/journey/router.rb:50:in `block in serve'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/journey/router.rb:32:in `each'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/journey/router.rb:32:in `serve'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/routing/route_set.rb:852:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/tempfile_reaper.rb:15:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/etag.rb:27:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/conditional_get.rb:27:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/head.rb:12:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/http/permissions_policy.rb:38:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/http/content_security_policy.rb:36:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/session/abstract/id.rb:266:in `context'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/session/abstract/id.rb:260:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/cookies.rb:698:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activerecord/lib/active_record/migration.rb:612:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/callbacks.rb:99:in `run_callbacks'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/callbacks.rb:26:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/executor.rb:14:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:28:in `call'](http://localhost:3000/posts/#)
[web-console (4.2.0) lib/web_console/middleware.rb:132:in `call_app'](http://localhost:3000/posts/#)
[web-console (4.2.0) lib/web_console/middleware.rb:28:in `block in call'](http://localhost:3000/posts/#)
[web-console (4.2.0) lib/web_console/middleware.rb:17:in `catch'](http://localhost:3000/posts/#)
[web-console (4.2.0) lib/web_console/middleware.rb:17:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/show_exceptions.rb:26:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) railties/lib/rails/rack/logger.rb:37:in `call_app'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) railties/lib/rails/rack/logger.rb:24:in `block in call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/tagged_logging.rb:114:in `block in tagged'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/tagged_logging.rb:38:in `tagged'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/tagged_logging.rb:114:in `tagged'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) railties/lib/rails/rack/logger.rb:24:in `call'](http://localhost:3000/posts/#)
[sprockets-rails (3.4.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/remote_ip.rb:92:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/request_id.rb:26:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/method_override.rb:24:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/runtime.rb:22:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/server_timing.rb:20:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/executor.rb:14:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/static.rb:23:in `call'](http://localhost:3000/posts/#)
[rack (2.2.4) lib/rack/sendfile.rb:110:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) actionpack/lib/action_dispatch/middleware/host_authorization.rb:138:in `call'](http://localhost:3000/posts/#)
[rails (a3475f07ccb0) railties/lib/rails/engine.rb:529:in `call'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/configuration.rb:252:in `call'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/request.rb:77:in `block in handle_request'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/thread_pool.rb:340:in `with_force_shutdown'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/request.rb:76:in `handle_request'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/server.rb:441:in `process_client'](http://localhost:3000/posts/#)
[puma (5.6.4) lib/puma/thread_pool.rb:147:in `block in spawn_thread'](http://localhost:3000/posts/#)

This is my PostsController:

class PostsController < ApplicationController

  def index
    files = Dir.glob(File.join(Rails.root, "posts", "*.md"))
    @posts = files.map do |f|
      parsed = FrontMatterParser::Parser.parse_file(f)
      Post.new(f, parsed.front_matter)
    end
  end

My goal is to iterate over my blog posts, authored in markdown with front matter that contains title and date and render a simple list. My bet is we need to pass some permitted classes to the yaml loader?

This conversation seems relevant: ruby/psych#262

Solved!

I had to new up a Loader and pass it into parse_file like this:

class PostsController < ApplicationController

  def index
    files = Dir.glob(File.join(Rails.root, "posts", "*.md"))
    @posts = files.map do |f|
      parsed = FrontMatterParser::Parser.parse_file(
        f,
        loader: FrontMatterParser::Loader::Yaml.new(allowlist_classes: [Date])
      )
      Post.new(f, parsed.front_matter)
    end
  end

That's it ๐Ÿ™‚

@waiting-for-dev Would it make sense to update this gem to include Date as allowed by default?

It is very common to have a date in the front matter. It would be nice if it was accepted without raising exceptions.

Thanks for your feedback, @collimarco. I think it makes sense to have it as a default.