/app/controllers/help_controller.rb

https://gitlab.com/artofhuman/gitlab-ce · Ruby · 108 lines · 62 code · 25 blank · 21 comment · 9 complexity · efe8277df2e015fd2f4e7d8f7297c5ad MD5 · raw file

  1. # frozen_string_literal: true
  2. class HelpController < ApplicationController
  3. skip_before_action :authenticate_user!
  4. layout 'help'
  5. # Taken from Jekyll
  6. # https://github.com/jekyll/jekyll/blob/3.5-stable/lib/jekyll/document.rb#L13
  7. YAML_FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
  8. def index
  9. # Remove YAML frontmatter so that it doesn't look weird
  10. @help_index = File.read(Rails.root.join('doc', 'README.md')).sub(YAML_FRONT_MATTER_REGEXP, '')
  11. # Prefix Markdown links with `help/` unless they are external links
  12. # See http://rubular.com/r/X3baHTbPO2
  13. @help_index.gsub!(%r{(?<delim>\]\()(?!.+://)(?!/)(?<link>[^\)\(]+\))}) do
  14. "#{$~[:delim]}#{Gitlab.config.gitlab.relative_url_root}/help/#{$~[:link]}"
  15. end
  16. end
  17. def show
  18. @path = clean_path_info(path_params[:path])
  19. respond_to do |format|
  20. format.any(:markdown, :md, :html) do
  21. # Note: We are purposefully NOT using `Rails.root.join`
  22. path = File.join(Rails.root, 'doc', "#{@path}.md")
  23. if File.exist?(path)
  24. # Remove YAML frontmatter so that it doesn't look weird
  25. @markdown = File.read(path).gsub(YAML_FRONT_MATTER_REGEXP, '')
  26. render 'show.html.haml'
  27. else
  28. # Force template to Haml
  29. render 'errors/not_found.html.haml', layout: 'errors', status: 404
  30. end
  31. end
  32. # Allow access to images in the doc folder
  33. format.any(:png, :gif, :jpeg, :mp4) do
  34. # Note: We are purposefully NOT using `Rails.root.join`
  35. path = File.join(Rails.root, 'doc', "#{@path}.#{params[:format]}")
  36. if File.exist?(path)
  37. send_file(path, disposition: 'inline')
  38. else
  39. head :not_found
  40. end
  41. end
  42. # Any other format we don't recognize, just respond 404
  43. format.any { head :not_found }
  44. end
  45. end
  46. def shortcuts
  47. end
  48. def instance_configuration
  49. @instance_configuration = InstanceConfiguration.new
  50. end
  51. def ui
  52. @user = User.new(id: 0, name: 'John Doe', username: '@johndoe')
  53. end
  54. private
  55. def path_params
  56. params.require(:path)
  57. params
  58. end
  59. PATH_SEPS = Regexp.union(*[::File::SEPARATOR, ::File::ALT_SEPARATOR].compact)
  60. # Taken from ActionDispatch::FileHandler
  61. # Cleans up the path, to prevent directory traversal outside the doc folder.
  62. def clean_path_info(path_info)
  63. parts = path_info.split(PATH_SEPS)
  64. clean = []
  65. # Walk over each part of the path
  66. parts.each do |part|
  67. # Turn `one//two` or `one/./two` into `one/two`.
  68. next if part.empty? || part == '.'
  69. if part == '..'
  70. # Turn `one/two/../` into `one`
  71. clean.pop
  72. else
  73. # Add simple folder names to the clean path.
  74. clean << part
  75. end
  76. end
  77. # If the path was an absolute path (i.e. `/` or `/one/two`),
  78. # add `/` to the front of the clean path.
  79. clean.unshift '/' if parts.empty? || parts.first.empty?
  80. # Join all the clean path parts by the path separator.
  81. ::File.join(*clean)
  82. end
  83. end