Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the way autoload paths are added to application.rb #205

Conversation

trinitytakei
Copy link
Contributor

The Problem

As described here: https://discord.com/channels/1082611227827638303/1082612756676620318/1255932550837309573 brakeman fails to parse config.application.rb after the phlex installer adds the extra autoload paths. As a result, it mistakenly reports a "Cross-Site Request Forgery" issue, and thus the scan_ruby job in ci.yml fails. (brakeman and ci.yml with a scan_ruby job are Omakase Rails since 7.2.beta).

The Proposed Solution

After some debugging, I figured out the code to be inserted into config/application.rb that is

  • functionally equivalent to the original
  • can be parsed properly by brakeman (and thus there's no mistaken "Cross-Site Request Forgery" report)

🤔

This is not a phlex-rails issue per se (the original code generated by phlex:install is not 'less valid' or worse in any way than the proposed code). However, I believe the fix still makes sense, because it's a simple one that works right now, won't cause any trouble down the line, and will hopefully save some headache for Phlex + brakeman users.

The proper/long term fix of course is to handle this in brakeman. I consulted with a security expert in the know, and he confirmed that brakeman's current parser is indeed not great; Prism to the rescue (at some point in the future).

"\#{root}/app/views/layouts"
)

ADD_EXTRA_AUTOLOAD_PATHS_CODE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think this line does anything, seems to be leftover from debugging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does: closes the heredoc from line 9 ;)

Probably three occurrences of ADD_EXTRA_AUTOLOAD_PATHS_CODE and the extra empty line (on line 15) was a bit disorienting.

Not sure what's the deal with using the same string 3x (yay/nay/what's the best practice).

As for the extra line. I prefer the extra line - it's the difference between

    config.autoload_paths.push(
      "#{root}/app/views/components",
      "#{root}/app/views",
      "#{root}/app/views/layouts"
    )
    config.application_name = "railsnew.io" # look ma, no empty line above me

and

    config.autoload_paths.push(
      "#{root}/app/views/components",
      "#{root}/app/views",
      "#{root}/app/views/layouts"
    )
    
    config.application_name = "railsnew.io" # 👌👌👌

While I prefer the second version (I'm a sucker for little things like this), in this case I don't mind either way as application.rb is not a file anyone is frequently working with.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it does: closes the heredoc from line 9 ;)

🤦 Oh of course! I thought it was a void call to the constant.

The extra line makes sense too.

Copy link
Collaborator

@joeldrapper joeldrapper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great apart from the one issue on line 16.

@trinitytakei trinitytakei force-pushed the adding-autoload_paths-should-not-trip-up-brakeman branch from 1a6802b to 3363cbf Compare August 14, 2024 10:28
@trinitytakei trinitytakei force-pushed the adding-autoload_paths-should-not-trip-up-brakeman branch from 3363cbf to d0531f9 Compare August 14, 2024 11:34
@trinitytakei
Copy link
Contributor Author

Just pushed rubocop fixes (I'm normally adding those with git commit -v -a --no-edit --amend - let me know if you prefer to have an explicit commit (and probably squash during merging, unless you are keen on keeping the history)).

@trinitytakei
Copy link
Contributor Author

So NOW it is weird. Rubocop 'fixed' this:

		ADD_EXTRA_AUTOLOAD_PATHS_CODE = <<-ADD_EXTRA_AUTOLOAD_PATHS_CODE
    config.autoload_paths.push(
      "\#{root}/app/views/components",
      "\#{root}/app/views",
      "\#{root}/app/views/layouts"
    )

				ADD_EXTRA_AUTOLOAD_PATHS_CODE

No idea why the weird indentation?

@joeldrapper
Copy link
Collaborator

Sometimes Rubocop does weird auto-correct things with indentation. What do you think about this indentation?

Using <<~ so Ruby strips the leading whitespace. I believe this will only strip the tabs. Additionally, using RUBY as the delineator means some editors will highlight it as Ruby.

CleanShot 2024-08-14 at 12 48 24@2x

@joeldrapper
Copy link
Collaborator

joeldrapper commented Aug 14, 2024

Oh no, I’m wrong. It strips the spaces too. 😢

@joeldrapper
Copy link
Collaborator

Don’t try too hard to please Rubocop. If Rubocop is wrong, we’ll change the Rubocop configuration.

@trinitytakei
Copy link
Contributor Author

Using <<~ so Ruby strips the leading whitespace.

Yeah, I toyed with that too, but between Rubocop and how I wanted it to look like, I culdn't figure it out.

Additionally, using RUBY as the delineator means some editors will highlight it as Ruby.

Sounds good. Heredoc syntax highlighting is underrated imo: https://mhenrixon.com/articles/heredoc-syntax-highlighting

@joeldrapper
Copy link
Collaborator

Good to merge after the heredoc update. 👍

@trinitytakei
Copy link
Contributor Author

@joeldrapper Sorry if I'm missing something, but is anything required of me at this point (CI is 🟢)?

@joeldrapper joeldrapper merged commit 51fd83a into phlex-ruby:main Aug 30, 2024
13 checks passed
@joeldrapper
Copy link
Collaborator

All good and merged. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants