Skip to content

Commit e2fffdf

Browse files
committed
more linter fixes
1 parent 9fca5a9 commit e2fffdf

14 files changed

+168
-188
lines changed

lib/secure_headers/headers/clear_site_data.rb

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,41 @@ class ClearSiteData
1111
EXECUTION_CONTEXTS = "executionContexts".freeze
1212
ALL_TYPES = [CACHE, COOKIES, STORAGE, EXECUTION_CONTEXTS]
1313

14-
class << self
15-
# Public: make an clear-site-data header name, value pair
16-
#
17-
# Returns nil if not configured, returns header name and value if configured.
18-
def make_header(config = nil, user_agent = nil)
19-
case config
20-
when nil, OPT_OUT, []
21-
# noop
22-
when Array
23-
[HEADER_NAME, make_header_value(config)]
24-
when true
25-
[HEADER_NAME, make_header_value(ALL_TYPES)]
26-
end
14+
# Public: make an clear-site-data header name, value pair
15+
#
16+
# Returns nil if not configured, returns header name and value if configured.
17+
def self.make_header(config = nil, user_agent = nil)
18+
case config
19+
when nil, OPT_OUT, []
20+
# noop
21+
when Array
22+
[HEADER_NAME, make_header_value(config)]
23+
when true
24+
[HEADER_NAME, make_header_value(ALL_TYPES)]
2725
end
26+
end
2827

29-
def validate_config!(config)
30-
case config
31-
when nil, OPT_OUT, true
32-
# valid
33-
when Array
34-
unless config.all? { |t| t.is_a?(String) }
35-
raise ClearSiteDataConfigError.new("types must be Strings")
36-
end
37-
else
38-
raise ClearSiteDataConfigError.new("config must be an Array of Strings or `true`")
28+
def self.validate_config!(config)
29+
case config
30+
when nil, OPT_OUT, true
31+
# valid
32+
when Array
33+
unless config.all? { |t| t.is_a?(String) }
34+
raise ClearSiteDataConfigError.new("types must be Strings")
3935
end
36+
else
37+
raise ClearSiteDataConfigError.new("config must be an Array of Strings or `true`")
4038
end
39+
end
4140

42-
# Public: Transform a clear-site-data config (an Array of Strings) into a
43-
# String that can be used as the value for the clear-site-data header.
44-
#
45-
# types - An Array of String of types of data to clear.
46-
#
47-
# Returns a String of quoted values that are comma separated.
48-
def make_header_value(types)
49-
types.map { |t| %("#{t}") }.join(", ")
50-
end
41+
# Public: Transform a clear-site-data config (an Array of Strings) into a
42+
# String that can be used as the value for the clear-site-data header.
43+
#
44+
# types - An Array of String of types of data to clear.
45+
#
46+
# Returns a String of quoted values that are comma separated.
47+
def self.make_header_value(types)
48+
types.map { |t| %("#{t}") }.join(", ")
5149
end
5250
end
5351
end

lib/secure_headers/headers/cookie.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ module SecureHeaders
77
class CookiesConfigError < StandardError; end
88
class Cookie
99

10-
class << self
11-
def validate_config!(config)
12-
CookiesConfig.new(config).validate!
13-
end
10+
def self.validate_config!(config)
11+
CookiesConfig.new(config).validate!
1412
end
1513

1614
attr_reader :raw_cookie, :config

lib/secure_headers/headers/expect_certificate_transparency.rb

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,29 @@ class ExpectCertificateTransparency
99
REQUIRED_MAX_AGE_ERROR = "max-age is a required directive.".freeze
1010
INVALID_MAX_AGE_ERROR = "max-age must be a number.".freeze
1111

12-
class << self
13-
# Public: Generate a expect-ct header.
14-
#
15-
# Returns nil if not configured, returns header name and value if
16-
# configured.
17-
def make_header(config, use_agent = nil)
18-
return if config.nil? || config == OPT_OUT
12+
# Public: Generate a expect-ct header.
13+
#
14+
# Returns nil if not configured, returns header name and value if
15+
# configured.
16+
def self.make_header(config, use_agent = nil)
17+
return if config.nil? || config == OPT_OUT
1918

20-
header = new(config)
21-
[HEADER_NAME, header.value]
22-
end
19+
header = new(config)
20+
[HEADER_NAME, header.value]
21+
end
2322

24-
def validate_config!(config)
25-
return if config.nil? || config == OPT_OUT
26-
raise ExpectCertificateTransparencyConfigError.new(INVALID_CONFIGURATION_ERROR) unless config.is_a? Hash
23+
def self.validate_config!(config)
24+
return if config.nil? || config == OPT_OUT
25+
raise ExpectCertificateTransparencyConfigError.new(INVALID_CONFIGURATION_ERROR) unless config.is_a? Hash
2726

