|
1 | 1 | require 'cgi' |
| 2 | +require 'secure_headers/utils/cookies_config' |
2 | 3 |
|
3 | 4 | module SecureHeaders |
4 | 5 | class CookiesConfigError < StandardError; end |
5 | 6 | class Cookie |
6 | 7 |
|
7 | 8 | class << self |
8 | 9 | def validate_config!(config) |
9 | | - return if config.nil? || config == OPT_OUT |
10 | | - raise CookiesConfigError.new("config must be a hash.") unless config.is_a? Hash |
11 | | - |
12 | | - # secure and httponly - validate only boolean or Hash configuration |
13 | | - [:secure, :httponly].each do |attribute| |
14 | | - if config[attribute] && !(config[attribute].is_a?(Hash) || config[attribute].is_a?(TrueClass) || config[attribute].is_a?(FalseClass)) |
15 | | - raise CookiesConfigError.new("#{attribute} cookie config must be a hash or boolean") |
16 | | - end |
17 | | - end |
18 | | - |
19 | | - # secure and httponly - validate exclusive use of only or except but not both at the same time |
20 | | - [:secure, :httponly].each do |attribute| |
21 | | - if config[attribute].is_a?(Hash) |
22 | | - if config[attribute].key?(:only) && config[attribute].key?(:except) |
23 | | - raise CookiesConfigError.new("#{attribute} cookie config is invalid, simultaneous use of conditional arguments `only` and `except` is not permitted.") |
24 | | - end |
25 | | - |
26 | | - if (intersection = (config[attribute].fetch(:only, []) & config[attribute].fetch(:only, []))).any? |
27 | | - raise CookiesConfigError.new("#{attribute} cookie config is invalid, cookies #{intersection.join(', ')} cannot be enforced as lax and strict") |
28 | | - end |
29 | | - end |
30 | | - end |
31 | | - |
32 | | - if config[:samesite] |
33 | | - raise CookiesConfigError.new("samesite cookie config must be a hash") unless config[:samesite].is_a?(Hash) |
34 | | - |
35 | | - # when configuring with booleans, only one enforcement is permitted |
36 | | - if config[:samesite].key?(:lax) && config[:samesite][:lax].is_a?(TrueClass) && config[:samesite].key?(:strict) |
37 | | - raise CookiesConfigError.new("samesite cookie config is invalid, combination use of booleans and Hash to configure lax and strict enforcement is not permitted.") |
38 | | - elsif config[:samesite].key?(:strict) && config[:samesite][:strict].is_a?(TrueClass) && config[:samesite].key?(:lax) |
39 | | - raise CookiesConfigError.new("samesite cookie config is invalid, combination use of booleans and Hash to configure lax and strict enforcement is not permitted.") |
40 | | - end |
41 | | - |
42 | | - # validate Hash-based samesite configuration |
43 | | - if config[:samesite].key?(:lax) && config[:samesite][:lax].is_a?(Hash) |
44 | | - # validate exclusive use of only or except but not both at the same time |
45 | | - if config[:samesite][:lax].key?(:only) && config[:samesite][:lax].key?(:except) |
46 | | - raise CookiesConfigError.new("samesite lax cookie config is invalid, simultaneous use of conditional arguments `only` and `except` is not permitted.") |
47 | | - end |
48 | | - |
49 | | - if config[:samesite].key?(:strict) |
50 | | - # validate exclusivity of only and except members |
51 | | - if (intersection = (config[:samesite][:lax].fetch(:only, []) & config[:samesite][:strict].fetch(:only, []))).any? |
52 | | - raise CookiesConfigError.new("samesite cookie config is invalid, cookie(s) #{intersection.join(', ')} cannot be enforced as lax and strict") |
53 | | - end |
54 | | - |
55 | | - if (intersection = (config[:samesite][:lax].fetch(:except, []) & config[:samesite][:strict].fetch(:except, []))).any? |
56 | | - raise CookiesConfigError.new("samesite cookie config is invalid, cookie(s) #{intersection.join(', ')} cannot be enforced as lax and strict") |
57 | | - end |
58 | | - end |
59 | | - end |
60 | | - |
61 | | - if config[:samesite].key?(:strict) && config[:samesite][:strict].is_a?(Hash) |
62 | | - # validate exclusive use of only or except but not both at the same time |
63 | | - if config[:samesite][:strict].key?(:only) && config[:samesite][:strict].key?(:except) |
64 | | - raise CookiesConfigError.new("samesite strict cookie config is invalid, simultaneous use of conditional arguments `only` and `except` is not permitted.") |
65 | | - end |
66 | | - end |
67 | | - end |
| 10 | + CookiesConfig.new(config).valid? |
68 | 11 | end |
69 | 12 | end |
70 | 13 |
|
|
0 commit comments