fluent-plugin-parser_cef 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b377ec06f9633e8cec0a789315bd8ad5a659773344d73607897c6b8adc192e6
4
- data.tar.gz: 52ebe7cebfd303741fd0992dcdaf3df612ce98441ead5b2f9b9eb65ae8182b84
3
+ metadata.gz: b15b0ff74bba7ae5207f7493b100edcc016d903d28021b605e6d557c30c1c8fd
4
+ data.tar.gz: '0966c3be3606e6b05b5bf55c96c4dd7bebf48dd91d341c4be4ac9736cab5941e'
5
5
  SHA512:
6
- metadata.gz: f71acec6579ffae1975da597e62156cd8e522d9cb26316288b9d9b6b33e2f32741edd1498af5baa737b67edfd376541ca077673d8f37c43edf2cc0189009dbf7
7
- data.tar.gz: 72ae69b82cf81ade1dfc54d6b5175cd133d0320b8e3cf58957be47c533fa9190545efb49d07f4af2d2a7746b6cb705ffd113e0fe8f0e1d445cdf73721c280ff5
6
+ metadata.gz: de60cf54a72888ed1f228615974c8ea62c517196fb46b4627d9b30ca16ddc8b2b8b7c0fd41ac3bf3b6d2567425e695e7eff39f58c6faf9b83fc26e566fc29c1e
7
+ data.tar.gz: df7da836cdc179cd1b36dea33d2a2c928ebb81d4c060fb8d6ef89f17ffc3bc481285a0dc267b19cee70cfdfc16c3221728d6d47f4cd02e8ce358d048dedc6b25
data/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
2
2
 
3
3
  All notable changes to this project are documented in this file.
4
4
 
