-
Notifications
You must be signed in to change notification settings - Fork 588
[FR] Add white space checking for KQL parse #3789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
40fb63d
9921f82
d3ddef7
81d78b3
3b9fab2
3026a3b
64910af
def19b5
1d8359c
96ce2da
358a1f1
e54155d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
# or more contributor license agreements. Licensed under the Elastic License | ||
# 2.0; you may not use this file except in compliance with the Elastic License | ||
# 2.0. | ||
|
||
import re | ||
|
||
from lark import Token # noqa: F401 | ||
from lark import Tree | ||
|
||
from typing import List | ||
from kql.errors import KqlParseError | ||
|
||
|
||
def check_whitespace(token_positions: List, token: str, lines: List[str]) -> None: | ||
"""Check for whitespace around a token.""" | ||
for line_num, column in token_positions: | ||
# Check the substring at the given position | ||
line = lines[line_num - 1] | ||
start = column - 1 | ||
end = column + len(token) - 1 | ||
|
||
# Handle cases where token starts at the beginning of the line and is followed by whitespace | ||
if start == 0 and (end < len(line) and re.match(r"\s", line[end])): | ||
continue | ||
|
||
# Check for whitespace around the token | ||
if ( | ||
start > 0 | ||
and (end < len(line) and re.match(r"\s", line[end]) or end == len(line)) | ||
and re.match(r"\s", line[start - 1]) | ||
): | ||
continue | ||
else: | ||
raise KqlParseError( | ||
error_msg=f"Missing whitespace around '{token}' token", | ||
line=line_num, | ||
column=column, | ||
source=line, | ||
width=len(token), | ||
trailer=None | ||
eric-forte-elastic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
|
||
def collect_token_positions(tree: Tree, token: str) -> List: | ||
"""Collect token positions from a tree.""" | ||
token_positions = [] | ||
for child in tree.children: | ||
if isinstance(child, Token) and child.value.lower() in [token]: | ||
token_positions.append((child.line, child.column)) | ||
elif isinstance(child, Tree): | ||
token_positions.extend(collect_token_positions(child, token)) | ||
return token_positions |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,3 +103,10 @@ def test_optimization(self): | |
"{'match': {'destination.ip': '169.254.169.254'}}]}}]}}" | ||
) | ||
self.assertEqual(dsl_str, good_case, "DSL string does not match the good case, optimization failed.") | ||
|
||
def test_blank_space(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about multi-line queries with tokens spanning lines or queries with leading/trailing whitespace on lines themselves? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to make sure I am following, is not
Which I think would be a query with the token spanning the line as the or requires both lines, and the needed second whitespace operator is leading on the second line? Could you give me an example, I'm not sure I am following? |
||
with self.assertRaises(kql.KqlParseError): | ||
kql.lark_parse('"Test-ServiceDaclPermission" or"Update-ExeFunctions"') | ||
eric-forte-elastic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
kql.lark_parse('"Test-ServiceDaclPermission" or "Update-ExeFunctions"') | ||
kql.lark_parse('"Test-ServiceDaclPermission" \nor "Update-ExeFunctions"') | ||
kql.lark_parse('"Test-ServiceDaclPermission" or\n "Update-ExeFunctions"') |
Uh oh!
There was an error while loading. Please reload this page.