Module: Datadog::AppSec::Configuration::Settings

Defined in:
lib/datadog/appsec/configuration/settings.rb

Overview

Settings

Constant Summary collapse

DEFAULT_OBFUSCATOR_KEY_REGEX =

rubocop:disable Layout/LineLength

'(?i)pass|pw(?:or)?d|secret|(?:api|private|public|access)[_-]?key|token|consumer[_-]?(?:id|key|secret)|sign(?:ed|ature)|bearer|authorization|jsessionid|phpsessid|asp\.net[_-]sessionid|sid|jwt'
DEFAULT_OBFUSCATOR_VALUE_REGEX =
'(?i)(?:p(?:ass)?w(?:or)?d|pass(?:[_-]?phrase)?|secret(?:[_-]?key)?|(?:(?:api|private|public|access)[_-]?)key(?:[_-]?id)?|(?:(?:auth|access|id|refresh)[_-]?)?token|consumer[_-]?(?:id|key|secret)|sign(?:ed|ature)?|auth(?:entication|orization)?|jsessionid|phpsessid|asp\.net(?:[_-]|-)sessionid|sid|jwt)(?:\s*=[^;]|"\s*:\s*"[^"]+")|bearer\s+[a-z0-9\._\-]+|token:[a-z0-9]{13}|gh[opsu]_[0-9a-zA-Z]{36}|ey[I-L][\w=-]+\.ey[I-L][\w=-]+(?:\.[\w.+\/=-]+)?|[\-]{5}BEGIN[a-z\s]+PRIVATE\sKEY[\-]{5}[^\-]+[\-]{5}END[a-z\s]+PRIVATE\sKEY|ssh-rsa\s*[a-z0-9\/\.+]{100,}'
DISABLED_AUTO_USER_INSTRUMENTATION_MODE =

rubocop:enable Layout/LineLength

'disabled'
ANONYMIZATION_AUTO_USER_INSTRUMENTATION_MODE =
'anonymization'
IDENTIFICATION_AUTO_USER_INSTRUMENTATION_MODE =
'identification'
AUTO_USER_INSTRUMENTATION_MODES =
[
  DISABLED_AUTO_USER_INSTRUMENTATION_MODE,
  ANONYMIZATION_AUTO_USER_INSTRUMENTATION_MODE,
  IDENTIFICATION_AUTO_USER_INSTRUMENTATION_MODE
].freeze
AUTO_USER_INSTRUMENTATION_MODES_ALIASES =
{
  'ident' => IDENTIFICATION_AUTO_USER_INSTRUMENTATION_MODE,
  'anon' => ANONYMIZATION_AUTO_USER_INSTRUMENTATION_MODE,
}.freeze
SAFE_TRACK_USER_EVENTS_MODE =

NOTE: These two constants are deprecated

'safe'
EXTENDED_TRACK_USER_EVENTS_MODE =
'extended'
APPSEC_VALID_TRACK_USER_EVENTS_MODE =
[
  SAFE_TRACK_USER_EVENTS_MODE, EXTENDED_TRACK_USER_EVENTS_MODE
].freeze
APPSEC_VALID_TRACK_USER_EVENTS_ENABLED_VALUES =
['1', 'true'].concat(
  APPSEC_VALID_TRACK_USER_EVENTS_MODE
).freeze

Class Method Summary collapse

Class Method Details

.add_settings!(base) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/BlockLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/datadog/appsec/configuration/settings.rb', line 45

def self.add_settings!(base)
  base.class_eval do
    settings :appsec do
      option :enabled do |o|
        o.type :bool
        o.env 'DD_APPSEC_ENABLED'
        o.default false
      end

      define_method(:instrument) do |integration_name|
        if enabled
          registered_integration = Datadog::AppSec::Contrib::Integration.registry[integration_name]
          if registered_integration
            klass = registered_integration.klass
            if klass.loaded? && klass.compatible?
              instance = klass.new
              instance.patcher.patch unless instance.patcher.patched?
            end
          end
        end
      end

      # RASP or Runtime Application Self-Protection
      # is a collection of techniques and heuristics aimed at detecting malicious inputs and preventing
      # any potential side-effects on the application resulting from the use of said malicious inputs.
      option :rasp_enabled do |o|
        o.type :bool, nilable: true
        o.env 'DD_APPSEC_RASP_ENABLED'
        o.default true
      end

      option :ruleset do |o|
        o.env 'DD_APPSEC_RULES'
        o.default :recommended
      end

      option :ip_passlist do |o|
        o.default []
      end

      option :ip_denylist do |o|
        o.type :array
        o.default []
      end

      option :user_id_denylist do |o|
        o.type :array
        o.default []
      end

      option :waf_timeout do |o|
        o.env 'DD_APPSEC_WAF_TIMEOUT' # us
        o.default 5_000
        o.setter do |v|
          Datadog::Core::Utils::Duration.call(v.to_s, base: :us)
        end
      end

      option :waf_debug do |o|
        o.env 'DD_APPSEC_WAF_DEBUG'
        o.default false
        o.type :bool
      end

      option :trace_rate_limit do |o|
        o.type :int
        o.env 'DD_APPSEC_TRACE_RATE_LIMIT' # trace/s
        o.default 100
      end

      option :obfuscator_key_regex do |o|
        o.type :string
        o.env 'DD_APPSEC_OBFUSCATION_PARAMETER_KEY_REGEXP'
        o.default DEFAULT_OBFUSCATOR_KEY_REGEX
      end

      option :obfuscator_value_regex do |o|
        o.type :string
        o.env 'DD_APPSEC_OBFUSCATION_PARAMETER_VALUE_REGEXP'
        o.default DEFAULT_OBFUSCATOR_VALUE_REGEX
      end

      settings :block do
        settings :templates do
          option :html do |o|
            o.env 'DD_APPSEC_HTTP_BLOCKED_TEMPLATE_HTML'
            o.type :string, nilable: true
            o.setter do |value|
              if value
                unless File.exist?(value)
                  raise(ArgumentError,
                    "appsec.templates.html: file not found: #{value}")
                end

                File.binread(value) || ''
              end
            end
          end

          option :json do |o|
            o.env 'DD_APPSEC_HTTP_BLOCKED_TEMPLATE_JSON'
            o.type :string, nilable: true
            o.setter do |value|
              if value
                unless File.exist?(value)
                  raise(ArgumentError,
                    "appsec.templates.json: file not found: #{value}")
                end

                File.binread(value) || ''
              end
            end
          end

          option :text do |o|
            o.env 'DD_APPSEC_HTTP_BLOCKED_TEMPLATE_TEXT'
            o.type :string, nilable: true
            o.setter do |value|
              if value
                unless File.exist?(value)
                  raise(ArgumentError,
                    "appsec.templates.text: file not found: #{value}")
                end

                File.binread(value) || ''
              end
            end
          end
        end
      end

      settings :stack_trace do
        option :enabled do |o|
          o.type :bool
          o.env 'DD_APPSEC_STACK_TRACE_ENABLED'
          o.default true
        end

        # The maximum number of stack trace frames to collect for each stack trace.
        #
        # If the stack trace exceeds this limit, the frames are dropped from the middle of the stack trace:
        # 75% of the frames are kept from the top of the stack trace and 25% from the bottom
        # (this percentage is also configurable).
        #
        # Minimum value is 10.
        # Set to zero if you don't want any frames to be dropped.
        #
        # Default value is 32
        option :max_depth do |o|
          o.type :int
          o.env 'DD_APPSEC_MAX_STACK_TRACE_DEPTH'
          o.default 32

          o.setter do |value|
            value = 0 if value < 0
            value
          end
        end

        # The percentage of frames to keep from the top of the stack trace.
        #
        # Default value is 75
        option :top_percentage do |o|
          o.type :int
          o.env 'DD_APPSEC_MAX_STACK_TRACE_DEPTH_TOP_PERCENT'
          o.default 75

          o.setter do |value|
            value = 100 if value > 100
            value = 0 if value.negative?
            value
          end
        end

        # Maximum number of stack traces to collect per span.
        #
        # Set to zero if you want to collect all stack traces.
        #
        # Default value is 2
        option :max_stack_traces do |o|
          o.type :int
          o.env 'DD_APPSEC_MAX_STACK_TRACES'
          o.default 2

          o.setter do |value|
            value = 0 if value < 0
            value
          end
        end
      end

      settings :auto_user_instrumentation do
        define_method(:enabled?) { get_option(:mode) != DISABLED_AUTO_USER_INSTRUMENTATION_MODE }

        option :mode do |o|
          o.type :string
          o.env 'DD_APPSEC_AUTO_USER_INSTRUMENTATION_MODE'
          o.default IDENTIFICATION_AUTO_USER_INSTRUMENTATION_MODE
          o.setter do |value|
            mode = AUTO_USER_INSTRUMENTATION_MODES_ALIASES.fetch(value, value)
            next mode if AUTO_USER_INSTRUMENTATION_MODES.include?(mode)

            Datadog.logger.warn(
              'The appsec.auto_user_instrumentation.mode value provided is not supported. ' \
              "Supported values are: #{AUTO_USER_INSTRUMENTATION_MODES.join(" | ")}. " \
              "Using value: #{DISABLED_AUTO_USER_INSTRUMENTATION_MODE}."
            )

            DISABLED_AUTO_USER_INSTRUMENTATION_MODE
          end
        end
      end

      # DEV-3.0: Remove `track_user_events.enabled` and `track_user_events.mode` options
      settings :track_user_events do
        option :enabled do |o|
          o.default true
          o.type :bool
          o.env 'DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING'
          o.env_parser do |env_value|
            if env_value == 'disabled'
              false
            else
              APPSEC_VALID_TRACK_USER_EVENTS_ENABLED_VALUES.include?(env_value.strip.downcase)
            end
          end
          o.after_set do |_, _, precedence|
            unless precedence == Datadog::Core::Configuration::Option::Precedence::DEFAULT
              Core.log_deprecation(key: :appsec_track_user_events_enabled) do
                'The appsec.track_user_events.enabled setting is deprecated. ' \
                'Please remove it from your Datadog.configure block and use ' \
                'appsec.auto_user_instrumentation.mode instead.'
              end
            end
          end
        end

        option :mode do |o|
          o.type :string
          o.env 'DD_APPSEC_AUTOMATED_USER_EVENTS_TRACKING'
          o.default SAFE_TRACK_USER_EVENTS_MODE
          o.setter do |v|
            if APPSEC_VALID_TRACK_USER_EVENTS_MODE.include?(v)
              v
            elsif v == 'disabled'
              SAFE_TRACK_USER_EVENTS_MODE
            else
              Datadog.logger.warn(
                'The appsec.track_user_events.mode value provided is not supported.' \
                "Supported values are: #{APPSEC_VALID_TRACK_USER_EVENTS_MODE.join(" | ")}." \
                "Using default value: #{SAFE_TRACK_USER_EVENTS_MODE}."
              )

              SAFE_TRACK_USER_EVENTS_MODE
            end
          end
          o.after_set do |_, _, precedence|
            unless precedence == Datadog::Core::Configuration::Option::Precedence::DEFAULT
              Core.log_deprecation(key: :appsec_track_user_events_mode) do
                'The appsec.track_user_events.mode setting is deprecated. ' \
                'Please remove it from your Datadog.configure block and use ' \
                'appsec.auto_user_instrumentation.mode instead.'
              end
            end
          end
        end
      end

      settings :api_security do
        option :enabled do |o|
          o.type :bool
          o.env 'DD_EXPERIMENTAL_API_SECURITY_ENABLED'
          o.default false
        end

        option :sample_rate do |o|
          o.type :float
          o.env 'DD_API_SECURITY_REQUEST_SAMPLE_RATE'
          o.default 0.1
          o.setter do |value|
            value = 1 if value > 1
            SampleRate.new(value)
          end
        end
      end

      option :sca_enabled do |o|
        o.type :bool, nilable: true
        o.env 'DD_APPSEC_SCA_ENABLED'
      end
    end
  end
end

.extended(base) ⇒ Object



39
40
41
42
# File 'lib/datadog/appsec/configuration/settings.rb', line 39

def self.extended(base)
  base = base.singleton_class unless base.is_a?(Class)
  add_settings!(base)
end