• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wpscanteam / CMSScanner / 10771931262

09 Sep 2024 11:06AM UTC coverage: 99.925%. Remained the same
10771931262

push

github

web-flow
Merge pull request #261 from wpscanteam/dependabot/bundler/webmock-tw-3.23.1

Update webmock requirement from ~> 3.19.1 to ~> 3.23.1

315 of 346 branches covered (91.04%)

1333 of 1334 relevant lines covered (99.93%)

120.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.25
/lib/cms_scanner/formatter.rb
1
# frozen_string_literal: true
2

3
require 'cms_scanner/formatter/buffer'
4✔
4

5
module CMSScanner
4✔
6
  # Formatter
7
  module Formatter
4✔
8
    # Module to be able to do Formatter.load() & Formatter.availables
9
    # and do that as well when the Formatter is included in another module
10
    module ClassMethods
4✔
11
      # @param [ String ] format
12
      # @param [ Array<String> ] custom_views
13
      #
14
      # @return [ Formatter::Base ]
15
      def load(format = nil, custom_views = nil)
4✔
16
        format ||= 'cli'
96✔
17
        custom_views ||= []
96✔
18

19
        f = const_get(format.tr('-', '_').camelize).new
96✔
20
        custom_views.each { |v| f.views_directories << v }
104✔
21
        f
96✔
22
      end
23

24
      # @return [ Array<String> ] The list of the available formatters (except the Base one)
25
      # @note: the #load method above should then be used to create the associated formatter
26
      def availables
4✔
27
        formatters = NS::Formatter.constants.select do |const|
312✔
28
          name = NS::Formatter.const_get(const)
2,808✔
29
          name.is_a?(Class) && name != NS::Formatter::Base
2,808✔
30
        end
31

32
        formatters.map { |sym| sym.to_s.underscore.dasherize }
1,560✔
33
      end
34
    end
35

36
    extend ClassMethods
4✔
37

38
    def self.included(base)
4✔
39
      base.extend(ClassMethods)
4✔
40
    end
41

42
    # This module should be implemented in the code which uses this Framework to
43
    # be able to override/implements instance methods for all the Formatters
44
    # w/o having to include/write the methods in each formatters.
45
    #
46
    # Example: to override the #views_directories (see the wpscan-v3/lib/wpscan/formatter.rb)
47
    module InstanceMethods
4✔
48
    end
49

50
    # Base Formatter
51
    class Base
4✔
52
      attr_reader :controller_name
4✔
53

54
      def initialize
4✔
55
        # Can't put this at the top level of the class, due to the NS::
56
        extend NS::Formatter::InstanceMethods
232✔
57
      end
58

59
      # @return [ String ] The underscored name of the class
60
      def format
4✔
61
        self.class.name.demodulize.underscore
228✔
62
      end
63

64
      # @return [ Boolean ]
65
      def user_interaction?
4✔
66
        format == 'cli'
28✔
67
      end
68

69
      # @return [ String ] The underscored format to use as a base
70
      def base_format; end
4✔
71

72
      # @return [ Array<String> ]
73
      def formats
4✔
74
        [format, base_format].compact
380✔
75
      end
76

77
      # This is called after the scan
78
      # and used in some formatters (e.g JSON)
79
      # to indent results
80
      def beautify; end
4✔
81

82
      # @see #render
83
      def output(tpl, vars = {}, controller_name = nil)
4✔
84
        puts render(tpl, vars, controller_name)
44✔
85
      end
86

87
      ERB_SUPPORTS_KVARGS = ::ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
4✔
88

89
      # @param [ String ] tpl
90
      # @param [ Hash ] vars
91
      # @param [ String ] controller_name
92
      def render(tpl, vars = {}, controller_name = nil)
4✔
93
        template_vars(vars)
160✔
94
        @controller_name = controller_name if controller_name
160✔
95

96
        # '-' is used to disable new lines when -%> is used
97
        # See http://www.ruby-doc.org/stdlib-2.1.1/libdoc/erb/rdoc/ERB.html
98
        # Since ruby 2.6, KVARGS are supported and passing argument is deprecated in ruby 3+
99
        if ERB_SUPPORTS_KVARGS
160✔
100
          ERB.new(File.read(view_path(tpl)), trim_mode: '-').result(binding)
160✔
101
        else
×
102
          ERB.new(File.read(view_path(tpl)), nil, '-').result(binding)
×
103
        end
104
      end
105

106
      # @param [ Hash ] vars
107
      #
108
      # @return [ Void ]
109
      def template_vars(vars)
4✔
110
        vars.each do |key, value|
160✔
111
          instance_variable_set("@#{key}", value) unless key == :views_directories
408✔
112
        end
113
      end
114

115
      # @param [ String ] tpl
116
      #
117
      # @return [ String ] The path of the view
118
      def view_path(tpl)
4✔
119
        if tpl[0, 1] == '@' # Global Template
184✔
120
          tpl = tpl.delete('@')
36✔
121
        else
148✔
122
          raise 'The controller_name can not be nil' unless controller_name
148✔
123

124
          tpl = "#{controller_name}/#{tpl}"
144✔
125
        end
126

127
        raise "Wrong tpl format: '#{tpl}'" unless %r{\A[\w/_]+\z}.match?(tpl)
180✔
128

129
        views_directories.reverse_each do |dir|
176✔
130
          formats.each do |format|
380✔
131
            potential_file = File.join(dir, format, "#{tpl}.erb")
392✔
132

133
            return potential_file if File.exist?(potential_file)
392✔
134
          end
135
        end
136

137
        raise "View not found for #{format}/#{tpl}"
4✔
138
      end
139

140
      # @return [ Array<String> ] The directories to look into for views
141
      def views_directories
4✔
142
        @views_directories ||= [
268✔
143
          APP_DIR, NS::APP_DIR,
144
          File.join(Dir.home, ".#{NS.app_name}"), File.join(Dir.pwd, ".#{NS.app_name}")
145
        ].uniq.reduce([]) { |acc, elem| acc << Pathname.new(elem).join('views').to_s }
416✔
146
      end
147
    end
148
  end
149
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc