fluidattacks_core_serializers 12.0.0__tar.gz → 12.2.0__tar.gz
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.
- fluidattacks_core_serializers-12.2.0/.envrc +1 -0
- fluidattacks_core_serializers-12.2.0/.gitignore +16 -0
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/PKG-INFO +3 -1
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/fluidattacks_core/serializers/snippet.py +36 -160
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/fluidattacks_core/serializers/syntax.py +41 -12
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/import-linter.cfg +1 -10
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/pyproject.toml +19 -3
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/ruff.toml +3 -0
- fluidattacks_core_serializers-12.2.0/tests/test_elixir_syntax.py +137 -0
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/tests/test_make_snippet.py +0 -164
- fluidattacks_core_serializers-12.2.0/uv.lock +983 -0
- fluidattacks_core_serializers-12.0.0/.envrc +0 -1
- fluidattacks_core_serializers-12.0.0/.gitignore +0 -5
- fluidattacks_core_serializers-12.0.0/build/default.nix +0 -19
- fluidattacks_core_serializers-12.0.0/build/filter.nix +0 -13
- fluidattacks_core_serializers-12.0.0/flake.lock +0 -303
- fluidattacks_core_serializers-12.0.0/flake.nix +0 -29
- fluidattacks_core_serializers-12.0.0/uv.lock +0 -957
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/fluidattacks_core/serializers/__init__.py +0 -0
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/fluidattacks_core/serializers/py.typed +0 -0
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/mypy.ini +0 -0
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/tests/__init__.py +0 -0
- {fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/tests/test_syntax_language_resolution.py +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
use flake ../../..#fluidattacks-core-serializers
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
dist/
|
|
2
|
+
dist-*/
|
|
3
|
+
*.zip
|
|
4
|
+
/utils/terraform-modules/**/.terraform.lock.hcl
|
|
5
|
+
/.cargo-cache
|
|
6
|
+
target/
|
|
7
|
+
.direnv/
|
|
8
|
+
.venv/
|
|
9
|
+
.vscode/settings.json
|
|
10
|
+
.zed/settings.json
|
|
11
|
+
# each package's shell writes its own, beside the package rather than here
|
|
12
|
+
**/.vscode/settings.json
|
|
13
|
+
*.so
|
|
14
|
+
# the publish check builds into its own tree per architecture
|
|
15
|
+
target-*/
|
|
16
|
+
/develops-ci-analytics-output
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: fluidattacks_core_serializers
|
|
3
|
-
Version: 12.
|
|
3
|
+
Version: 12.2.0
|
|
4
4
|
Summary: Fluid Attacks Core Serializers Library
|
|
5
5
|
Author-email: Development <development@fluidattacks.com>
|
|
6
6
|
License: MPL-2.0
|
|
@@ -11,9 +11,11 @@ Classifier: Programming Language :: Python :: 3
|
|
|
11
11
|
Classifier: Programming Language :: Python :: 3.11
|
|
12
12
|
Classifier: Topic :: Software Development :: Libraries
|
|
13
13
|
Requires-Python: >=3.11
|
|
14
|
+
Requires-Dist: fluidattacks-core-snippets>=0.1.0
|
|
14
15
|
Requires-Dist: more-itertools>=10.0.0
|
|
15
16
|
Requires-Dist: tree-sitter-c-sharp>=0.23.1
|
|
16
17
|
Requires-Dist: tree-sitter-dart-orchard>=0.3.2
|
|
18
|
+
Requires-Dist: tree-sitter-elixir>=0.3.5
|
|
17
19
|
Requires-Dist: tree-sitter-go>=0.23.4
|
|
18
20
|
Requires-Dist: tree-sitter-html>=0.23.2
|
|
19
21
|
Requires-Dist: tree-sitter-java>=0.23.4
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
from collections.abc import Iterator
|
|
2
1
|
from contextlib import suppress
|
|
3
|
-
from operator import itemgetter
|
|
4
2
|
from typing import NamedTuple
|
|
5
3
|
|
|
6
|
-
from more_itertools import chunked
|
|
7
4
|
from tree_sitter import Node
|
|
8
5
|
|
|
9
6
|
from fluidattacks_core.serializers.syntax import (
|
|
@@ -16,6 +13,8 @@ from fluidattacks_core.serializers.syntax import (
|
|
|
16
13
|
get_language_from_path,
|
|
17
14
|
get_node_content,
|
|
18
15
|
)
|
|
16
|
+
from fluidattacks_core.snippets import SnippetViewport as CoreSnippetViewport
|
|
17
|
+
from fluidattacks_core.snippets import make_snippet
|
|
19
18
|
|
|
20
19
|
# Constants
|
|
21
20
|
SNIPPETS_CONTEXT: int = 10
|
|
@@ -112,167 +111,41 @@ def _make_snippet(
|
|
|
112
111
|
viewport: SnippetViewport | None = None,
|
|
113
112
|
offset: int | None = None,
|
|
114
113
|
) -> Snippet:
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
# Build a list of line numbers to line contents, handling wrapping
|
|
119
|
-
if viewport is not None:
|
|
120
|
-
lines = _get_lines(lines_raw, viewport, offset=offset)
|
|
121
|
-
# Find the vertical center of the snippet
|
|
122
|
-
viewport_center = next(
|
|
123
|
-
(index for index, (line_no, _) in enumerate(lines) if line_no == viewport.line),
|
|
124
|
-
0,
|
|
125
|
-
)
|
|
126
|
-
|
|
127
|
-
# Find the horizontal left of the snippet
|
|
128
|
-
# We'll place the center at 25% from the left border
|
|
129
|
-
viewport_left: int = (
|
|
130
|
-
max(viewport.column - viewport.columns_per_line // 4, 0)
|
|
131
|
-
if viewport.column is not None
|
|
132
|
-
else 0
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
if lines:
|
|
136
|
-
# How many chars do we need to write the line number
|
|
137
|
-
loc_width: int = len(str(lines[-1][0]))
|
|
138
|
-
modify_lines(lines, viewport, viewport_left, loc_width)
|
|
139
|
-
if viewport.line_context == -1:
|
|
140
|
-
offset = 0
|
|
141
|
-
elif viewport_center - viewport.line_context <= 0:
|
|
142
|
-
offset = 0
|
|
143
|
-
lines = lines[
|
|
144
|
-
slice(
|
|
145
|
-
0,
|
|
146
|
-
2 * viewport.line_context + 1,
|
|
147
|
-
)
|
|
148
|
-
]
|
|
149
|
-
else:
|
|
150
|
-
offset = (
|
|
151
|
-
max(viewport_center - viewport.line_context, 0)
|
|
152
|
-
if (viewport_center + viewport.line_context < len(lines))
|
|
153
|
-
else max(len(lines) - 2 * viewport.line_context - 1, 0)
|
|
154
|
-
)
|
|
155
|
-
lines = lines[
|
|
156
|
-
slice(
|
|
157
|
-
max(viewport_center - viewport.line_context, 0),
|
|
158
|
-
viewport_center + viewport.line_context + 1,
|
|
159
|
-
)
|
|
160
|
-
if (viewport_center + viewport.line_context < len(lines))
|
|
161
|
-
else slice(
|
|
162
|
-
max(len(lines) - 2 * viewport.line_context - 1, 0),
|
|
163
|
-
len(lines),
|
|
164
|
-
)
|
|
165
|
-
]
|
|
166
|
-
|
|
167
|
-
# Highlight the column if requested
|
|
168
|
-
|
|
169
|
-
if _must_highlight_line_number(viewport):
|
|
170
|
-
lines.append((0, f" {' ':>{loc_width}} ^ Col {viewport_left}"))
|
|
171
|
-
|
|
172
|
-
if viewport:
|
|
173
|
-
return Snippet(
|
|
174
|
-
content="\n".join(map(itemgetter(1), lines)),
|
|
175
|
-
offset=offset,
|
|
176
|
-
line=viewport.line,
|
|
177
|
-
column=viewport.column,
|
|
178
|
-
columns_per_line=viewport.columns_per_line,
|
|
179
|
-
line_context=viewport.line_context,
|
|
180
|
-
highlight_line_number=viewport.highlight_line_number,
|
|
181
|
-
show_line_numbers=viewport.show_line_numbers,
|
|
182
|
-
wrap=viewport.wrap,
|
|
183
|
-
start_point=None,
|
|
184
|
-
)
|
|
114
|
+
if viewport is None:
|
|
115
|
+
rendered = make_snippet(content, None, offset)
|
|
116
|
+
return Snippet(content=rendered.content, offset=rendered.offset)
|
|
185
117
|
|
|
118
|
+
rendered = make_snippet(content, CoreSnippetViewport(**viewport._asdict()), offset)
|
|
186
119
|
return Snippet(
|
|
187
|
-
content=
|
|
188
|
-
offset=offset,
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
loc_width: int,
|
|
197
|
-
) -> None:
|
|
198
|
-
if not lines:
|
|
199
|
-
return
|
|
200
|
-
|
|
201
|
-
line_no_last: int | None = lines[-2][0] if len(lines) >= 2 else None
|
|
202
|
-
|
|
203
|
-
for index, (line_no, line) in enumerate(lines):
|
|
204
|
-
mark_symbol = _get_mark_symbol(line_no, line_no_last, viewport._replace())
|
|
205
|
-
line_no_str = "" if line_no == line_no_last else str(line_no)
|
|
206
|
-
line_no_last = line_no
|
|
207
|
-
|
|
208
|
-
# Apply horizontal viewport slicing
|
|
209
|
-
n_line = line[viewport_left : viewport_left + viewport.columns_per_line + 1]
|
|
210
|
-
line_prefix = f"{mark_symbol} {line_no_str:>{loc_width}} | "
|
|
211
|
-
|
|
212
|
-
lines[index] = (
|
|
213
|
-
line_no,
|
|
214
|
-
_format_line(n_line, line_prefix, viewport, lines[index][1]),
|
|
215
|
-
)
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
def _format_line(
|
|
219
|
-
line: str,
|
|
220
|
-
line_prefix: str,
|
|
221
|
-
viewport: SnippetViewport,
|
|
222
|
-
original_line: str,
|
|
223
|
-
) -> str:
|
|
224
|
-
if not viewport.show_line_numbers:
|
|
225
|
-
if original_line.startswith(line_prefix.rstrip()):
|
|
226
|
-
line = line.replace(line_prefix, "").replace(line_prefix.rstrip(), "")
|
|
227
|
-
return line
|
|
228
|
-
|
|
229
|
-
if not line.startswith(line_prefix) and viewport.highlight_line_number:
|
|
230
|
-
return f"{line_prefix}{line}".rstrip(" ")
|
|
231
|
-
|
|
232
|
-
if original_line.startswith(line_prefix[0]):
|
|
233
|
-
line = line.replace(line_prefix, "")
|
|
234
|
-
return line
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
def _get_mark_symbol(
|
|
238
|
-
line_no: int,
|
|
239
|
-
line_no_last: int | None,
|
|
240
|
-
viewport: SnippetViewport,
|
|
241
|
-
) -> str:
|
|
242
|
-
if line_no == viewport.line and line_no != line_no_last:
|
|
243
|
-
return ">"
|
|
244
|
-
return " "
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
def _must_highlight_line_number(viewport: SnippetViewport) -> bool:
|
|
248
|
-
return bool(
|
|
249
|
-
viewport.column is not None
|
|
250
|
-
and viewport.highlight_line_number
|
|
251
|
-
and viewport.show_line_numbers,
|
|
120
|
+
content=rendered.content,
|
|
121
|
+
offset=rendered.offset,
|
|
122
|
+
line=viewport.line,
|
|
123
|
+
column=viewport.column,
|
|
124
|
+
columns_per_line=viewport.columns_per_line,
|
|
125
|
+
line_context=viewport.line_context,
|
|
126
|
+
highlight_line_number=viewport.highlight_line_number,
|
|
127
|
+
show_line_numbers=viewport.show_line_numbers,
|
|
128
|
+
wrap=viewport.wrap,
|
|
252
129
|
)
|
|
253
130
|
|
|
254
131
|
|
|
255
|
-
def
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
)
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
if line:
|
|
273
|
-
yield from chunked(list(line), n=chunk_size) # type: ignore # noqa: PGH003
|
|
274
|
-
else:
|
|
275
|
-
yield ""
|
|
132
|
+
def _elixir_function_identifier(arguments: Node) -> Node | None:
|
|
133
|
+
head = arguments.named_child(0)
|
|
134
|
+
while head is not None:
|
|
135
|
+
if head.type != "binary_operator":
|
|
136
|
+
identifier = head.child_by_field_name("target") or head
|
|
137
|
+
if (head.type, identifier.text) == ("call", b"unquote"):
|
|
138
|
+
return None
|
|
139
|
+
return (
|
|
140
|
+
identifier
|
|
141
|
+
if identifier.type == "identifier"
|
|
142
|
+
else identifier.child_by_field_name("operator")
|
|
143
|
+
)
|
|
144
|
+
operator = head.child_by_field_name("operator")
|
|
145
|
+
if operator is None or operator.text != b"when":
|
|
146
|
+
return operator
|
|
147
|
+
head = head.child_by_field_name("left")
|
|
148
|
+
return None
|
|
276
149
|
|
|
277
150
|
|
|
278
151
|
def find_function_name(func_nodes: list[Node], language: str) -> tuple[Node, str] | None:
|
|
@@ -281,7 +154,10 @@ def find_function_name(func_nodes: list[Node], language: str) -> tuple[Node, str
|
|
|
281
154
|
(children := node.child_by_field_name(field_name))
|
|
282
155
|
or (children := next((x for x in node.children if x.type == field_name), None))
|
|
283
156
|
):
|
|
284
|
-
|
|
157
|
+
if language == "elixir":
|
|
158
|
+
children = _elixir_function_identifier(children)
|
|
159
|
+
if children is not None:
|
|
160
|
+
return children, node.type
|
|
285
161
|
return None
|
|
286
162
|
|
|
287
163
|
|
|
@@ -38,6 +38,8 @@ TREE_SITTER_SUPPORTED_IMPERATIVE_LANGUAGES = {
|
|
|
38
38
|
".cs": "csharp",
|
|
39
39
|
".cshtml": "razor",
|
|
40
40
|
".dart": "dart",
|
|
41
|
+
".ex": "elixir",
|
|
42
|
+
".exs": "elixir",
|
|
41
43
|
".go": "go",
|
|
42
44
|
".java": "java",
|
|
43
45
|
".js": "javascript",
|
|
@@ -87,6 +89,7 @@ TREE_SITTER_IMPORTS_DECLARATIONS_MAP: dict[str, tuple[str, ...]] = {
|
|
|
87
89
|
}
|
|
88
90
|
|
|
89
91
|
TREE_SITTER_FUNCTION_DECLARATION_MAP: dict[str, tuple[str, ...]] = {
|
|
92
|
+
"elixir": ("call",),
|
|
90
93
|
"csharp": (
|
|
91
94
|
"constructor_declaration",
|
|
92
95
|
"conversion_operator_declaration",
|
|
@@ -188,6 +191,9 @@ TREE_SITTER_FUNCTION_DECLARATION_MAP: dict[str, tuple[str, ...]] = {
|
|
|
188
191
|
}
|
|
189
192
|
|
|
190
193
|
TREE_SITTER_FUNCTION_IDENTIFIER = {
|
|
194
|
+
"elixir": {
|
|
195
|
+
"call": "arguments",
|
|
196
|
+
},
|
|
191
197
|
"python": {
|
|
192
198
|
"function_definition": "name",
|
|
193
199
|
},
|
|
@@ -254,6 +260,13 @@ TREE_SITTER_REQUIRES_SEQUENCE = {
|
|
|
254
260
|
"dart": ("function_signature", "function_body"),
|
|
255
261
|
}
|
|
256
262
|
|
|
263
|
+
_TREE_SITTER_DECLARATION_TARGETS = {
|
|
264
|
+
"elixir": {
|
|
265
|
+
# Elixir represents definitions, modules and ordinary invocations as calls.
|
|
266
|
+
"call": (b"def", b"defp", b"defmacro", b"defmacrop"),
|
|
267
|
+
},
|
|
268
|
+
}
|
|
269
|
+
|
|
257
270
|
|
|
258
271
|
class InvalidFileType(Exception): # noqa: N818
|
|
259
272
|
"""Exception to control file type."""
|
|
@@ -327,18 +340,40 @@ def parse_content_tree_sitter(
|
|
|
327
340
|
return parser.parse(content)
|
|
328
341
|
|
|
329
342
|
|
|
343
|
+
def _matches_declaration(language: str, node: Node) -> bool:
|
|
344
|
+
targets = _TREE_SITTER_DECLARATION_TARGETS.get(language, {}).get(node.type)
|
|
345
|
+
if targets is None:
|
|
346
|
+
return True
|
|
347
|
+
target = node.child_by_field_name("target")
|
|
348
|
+
if target is not None and target.type == "dot":
|
|
349
|
+
module = target.child_by_field_name("left")
|
|
350
|
+
target = (
|
|
351
|
+
target.child_by_field_name("right")
|
|
352
|
+
if module is not None
|
|
353
|
+
and b"".join((module.text or b"").split()) in (b"Kernel", b"Elixir.Kernel")
|
|
354
|
+
else None
|
|
355
|
+
)
|
|
356
|
+
return target is not None and target.type == "identifier" and target.text in targets
|
|
357
|
+
|
|
358
|
+
|
|
330
359
|
def query_nodes_by_language(
|
|
331
360
|
language: str,
|
|
332
|
-
tree: Tree,
|
|
361
|
+
tree: Tree | Node,
|
|
333
362
|
queries_map: dict[str, tuple[str, ...]],
|
|
334
363
|
) -> dict[str, list[Node]]:
|
|
335
364
|
try:
|
|
336
365
|
language_parser = get_language_for_tree(language)
|
|
337
366
|
except OSError as exc:
|
|
338
367
|
raise InvalidFileType from exc
|
|
339
|
-
|
|
368
|
+
node_types = queries_map.get(language, ())
|
|
369
|
+
query = "\n".join(f"({item}) @{item}" for item in node_types)
|
|
340
370
|
query_cursor = QueryCursor(tree_sitter.Query(language_parser, query))
|
|
341
|
-
return
|
|
371
|
+
return {
|
|
372
|
+
kind: [node for node in nodes if _matches_declaration(language, node)]
|
|
373
|
+
for kind, nodes in query_cursor.captures(
|
|
374
|
+
tree.root_node if isinstance(tree, Tree) else tree
|
|
375
|
+
).items()
|
|
376
|
+
}
|
|
342
377
|
|
|
343
378
|
|
|
344
379
|
def query_function_by_name(
|
|
@@ -362,10 +397,7 @@ def find_function_expression_by_query(
|
|
|
362
397
|
original_node: Node,
|
|
363
398
|
desired_line: int,
|
|
364
399
|
) -> Node | None:
|
|
365
|
-
|
|
366
|
-
target_nodes: tuple[str, ...] = TREE_SITTER_FUNCTION_DECLARATION_MAP.get(language, ())
|
|
367
|
-
query = "([" + (" ".join(f"({item})" for item in target_nodes)) + "] @target )"
|
|
368
|
-
result = QueryCursor(tree_sitter.Query(language_parser, query)).captures(original_node)
|
|
400
|
+
result = query_nodes_by_language(language, original_node, TREE_SITTER_FUNCTION_DECLARATION_MAP)
|
|
369
401
|
|
|
370
402
|
nodes = [node for _, nodes in result.items() for node in nodes]
|
|
371
403
|
candidates = [
|
|
@@ -394,7 +426,7 @@ def find_function_expression(original_node: Node | None, language: str) -> Node
|
|
|
394
426
|
node = original_node
|
|
395
427
|
while node:
|
|
396
428
|
for node_name in target_nodes:
|
|
397
|
-
if node_name == node.type:
|
|
429
|
+
if node_name == node.type and _matches_declaration(language, node):
|
|
398
430
|
return node
|
|
399
431
|
node = node.parent
|
|
400
432
|
|
|
@@ -466,10 +498,7 @@ def _process_hierarchical_candidates(
|
|
|
466
498
|
List of nodes ordered from most specific to most general
|
|
467
499
|
|
|
468
500
|
"""
|
|
469
|
-
|
|
470
|
-
target_nodes: tuple[str, ...] = TREE_SITTER_FUNCTION_DECLARATION_MAP.get(language, ())
|
|
471
|
-
query = "([" + (" ".join(f"({item})" for item in target_nodes)) + "] @target )"
|
|
472
|
-
result = QueryCursor(tree_sitter.Query(language_parser, query)).captures(root_node)
|
|
501
|
+
result = query_nodes_by_language(language, root_node, TREE_SITTER_FUNCTION_DECLARATION_MAP)
|
|
473
502
|
|
|
474
503
|
nodes = [node for _, nodes in result.items() for node in nodes]
|
|
475
504
|
return [
|
{fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/import-linter.cfg
RENAMED
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
[importlinter]
|
|
2
|
-
root_package = fluidattacks_core
|
|
2
|
+
root_package = fluidattacks_core.serializers
|
|
3
3
|
include_external_packages = True
|
|
4
4
|
|
|
5
|
-
[importlinter:contract:portion]
|
|
6
|
-
name = Single Portion
|
|
7
|
-
type = layers
|
|
8
|
-
containers =
|
|
9
|
-
fluidattacks_core
|
|
10
|
-
exhaustive = true
|
|
11
|
-
layers =
|
|
12
|
-
serializers
|
|
13
|
-
|
|
14
5
|
[importlinter:contract:dag]
|
|
15
6
|
name = Direct Acyclic Graph
|
|
16
7
|
type = layers
|
{fluidattacks_core_serializers-12.0.0 → fluidattacks_core_serializers-12.2.0}/pyproject.toml
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "fluidattacks_core_serializers"
|
|
3
|
-
version = "12.
|
|
3
|
+
version = "12.2.0"
|
|
4
4
|
description = "Fluid Attacks Core Serializers Library"
|
|
5
5
|
authors = [{ name = "Development", email = "development@fluidattacks.com" }]
|
|
6
6
|
license = { text = "MPL-2.0" }
|
|
@@ -14,9 +14,11 @@ classifiers = [
|
|
|
14
14
|
]
|
|
15
15
|
requires-python = ">=3.11"
|
|
16
16
|
dependencies = [
|
|
17
|
+
"fluidattacks-core-snippets>=0.1.0",
|
|
17
18
|
"more-itertools>=10.0.0",
|
|
18
19
|
"tree-sitter-c-sharp>=0.23.1",
|
|
19
20
|
"tree-sitter-dart-orchard>=0.3.2",
|
|
21
|
+
"tree-sitter-elixir>=0.3.5",
|
|
20
22
|
"tree-sitter-go>=0.23.4",
|
|
21
23
|
"tree-sitter-html>=0.23.2",
|
|
22
24
|
"tree-sitter-java>=0.23.4",
|
|
@@ -48,6 +50,7 @@ known_first_party = ["fluidattacks_core"]
|
|
|
48
50
|
DEP002 = [
|
|
49
51
|
"tree-sitter-c-sharp",
|
|
50
52
|
"tree-sitter-dart-orchard",
|
|
53
|
+
"tree-sitter-elixir",
|
|
51
54
|
"tree-sitter-go",
|
|
52
55
|
"tree-sitter-html",
|
|
53
56
|
"tree-sitter-java",
|
|
@@ -61,8 +64,8 @@ DEP002 = [
|
|
|
61
64
|
"tree-sitter-yaml",
|
|
62
65
|
]
|
|
63
66
|
|
|
64
|
-
[tool.uv]
|
|
65
|
-
|
|
67
|
+
[tool.uv.sources]
|
|
68
|
+
fluidattacks-core-snippets = { index = "internal" }
|
|
66
69
|
|
|
67
70
|
[build-system]
|
|
68
71
|
requires = ["hatchling"]
|
|
@@ -70,3 +73,16 @@ build-backend = "hatchling.build"
|
|
|
70
73
|
|
|
71
74
|
[tool.hatch.build.targets.wheel]
|
|
72
75
|
packages = ["fluidattacks_core"]
|
|
76
|
+
|
|
77
|
+
# mirror's cooldown holds back fresh releases; uv's exclude-newer cannot
|
|
78
|
+
# work there, since Cloudsmith's index has no upload times
|
|
79
|
+
[[tool.uv.index]]
|
|
80
|
+
name = "mirror"
|
|
81
|
+
url = "https://token@dl.cloudsmith.io/basic/fluidattacks/mirror/python/simple/"
|
|
82
|
+
default = true
|
|
83
|
+
|
|
84
|
+
# ours, released from trunk; explicit, so nothing else resolves from it
|
|
85
|
+
[[tool.uv.index]]
|
|
86
|
+
name = "internal"
|
|
87
|
+
url = "https://token@dl.cloudsmith.io/basic/fluidattacks/internal/python/simple/"
|
|
88
|
+
explicit = true
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from fluidattacks_core.serializers.snippet import Function, extract_function
|
|
4
|
+
from fluidattacks_core.serializers.syntax import (
|
|
5
|
+
TREE_SITTER_FUNCTION_DECLARATION_MAP,
|
|
6
|
+
extract_hierarchical_nodes,
|
|
7
|
+
find_function_expression,
|
|
8
|
+
get_language_from_path,
|
|
9
|
+
parse_content_tree_sitter,
|
|
10
|
+
query_nodes_by_language,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@pytest.mark.parametrize("path", ["lib/decoder.ex", "scripts/decoder.exs"])
|
|
15
|
+
def test_elixir_file_parses(path: str) -> None:
|
|
16
|
+
language = get_language_from_path(path)
|
|
17
|
+
assert language == "elixir"
|
|
18
|
+
tree = parse_content_tree_sitter(b"IO.puts(:hello)", language)
|
|
19
|
+
assert not tree.root_node.has_error
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.mark.parametrize("definition", ["def", "defp", "defmacro", "defmacrop"])
|
|
23
|
+
@pytest.mark.parametrize(
|
|
24
|
+
"body",
|
|
25
|
+
[
|
|
26
|
+
"decode(data) do\n if is_binary(data) do\n :erlang.binary_to_term(data)"
|
|
27
|
+
"\n end\n end",
|
|
28
|
+
"decode(data) when is_binary(data), do: :erlang.binary_to_term(data)",
|
|
29
|
+
],
|
|
30
|
+
)
|
|
31
|
+
def test_elixir_extracts_complete_definition(definition: str, body: str) -> None:
|
|
32
|
+
expected = f" {definition} {body}"
|
|
33
|
+
source = f"defmodule Decoder do\n{expected}\n def other(data), do: data\nend"
|
|
34
|
+
line = next(
|
|
35
|
+
index for index, text in enumerate(source.splitlines(), 1) if "binary_to_term" in text
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
snippet = extract_function(source, line, language="elixir")
|
|
39
|
+
|
|
40
|
+
assert snippet is not None
|
|
41
|
+
assert snippet.content == expected
|
|
42
|
+
assert snippet.start_point == (1, 0)
|
|
43
|
+
assert snippet.end_point is not None
|
|
44
|
+
assert snippet.end_point[0] == len(expected.splitlines())
|
|
45
|
+
assert snippet.function == Function(
|
|
46
|
+
name="decode", node_type="call", field_identifier_name="identifier"
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_elixir_ignores_calls_outside_definitions() -> None:
|
|
51
|
+
source = b"defmodule Decoder do\n IO.puts(:hello)\n def decode(data), do: data\nend"
|
|
52
|
+
tree = parse_content_tree_sitter(source, "elixir")
|
|
53
|
+
|
|
54
|
+
captures = query_nodes_by_language("elixir", tree, TREE_SITTER_FUNCTION_DECLARATION_MAP)
|
|
55
|
+
|
|
56
|
+
assert list(captures) == ["call"]
|
|
57
|
+
assert [node.text for node in captures["call"]] == [b"def decode(data), do: data"]
|
|
58
|
+
assert extract_function(source.decode(), 2, language="elixir") is None
|
|
59
|
+
assert extract_hierarchical_nodes(source, 2, "elixir") == []
|
|
60
|
+
assert find_function_expression(tree.root_node.named_children[0], "elixir") is None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def test_elixir_hierarchy_keeps_definition_around_nested_call() -> None:
|
|
64
|
+
source = (
|
|
65
|
+
b"defmodule Decoder do\n def decode(data) do\n :erlang.binary_to_term(data)\n end\nend"
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
nodes = extract_hierarchical_nodes(source, 3, "elixir")
|
|
69
|
+
|
|
70
|
+
assert [node.text for node in nodes] == [
|
|
71
|
+
b"def decode(data) do\n :erlang.binary_to_term(data)\n end",
|
|
72
|
+
]
|
|
73
|
+
inner_calls = query_nodes_by_language("elixir", nodes[0], {"elixir": ("dot",)})
|
|
74
|
+
assert find_function_expression(inner_calls["dot"][0], "elixir") == nodes[0]
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@pytest.mark.parametrize(
|
|
78
|
+
("target", "is_definition"),
|
|
79
|
+
[
|
|
80
|
+
("Kernel.def", True),
|
|
81
|
+
("Elixir.Kernel.def", True),
|
|
82
|
+
("Elixir .\n Kernel.def", True),
|
|
83
|
+
("Other.def", False),
|
|
84
|
+
("Kernel.inspect", False),
|
|
85
|
+
],
|
|
86
|
+
)
|
|
87
|
+
def test_elixir_qualified_definition_boundary(target: str, *, is_definition: bool) -> None:
|
|
88
|
+
expected = f" {target} decode(data) do\n :erlang.binary_to_term(data)\n end"
|
|
89
|
+
source = f"defmodule Decoder do\n{expected}\nend"
|
|
90
|
+
vulnerable_line = source.splitlines().index(" :erlang.binary_to_term(data)") + 1
|
|
91
|
+
tree = parse_content_tree_sitter(source.encode(), "elixir")
|
|
92
|
+
inner_node = tree.root_node.descendant_for_point_range(
|
|
93
|
+
(vulnerable_line - 1, 4), (vulnerable_line - 1, 5)
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
snippet = extract_function(source, vulnerable_line, language="elixir")
|
|
97
|
+
hierarchy = extract_hierarchical_nodes(source.encode(), vulnerable_line, "elixir")
|
|
98
|
+
ancestor = find_function_expression(inner_node, "elixir")
|
|
99
|
+
|
|
100
|
+
if is_definition:
|
|
101
|
+
assert snippet is not None
|
|
102
|
+
assert snippet.content == expected
|
|
103
|
+
assert snippet.function == Function(
|
|
104
|
+
name="decode", node_type="call", field_identifier_name="identifier"
|
|
105
|
+
)
|
|
106
|
+
assert [node.text for node in hierarchy] == [expected.lstrip().encode()]
|
|
107
|
+
assert ancestor is not None
|
|
108
|
+
assert ancestor.text == expected.lstrip().encode()
|
|
109
|
+
else:
|
|
110
|
+
assert snippet is None
|
|
111
|
+
assert hierarchy == []
|
|
112
|
+
assert ancestor is None
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@pytest.mark.parametrize(
|
|
116
|
+
("head", "name"),
|
|
117
|
+
[
|
|
118
|
+
("decode", "decode"),
|
|
119
|
+
("decode when true", "decode"),
|
|
120
|
+
("left + right", "+"),
|
|
121
|
+
("-value", "-"),
|
|
122
|
+
("unquote", "unquote"),
|
|
123
|
+
("unquote(name)", None),
|
|
124
|
+
("unquote(name)(data)", None),
|
|
125
|
+
],
|
|
126
|
+
)
|
|
127
|
+
def test_elixir_head_identifier(head: str, name: str | None) -> None:
|
|
128
|
+
snippet = extract_function(
|
|
129
|
+
f"defmodule Decoder do\n def {head}, do: :ok\nend", 2, language="elixir"
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
assert snippet is not None
|
|
133
|
+
if name is None:
|
|
134
|
+
assert snippet.function is None
|
|
135
|
+
else:
|
|
136
|
+
assert snippet.function is not None
|
|
137
|
+
assert snippet.function.name == name
|