28-
unless [true, false, nil].include?(config[:enforce])
29-
raise ExpectCertificateTransparencyConfigError.new(INVALID_ENFORCE_VALUE_ERROR)
30-
end
27+
unless [true, false, nil].include?(config[:enforce])
28+
raise ExpectCertificateTransparencyConfigError.new(INVALID_ENFORCE_VALUE_ERROR)
29+
end
3130

32-
if !config[:max_age]
33-
raise ExpectCertificateTransparencyConfigError.new(REQUIRED_MAX_AGE_ERROR)
34-
elsif config[:max_age].to_s !~ /\A\d+\z/
35-
raise ExpectCertificateTransparencyConfigError.new(INVALID_MAX_AGE_ERROR)
36-
end
31+
if !config[:max_age]
32+
raise ExpectCertificateTransparencyConfigError.new(REQUIRED_MAX_AGE_ERROR)
33+
elsif config[:max_age].to_s !~ /\A\d+\z/
34+
raise ExpectCertificateTransparencyConfigError.new(INVALID_MAX_AGE_ERROR)
3735
end
3836
end
3937

lib/secure_headers/headers/referrer_policy.rb

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,27 @@ class ReferrerPolicy
1515
unsafe-url
1616
)
1717

18-
class << self
19-
# Public: generate an Referrer Policy header.
20-
#
21-
# Returns a default header if no configuration is provided, or a
22-
# header name and value based on the config.
23-
def make_header(config = nil, user_agent = nil)
24-
return if config == OPT_OUT
25-
config ||= DEFAULT_VALUE
26-
[HEADER_NAME, Array(config).join(", ")]
27-
end
18+
# Public: generate an Referrer Policy header.
19+
#
20+
# Returns a default header if no configuration is provided, or a
21+
# header name and value based on the config.
22+
def self.make_header(config = nil, user_agent = nil)
23+
return if config == OPT_OUT
24+
config ||= DEFAULT_VALUE
25+
[HEADER_NAME, Array(config).join(", ")]
26+
end
2827

29-
def validate_config!(config)
30-
case config
31-
when nil, OPT_OUT
32-
# valid
33-
when String, Array
34-
config = Array(config)
35-
unless config.all? { |t| t.is_a?(String) && VALID_POLICIES.include?(t.downcase) }
36-
raise ReferrerPolicyConfigError.new("Value can only be one or more of #{VALID_POLICIES.join(", ")}")
37-
end
38-
else
39-
raise TypeError.new("Must be a string or array of strings. Found #{config.class}: #{config}")
28+
def self.validate_config!(config)
29+
case config
30+
when nil, OPT_OUT
31+
# valid
32+
when String, Array
33+
config = Array(config)
34+
unless config.all? { |t| t.is_a?(String) && VALID_POLICIES.include?(t.downcase) }
35+
raise ReferrerPolicyConfigError.new("Value can only be one or more of #{VALID_POLICIES.join(", ")}")
4036
end
37+
else
38+
raise TypeError.new("Must be a string or array of strings. Found #{config.class}: #{config}")
4139
end
4240
end
4341
end

lib/secure_headers/headers/strict_transport_security.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,19 @@ class StrictTransportSecurity
99
VALID_STS_HEADER = /\Amax-age=\d+(; includeSubdomains)?(; preload)?\z/i
1010
MESSAGE = "The config value supplied for the HSTS header was invalid. Must match #{VALID_STS_HEADER}"
1111

12-
class << self
13-
# Public: generate an hsts header name, value pair.
14-
#
15-
# Returns a default header if no configuration is provided, or a
16-
# header name and value based on the config.
17-
def make_header(config = nil, user_agent = nil)
18-
return if config == OPT_OUT
19-
[HEADER_NAME, config || DEFAULT_VALUE]
20-
end
12+
# Public: generate an hsts header name, value pair.
13+
#
14+
# Returns a default header if no configuration is provided, or a
15+
# header name and value based on the config.
16+
def self.make_header(config = nil, user_agent = nil)
17+
return if config == OPT_OUT
18+
[HEADER_NAME, config || DEFAULT_VALUE]
19+
end
2120

