[Bug] KQL does not properly escape leading forward slash#6001
Open
eric-forte-elastic wants to merge 5 commits into
Open
[Bug] KQL does not properly escape leading forward slash#6001eric-forte-elastic wants to merge 5 commits into
eric-forte-elastic wants to merge 5 commits into
Conversation
Co-authored-by: Copilot <copilot@github.com>
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.
Tested Locally works as expected.
Testing 🟢
detection-rules on 441-bug-kql-does-not-properly-escape-leading-forward-slash [$?] is 📦 v1.6.29 via 🐍 v3.12.8 (.venv) on ☁️ shashank.suryanarayana@elastic.co
❯ pip3 install lib/kql
Looking in indexes: https://pypi.org/simple, https://shashank.suryanarayana%40elastic.co:****@artifactory.elastic.dev/artifactory/api/pypi/pypi-endgame/simple
Processing ./lib/kql
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: eql==0.9.19 in ./.venv/lib/python3.12/site-packages (from detection-rules-kql==0.1.11) (0.9.19)
Requirement already satisfied: lark-parser>=0.12.0 in ./.venv/lib/python3.12/site-packages (from detection-rules-kql==0.1.11) (0.12.0)
Building wheels for collected packages: detection-rules-kql
Building wheel for detection-rules-kql (pyproject.toml) ... done
Created wheel for detection-rules-kql: filename=detection_rules_kql-0.1.11-py3-none-any.whl size=19164 sha256=98f119e2b61b6ebacaa1f46d7487a190ebb72cb07fc2b46ab5644fb67416a8bd
Stored in directory: /private/var/folders/jk/t_tlgnwx4w998xqw3_kjzyx00000gn/T/pip-ephem-wheel-cache-fztha8z4/wheels/a5/04/40/23c256d5f994741fa8afe9042023a31c1bafcf883c8be68ab6
Successfully built detection-rules-kql
Installing collected packages: detection-rules-kql
Attempting uninstall: detection-rules-kql
Found existing installation: detection-rules-kql 0.1.10
Uninstalling detection-rules-kql-0.1.10:
Successfully uninstalled detection-rules-kql-0.1.10
Successfully installed detection-rules-kql-0.1.11
[notice] A new release of pip is available: 25.1.1 -> 26.1
[notice] To update, run: pip install --upgrade pip
detection-rules on 441-bug-kql-does-not-properly-escape-leading-forward-slash [$?] is 📦 v1.6.29 via 🐍 v3.12.8 (.venv) on ☁️ shashank.suryanarayana@elastic.co took 21s
❯ python -c 'import kql; q = kql.to_dsl(r"process.args:/lockscreenurl\:http*")["bool"]["filter"][0]["query_string"]["query"]; print(repr(q)); assert q == r"\/lockscreenurl\:http*", "issue #441 still present"; print("OK")'
'\\/lockscreenurl\\:http*'
OK
detection-rules on 441-bug-kql-does-not-properly-escape-leading-forward-slash [$?] 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 #441
Summary - What I changed
Update to align
kql.to_dslwildcard conversion with Kibana'stoQueryStringQuery.ToDsl._walk_wildcardemittedtree.valueverbatim as a Lucenequery_string.query, under-escaping Lucene-reserved characters — most notably a leading/, which Lucene treats as a regex delimiter.Compounding the issue,
Wildcard.valuehad inconsistent semantics: the parser stored raw KQL text (with KQL escape sequences), whileeql2kqlandValue.from_pythonstored plain Python literals.Note
A main decision point is whether or not we are ok with modfying the type that is KQL parser value function returns. If so this PR can be small, otherwise it is a bit larger.
Difference in values passed from parser.py

More complex fix that does not require parser change: eeb1c76
Less complex fix (current PR). The less complex fix addresses a long time mismatch in behavior and as such is my preference for the fix.
Howver, this does introduce risk for any external caller that constructed Wildcard("...") directly with a raw, KQL-escaped string and depended on the old over-escaping behavior of to_dsl/_render would see different output. That being said, the bug is in effect the difference, so the change is a forseeable consequence of fixing it.
How To Test
Unit tests should pass and run the following Python one lcommand and/or script to identify that the issue is resolved.
Script Version
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