[Bug] TOML string outputs are not properly escaped#6000
Open
eric-forte-elastic wants to merge 5 commits intomainfrom
Open
[Bug] TOML string outputs are not properly escaped#6000eric-forte-elastic wants to merge 5 commits intomainfrom
eric-forte-elastic wants to merge 5 commits intomainfrom
Conversation
Contributor
Bug - GuidelinesThese guidelines serve as a reminder set of considerations when addressing a bug in the code. Documentation and Context
Code Standards and Practices
Testing
Additional Checks
|
shashank-elastic
approved these changes
Apr 29, 2026
Contributor
shashank-elastic
left a comment
There was a problem hiding this comment.
Local Testing works as expected
Testing 🟢
detection-rules on 5182-bug-toml-string-outputs-are-not-properly-escaped [$?] is 📦 v1.6.29 via 🐍 v3.12.8 (.venv) on ☁️ shashank.suryanarayana@elastic.co
❯ python -m detection_rules import-rules-to-repo sample_rule.ndjson --required-only
Loaded config file: /Users/shashankks/elastic_workspace/detection-rules/.detection-rules-cfg.json
█▀▀▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄��▄ ▄▄▄ ▄▄▄ ▄ ▄ █▀▀▄ ▄ ▄ ▄ ▄▄▄ ▄▄▄
█ █ █▄▄ █ █▄▄ █ █ █ █ █ █▀▄ █ █▄▄▀ █ █ █ █▄▄ █▄▄
█▄▄▀ █▄▄ █ █▄▄ █▄▄ █ ▄█▄ █▄█ █ ▀▄█ █ ▀▄ █▄▄█ █▄▄ █▄▄ ▄▄█
[+] Building rule for /Users/shashankks/elastic_workspace/detection-rules/rules/sample_escaped_backslash_in_query_string_filter.toml
1 results exported
1 rules converted
0 exceptions exported
0 actions connectors exported
detection-rules on 5182-bug-toml-string-outputs-are-not-properly-escaped [$?] is 📦 v1.6.29 via 🐍 v3.12.8 (.venv) on ☁️ shashank.suryanarayana@elastic.co
❯
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request
Issue link(s):
Resolves #5182
Summary - What I changed
In
RuleTomlEncoder.dump_str, the multiline branch now escapes backslashes per line before emitting the"""…"""block. Literal newlines and single"characters remain valid inside triple-DQ basic strings and are left alone. Only\is doubled so that a source\:becomes TOML\\:, which the TOML parser decodes back to the original\:.Before (invalid TOML):
After (valid TOML, same decoded value):
The PR also adds this filter to the test_toml.jdson to test for this creating invalid toml.
Alternatives
Also considered alternatives such as trying to use
NESTED_PRESERVED_FIELD_NAMESor similar approaches as other formatting PRs. However, none were found that would reach the core issue. In the case ofNESTED_PRESERVED_FIELD_NAMES, among other reasons we have a hard-coded skip for any key namedquerynot just the base rulequery(which perhaps should be updated in another PR).How To Test
Either create a rule with a filter similar to that of the issue or use the supplied test ndjson.
issue_5182_rule.ndjson.txt
Once downloaded, run an import rule to repo command to see the newly created TOML and verify.
Also run
make test-remote-clito ensure no formatting issues on testing rules.Details
Checklist
bug,enhancement,schema,maintenance,Rule: New,Rule: Deprecation,Rule: Tuning,Hunt: New, orHunt: Tuningso guidelines can be generatedmeta:rapid-mergelabel if planning to merge within 24 hoursContributor checklist