22-
def validate_config!(config)
23-
return if config.nil? || config == OPT_OUT
24-
raise TypeError.new("Must be a string. Found #{config.class}: #{config} #{config.class}") unless config.is_a?(String)
25-
raise STSConfigError.new(MESSAGE) unless config =~ VALID_STS_HEADER
26-
end
21+
def self.validate_config!(config)
22+
return if config.nil? || config == OPT_OUT
23+
raise TypeError.new("Must be a string. Found #{config.class}: #{config} #{config.class}") unless config.is_a?(String)
24+
raise STSConfigError.new(MESSAGE) unless config =~ VALID_STS_HEADER
2725
end
2826
end
2927
end

lib/secure_headers/headers/x_content_type_options.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@ class XContentTypeOptions
66
HEADER_NAME = "x-content-type-options".freeze
77
DEFAULT_VALUE = "nosniff"
88

9-
class << self
10-
# Public: generate an X-Content-Type-Options header.
11-
#
12-
# Returns a default header if no configuration is provided, or a
13-
# header name and value based on the config.
14-
def make_header(config = nil, user_agent = nil)
15-
return if config == OPT_OUT
16-
[HEADER_NAME, config || DEFAULT_VALUE]
17-
end
9+
# Public: generate an X-Content-Type-Options header.
10+
#
11+
# Returns a default header if no configuration is provided, or a
12+
# header name and value based on the config.
13+
def self.make_header(config = nil, user_agent = nil)
14+
return if config == OPT_OUT
15+
[HEADER_NAME, config || DEFAULT_VALUE]
16+
end
1817

19-
def validate_config!(config)
20-
return if config.nil? || config == OPT_OUT
21-
raise TypeError.new("Must be a string. Found #{config.class}: #{config}") unless config.is_a?(String)
22-
unless config.casecmp(DEFAULT_VALUE) == 0
23-
raise XContentTypeOptionsConfigError.new("Value can only be nil or 'nosniff'")
24-
end
18+
def self.validate_config!(config)
19+
return if config.nil? || config == OPT_OUT
20+
raise TypeError.new("Must be a string. Found #{config.class}: #{config}") unless config.is_a?(String)
21+
unless config.casecmp(DEFAULT_VALUE) == 0
22+
raise XContentTypeOptionsConfigError.new("Value can only be nil or 'nosniff'")
2523
end
2624
end
2725
end

lib/secure_headers/headers/x_download_options.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@ class XDownloadOptions
55
HEADER_NAME = "x-download-options".freeze
66
DEFAULT_VALUE = "noopen"
77

8-
class << self
9-
# Public: generate an x-download-options header.
10-
#
11-
# Returns a default header if no configuration is provided, or a
12-
# header name and value based on the config.
13-
def make_header(config = nil, user_agent = nil)
14-
return if config == OPT_OUT
15-
[HEADER_NAME, config || DEFAULT_VALUE]
16-
end
8+
# Public: generate an x-download-options header.
9+
#
10+
# Returns a default header if no configuration is provided, or a
11+
# header name and value based on the config.
12+
def self.make_header(config = nil, user_agent = nil)
13+
return if config == OPT_OUT
14+
[HEADER_NAME, config || DEFAULT_VALUE]
15+
end
1716

18-
def validate_config!(config)
19-
return if config.nil? || config == OPT_OUT
20-
raise TypeError.new("Must be a string. Found #{config.class}: #{config}") unless config.is_a?(String)
21-
unless config.casecmp(DEFAULT_VALUE) == 0
22-
raise XDOConfigError.new("Value can only be nil or 'noopen'")
23-
end
17+
def self.validate_config!(config)
18+
return if config.nil? || config == OPT_OUT
19+
raise TypeError.new("Must be a string. Found #{config.class}: #{config}") unless config.is_a?(String)
20+
unless config.casecmp(DEFAULT_VALUE) == 0
21+
raise XDOConfigError.new("Value can only be nil or 'noopen'")
2422
end
2523
end
2624
end

