Skip to content

[DaC] [Bug] Stop appending trailing newline to query on TOML export - #6865

Closed
Varshith-Kali wants to merge 1 commit into
elastic:mainfrom
Varshith-Kali:fix/export-query-trailing-newline
Closed

Varshith-Kali wants to merge 1 commit into
elastic:mainfrom
Varshith-Kali:fix/export-query-trailing-newline

Conversation

@Varshith-Kali

Copy link
Copy Markdown

What

Every export→import round-trip of a rule added a phantom trailing newline to query (and threat_query): toml_write() emitted query = '''\n<query>\n'''. TOML only trims the newline immediately following the opening delimiter, so the trailing \n before the closing ''' survived parsing — the imported query never matched the source.

Fix

Emit '''\n<query>''' (no trailing pad) for both query and threat_query in detection_rules/rule_formatter.py, with an explanatory NOTE comment. The intentional write-time .strip() standardization is untouched (repo lint policy, acknowledged in the issue thread).

Fixes #6081.

Tests

tests/test_toml_formatter.py: 8/8 pass (7 pre-existing + 1 new). The new test_query_round_trip_adds_no_trailing_newline asserts exact (strip-free) round-trip equality for query and threat_query across 5 shapes (single-line, multi-line, backslash paths, quote mixes) plus double round-trip stability. Notably, I removed a .strip() in the compare_formatted helper that was masking this bug — A/B via stash: without the fix, 3 tests fail (the new one plus test_formatter_rule and test_normalization, which now correctly catch it); with the fix, all pass. Ruff check and format clean on both changed files.

One pre-existing limitation, unchanged and out of scope: a query literally containing ''' cannot round-trip (raw insertion into a TOML literal string) — fails identically before/after this change.

toml_write wrapped query/threat_query as '''\n<query>\n'''. TOML only
trims the newline immediately following the opening delimiter, so the
trailing newline survived parsing and every export/import round-trip
appended a phantom newline to the query in Kibana (no accumulation
thanks to the write-time strip, but the imported value never matched
the source).

Write '''\n<query>''' instead so the parsed value is byte-identical to
the written query. threat_query had the identical pattern and is fixed
the same way.

Tests:
- new test_query_round_trip_adds_no_trailing_newline asserts exact
  round-trip equality (no strip masking) for query and threat_query,
  including a double round-trip stability check
- removed the formatted-side .strip() in compare_formatted that was
  masking this bug; the two pre-existing formatter tests now fail
  without the fix and pass with it

Fixes elastic#6081.

Signed-off-by: Varshith Puli <pulivarshit@gmail.com
@botelastic botelastic Bot added the python Internal python for the repository label Sep 23, 2026
@cla-checker-service

Copy link
Copy Markdown

❌ Author of the following commits did not sign a Contributor Agreement:
ba77ae7

Please, read and sign the above mentioned agreement if you want to contribute to this project

@botelastic

botelastic Bot commented Nov 22, 2026

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@botelastic botelastic Bot added the stale 60 days of inactivity label Nov 22, 2026
@botelastic

botelastic Bot commented Nov 29, 2026

Copy link
Copy Markdown

This has been closed due to inactivity. If you feel this is an error, please re-open and include a justifying comment.

@botelastic botelastic Bot closed this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport: auto community python Internal python for the repository stale 60 days of inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DaC] additional line break added to query after export/import

1 participant