5
+ ## 1.2.0 - 2026-12-05
6
+
7
+ ### Added
8
+
9
+ - `cef_unescape` option to handle CEF escape sequences: escaped `|` in
10
+ header fields and escaped `=` in extension values no longer act as
11
+ separators, and `\|` `\\` (header) and `\=` `\\` `\|` `\n` `\r`
12
+ (extension) are unescaped. Defaults to `false`, which keeps the 1.1.0
13
+ output (#14).
14
+
15
+ ### Removed
16
+
17
+ - The outdated `.rubocop.yml`, which current RuboCop rejects and which was
18
+ shipped in the gem (#15).
19
+
5
20
  ## 1.1.0 - 2026-12-05
6
21
 
7
22
  ### Added
data/README.md CHANGED
@@ -130,6 +130,7 @@ The `<PRI>`, RFC 5424 and header-less formats are supported from 1.1.0.
130
130
  | `cef_keyfilename` | string | `config/cef_version_0_keys.yaml` | YAML file with the known CEF keys used in strict mode. Relative paths are resolved from the plugin directory. |
131
131
  | `output_raw_field` | bool | `false` | Also add the original line as `raw` when parsing succeeds. |
132
132
  | `cef_additional_keys` | array | `[]` | Extra keys treated as known keys in strict mode, e.g. `cef_additional_keys vendorKey1,vendorKey2`. (1.1.0+) |
133
+ | `cef_unescape` | bool | `false` | Handle CEF escape sequences. See below. (1.2.0+) |
133
134
 
134
135
  ### Strict mode
135
136
 
@@ -150,6 +151,25 @@ to the previous value (a key at the start of the extension still becomes a
150
151
  field). Add them with `cef_additional_keys`, use your own key file, or set
151
152
  `parse_strict_mode false`.
152
153
 
154
+ ### Escape sequences
155
+
156
+ CEF escapes `|` and `\` in header fields, and `=` and `\` in extension
157
+ values, with a backslash, and encodes line breaks in extension values as `\n`
158
+ and `\r`. With `cef_unescape true`:
159
+
160
+ - an escaped `\|` does not end a header field, and an escaped `\=` does not
161
+ start a new key;
162
+ - `\|` and `\\` in header fields, and `\=`, `\\`, `\|`, `\n` and `\r` in
163
+ extension values, are unescaped. Other backslash sequences are kept.
164
+
165
+ For example, `CEF:0|Vend\|or|...|msg=a\=b cs1=C:\\temp` gives
166
+ `cef_device_vendor` = `Vend|or`, `msg` = `a=b` and `cs1` = `C:\temp`.
167
+
168
+ The default is `false`, which keeps the behaviour of 1.1.0 and earlier: escape
169
+ sequences are left in the values, and an escaped `|` or `=` can split fields
170
+ incorrectly. Enable it for new setups; the default may change in a future
171
+ major version.
172
+
153
173
  ## Development
154
174
 
155
175
  The easiest way is the included [Dev Container](.devcontainer/devcontainer.json)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.2.0
@@ -17,10 +17,24 @@ module Fluent
17
17
  config_param :cef_keyfilename, :string, :default => 'config/cef_version_0_keys.yaml'
18
18
  config_param :output_raw_field, :bool, :default => false
19
19
  config_param :cef_additional_keys, :array, :value_type => :string, :default => []
20
+ config_param :cef_unescape, :bool, :default => false
21
+
22
+ CEF_HEADER_FIELDS = %w[
23
+ cef_device_vendor cef_device_product cef_device_version
24
+ cef_device_event_class_id cef_name cef_severity
25
+ ].freeze
20
26
 
21
27
  def configure(conf)
22
28
  super
23
- @key_value_format_regexp = /([^\s=]+)=(.*?)(?:(?=[^\s=]+=)|\z)/
29
+ @key_value_format_regexp =
30
+ if @cef_unescape
31
+ # Keys cannot contain backslashes, and escape sequences (\\, \=)
32
+ # are consumed as a unit, so an escaped "=" never starts a key. A
33
+ # lone backslash at the very end is kept as part of the value.
34
+ /([^\s=\\]+)=((?:\\.|[^\\]|\\\z)*?)(?:(?=[^\s=\\]+=)|\z)/m
35
+ else
36
+ /([^\s=]+)=(.*?)(?:(?=[^\s=]+=)|\z)/
37
+ end
24
38
  @valid_format_regexps = create_valid_format_regexps
25
39
  @utc_offset = get_utc_offset(@log_utc_offset)
26
40
  begin
@@ -79,6 +93,11 @@ module Fluent
79
93
  record_cef_extension = parse_cef_extension(text_cef_extension)
80
94
  record.merge!(record_cef_extension)
81
95
  end
96
+ if @cef_unescape
97
+ CEF_HEADER_FIELDS.each do |key|
98
+ record[key] = unescape_cef_header(record[key]) if record.key?(key)
99
+ end
100
+ end
82
101
  record["raw"] = replaced_text if @output_raw_field
83
102
  yield time, record
84
103
  return
@@ -122,14 +141,16 @@ module Fluent
122
141
  (?<syslog_msgid>\S+)\s
123
142
  (?<syslog_structured_data>-|(?:\[(?:[^\]\\]|\\.)*\])+)\s
124
143
  /x
144
+ # With cef_unescape, "\|" and "\\" inside a header field do not end it.
145
+ field = @cef_unescape ? /(?:\\.|[^|\\])*/ : /[^|]*/
125
146
  cef_header = /
126
147
  CEF:(?<cef_version>#{@cef_version})\|
127
- (?<cef_device_vendor>[^|]*)\|
128
- (?<cef_device_product>[^|]*)\|
129
- (?<cef_device_version>[^|]*)\|
130
- (?<cef_device_event_class_id>[^|]*)\|
131
- (?<cef_name>[^|]*)\|
132
- (?<cef_severity>[^|]*)
148
+ (?<cef_device_vendor>#{field})\|
149
+ (?<cef_device_product>#{field})\|
150
+ (?<cef_device_version>#{field})\|
151
+ (?<cef_device_event_class_id>#{field})\|
152
+ (?<cef_name>#{field})\|
153
+ (?<cef_severity>#{field})
133
154
  /x
134
155
  [rfc3164_header, rfc5424_header, nil].map do |syslog_header|
135
156
  Regexp.new(/
@@ -164,10 +185,31 @@ module Fluent
164
185
  end
165
186
 
166
187
  def parse_cef_extension(text)
167
- if @parse_strict_mode == true
168
- return parse_cef_extension_with_strict_mode(text)
169
- else
170
- return parse_cef_extension_without_strict_mode(text)
188
+ record =
189
+ if @parse_strict_mode == true
190
+ parse_cef_extension_with_strict_mode(text)
191
+ else
192
+ parse_cef_extension_without_strict_mode(text)
193
+ end
194
+ record.transform_values! { |value| unescape_cef_extension(value) } if @cef_unescape
195
+ record
196
+ end
197
+
198
+ # CEF header fields escape "|" and "\" with a backslash.
199
+ def unescape_cef_header(value)
200
+ value.gsub(/\\([|\\])/, '\\1')
201
+ end
202
+
203
+ # CEF extension values escape "=" and "\" with a backslash and encode
204
+ # line breaks as \n or \r. An escaped "|" is accepted as well. Other
205
+ # backslash sequences are kept as they are.
206
+ def unescape_cef_extension(value)
207
+ value.gsub(/\\([=\\|nr])/) do
208
+ case $1
209
+ when "n" then "\n"
210
+ when "r" then "\r"
211
+ else $1
212
+ end
171
213
  end
172
214
  end
173
215
 
@@ -536,5 +536,78 @@ RSpec.describe Fluent::Plugin::CommonEventFormatParser do
536
536
  )
537
537
  end
538
538
  end
539
+
540
+ context "CEF escape sequences" do
541
+ def parse_cef(text, conf = "")
542
+ parsed = nil
543
+ create_driver(conf).instance.parse(text) { |_time, record| parsed = record }
544
+ parsed
545
+ end
546
+
547
+ let (:header_text) { 'CEF:0|Vend\|or|Prod\\\\uct|1.0|ID|Name|5|src=192.0.2.1' }
548
+ let (:extension_text) { 'CEF:0|V|P|1|1|N|1|msg=a\=b c=d cs1=C:\\\\temp\\\\x cs2=line1\nline2\rend src=192.0.2.1' }
549
+
550
+ context "with cef_unescape false (default)" do
551
+ it "keeps the 1.1.0 behaviour for escaped pipes in the header" do
552
+ record = parse_cef(header_text)
553
+ expect(record["cef_device_vendor"]).to eq 'Vend\\'
554
+ expect(record["cef_device_product"]).to eq 'or'
555
+ end
556
+
557
+ it "keeps escape sequences in extension values" do
558
+ record = parse_cef(extension_text)
559
+ expect(record["msg"]).to eq 'a\=b c=d'
560
+ expect(record["cs1"]).to eq 'C:\\\\temp\\\\x'
561
+ expect(record["cs2"]).to eq 'line1\nline2\rend'
562
+ end
563
+ end
564
+
565
+ context "with cef_unescape true" do
566
+ it "does not split header fields on escaped pipes and unescapes them" do
567
+ record = parse_cef(header_text, %[cef_unescape true])
568
+ expect(record).to include(
569
+ "cef_device_vendor" => "Vend|or",
570
+ "cef_device_product" => 'Prod\\uct',
571
+ "cef_device_version" => "1.0",
572
+ "cef_device_event_class_id" => "ID",
573
+ "cef_name" => "Name",
574
+ "cef_severity" => "5",
575
+ "src" => "192.0.2.1",
576
+ )
577
+ end
578
+
579
+ it "unescapes extension values in strict mode" do
580
+ record = parse_cef(extension_text, %[cef_unescape true])
581
+ expect(record.reject { |key, _| key.start_with?("cef_") }).to eq(
582
+ "msg" => "a=b c=d",
583
+ "cs1" => 'C:\\temp\\x',
584
+ "cs2" => "line1\nline2\rend",
585
+ "src" => "192.0.2.1",
586
+ )
587
+ end
588
+
589
+ it "does not treat an escaped equals sign as a key separator without strict mode" do
590
+ record = parse_cef(extension_text, %[cef_unescape true\nparse_strict_mode false])
591
+ expect(record.reject { |key, _| key.start_with?("cef_") }).to eq(
592
+ "msg" => "a=b",
593
+ "c" => "d",
594
+ "cs1" => 'C:\\temp\\x',
595
+ "cs2" => "line1\nline2\rend",
596
+ "src" => "192.0.2.1",
597
+ )
598
+ end
599
+
600
+ it "unescapes values that absorbed unknown keys in strict mode" do
601
+ record = parse_cef('CEF:0|V|P|1|1|N|1|src=192.0.2.1 vendorKey=a\=b act=x', %[cef_unescape true])
602
+ expect(record["src"]).to eq "192.0.2.1 vendorKey=a=b"
603
+ expect(record["act"]).to eq "x"
604
+ end
605
+
606
+ it "keeps a trailing backslash and unknown escape sequences" do
607
+ record = parse_cef('CEF:0|V|P|1|1|N|1|msg=keep\t end\\', %[cef_unescape true])
608
+ expect(record["msg"]).to eq 'keep\t end\\'
609
+ end
610
+ end
611
+ end
539
612
  end
540
613
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-parser_cef
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomoyuki Sugimura
@@ -99,7 +99,6 @@ files:
99
99
  - ".github/workflows/test.yml"
100
100
  - ".gitignore"
101
101
  - ".rspec"
102
- - ".rubocop.yml"
103
102
  - CHANGELOG.md
104
103
  - Gemfile
105
104
  - LICENSE