This repository has been archived by the owner on Mar 4, 2022. It is now read-only.
forked from Shopify/erb_lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
erb_safety.rb
65 lines (55 loc) · 1.58 KB
/
erb_safety.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: true
require 'better_html'
require 'better_html/test_helper/safe_erb_tester'
module ERBLint
module Linters
# Detect unsafe ruby interpolations into javascript.
class ErbSafety < Linter
include LinterRegistry
class ConfigSchema < LinterConfig
property :better_html_config, accepts: String
end
self.config_schema = ConfigSchema
def initialize(file_loader, config)
super
@config_filename = @config.better_html_config
end
def run(processed_source)
testers_for(processed_source.parser).each do |tester|
tester.validate
tester.errors.each do |error|
add_offense(
error.location,
error.message
)
end
end
end
private
def tester_classes
[
BetterHtml::TestHelper::SafeErb::NoStatements,
BetterHtml::TestHelper::SafeErb::AllowedScriptType,
BetterHtml::TestHelper::SafeErb::TagInterpolation,
BetterHtml::TestHelper::SafeErb::ScriptInterpolation,
]
end
def testers_for(parser)
tester_classes.map do |tester_klass|
tester_klass.new(parser, config: better_html_config)
end
end
def better_html_config
@better_html_config ||= begin
config_hash =
if @config_filename.nil?
{}
else
@file_loader.yaml(@config_filename).symbolize_keys
end
BetterHtml::Config.new(**config_hash)
end
end
end
end
end