lib/secure_headers/headers/x_frame_options.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@ class XFrameOptions
1010
DEFAULT_VALUE = SAMEORIGIN
1111
VALID_XFO_HEADER = /\A(#{SAMEORIGIN}\z|#{DENY}\z|#{ALLOW_ALL}\z|#{ALLOW_FROM}[:\s])/i
1212

13-
class << self
14-
# Public: generate an X-Frame-Options header.
15-
#
16-
# Returns a default header if no configuration is provided, or a
17-
# header name and value based on the config.
18-
def make_header(config = nil, user_agent = nil)
19-
return if config == OPT_OUT
20-
[HEADER_NAME, config || DEFAULT_VALUE]
21-
end
13+
# Public: generate an X-Frame-Options header.
14+
#
15+
# Returns a default header if no configuration is provided, or a
16+
# header name and value based on the config.
17+
def self.make_header(config = nil, user_agent = nil)
18+
return if config == OPT_OUT
19+
[HEADER_NAME, config || DEFAULT_VALUE]
20+
end
2221

23-
def validate_config!(config)
24-
return if config.nil? || config == OPT_OUT
25-
raise TypeError.new("Must be a string. Found #{config.class}: #{config}") unless config.is_a?(String)
26-
unless config =~ VALID_XFO_HEADER
27-
raise XFOConfigError.new("Value must be SAMEORIGIN|DENY|ALLOW-FROM:|ALLOWALL")
28-
end
22+
def self.validate_config!(config)
23+
return if config.nil? || config == OPT_OUT
24+
raise TypeError.new("Must be a string. Found #{config.class}: #{config}") unless config.is_a?(String)
25+
unless config =~ VALID_XFO_HEADER
26+
raise XFOConfigError.new("Value must be SAMEORIGIN|DENY|ALLOW-FROM:|ALLOWALL")
2927
end
3028
end
3129
end

lib/secure_headers/headers/x_permitted_cross_domain_policies.rb

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@ class XPermittedCrossDomainPolicies
66
DEFAULT_VALUE = "none"
77
VALID_POLICIES = %w(all none master-only by-content-type by-ftp-filename)
88

9-
class << self
10-
# Public: generate an x-permitted-cross-domain-policies header.
11-
#
12-
# Returns a default header if no configuration is provided, or a
13-
# header name and value based on the config.
14-
def make_header(config = nil, user_agent = nil)
15-
return if config == OPT_OUT
16-
[HEADER_NAME, config || DEFAULT_VALUE]
17-
end
9+
# Public: generate an x-permitted-cross-domain-policies header.
10+
#
11+
# Returns a default header if no configuration is provided, or a
12+
# header name and value based on the config.
13+
def self.make_header(config = nil, user_agent = nil)
14+
return if config == OPT_OUT
15+
[HEADER_NAME, config || DEFAULT_VALUE]
16+
end
1817

19-
def validate_config!(config)
20-
return if config.nil? || config == OPT_OUT
21-
raise TypeError.new("Must be a string. Found #{config.class}: #{config}") unless config.is_a?(String)
22-
unless VALID_POLICIES.include?(config.downcase)
23-
raise XPCDPConfigError.new("Value can only be one of #{VALID_POLICIES.join(', ')}")
24-
end
18+
def self.validate_config!(config)
19+
return if config.nil? || config == OPT_OUT
20+
raise TypeError.new("Must be a string. Found #{config.class}: #{config}") unless config.is_a?(String)
21+
unless VALID_POLICIES.include?(config.downcase)
22+
raise XPCDPConfigError.new("Value can only be one of #{VALID_POLICIES.join(', ')}")
2523
end
2624
end
2725
end

lib/secure_headers/headers/x_xss_protection.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@ class XXssProtection
66
DEFAULT_VALUE = "0".freeze
77
VALID_X_XSS_HEADER = /\A[01](; mode=block)?(; report=.*)?\z/
88

9-
class << self
10-
# Public: generate an X-Xss-Protection header.
11-
#
12-
# Returns a default header if no configuration is provided, or a
13-
# header name and value based on the config.
14-
def make_header(config = nil, user_agent = nil)
15-
return if config == OPT_OUT
16-
[HEADER_NAME, config || DEFAULT_VALUE]
17-
end
9+
# Public: generate an X-Xss-Protection header.
10+
#
11+
# Returns a default header if no configuration is provided, or a
12+
# header name and value based on the config.
13+
def self.make_header(config = nil, user_agent = nil)
14+
return if config == OPT_OUT
15+
[HEADER_NAME, config || DEFAULT_VALUE]
16+
end
1817

19-
def validate_config!(config)
20-
return if config.nil? || config == OPT_OUT
21-
raise TypeError.new("Must be a string. Found #{config.class}: #{config}") unless config.is_a?(String)
22-
raise XXssProtectionConfigError.new("Invalid format (see VALID_X_XSS_HEADER)") unless config.to_s =~ VALID_X_XSS_HEADER
23-
end
18+
def self.validate_config!(config)
19+
return if config.nil? || config == OPT_OUT
20+
raise TypeError.new("Must be a string. Found #{config.class}: #{config}") unless config.is_a?(String)
21+
raise XXssProtectionConfigError.new("Invalid format (see VALID_X_XSS_HEADER)") unless config.to_s =~ VALID_X_XSS_HEADER
2422
end
2523
end
2624
end

0 commit comments

Comments
 (0)