-
Notifications
You must be signed in to change notification settings - Fork 706
Progress towards tree-sitter feature #3102
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
base: master
Are you sure you want to change the base?
Changes from all commits
7bd2acd
f8d3848
bcfe22e
d86a44e
3ebf4bd
9b36aac
ddb701c
772e55d
fcca9ad
88a98f4
18342bb
14610dc
8057248
291975a
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,20 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from PyInstaller.utils.hooks import collect_data_files | ||
|
|
||
|
|
||
| # Tree-sitter signature lookups use importlib.resources, so PyInstaller must | ||
| # bundle the JSON files alongside the package. | ||
| datas = collect_data_files("capa.features.extractors.ts.signatures") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Copyright 2022 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import Tuple, Iterator | ||
|
|
||
| from capa.features.common import OS, OS_ANY, ARCH_ANY, FORMAT_SCRIPT, Arch, Format, Feature, ScriptLanguage | ||
| from capa.features.address import NO_ADDRESS, Address, FileOffsetRangeAddress | ||
|
|
||
| # Can be used to instantiate tree_sitter Language objects (see ts/query.py) | ||
| LANG_CS = "c_sharp" | ||
| LANG_HTML = "html" | ||
| LANG_JS = "javascript" | ||
| LANG_PY = "python" | ||
| LANG_TEM = "embedded_template" | ||
|
|
||
| EXT_ASPX = ("aspx", "aspx_") | ||
| EXT_CS = ("cs", "cs_") | ||
| EXT_HTML = ("html", "html_") | ||
| EXT_PY = ("py", "py_") | ||
|
|
||
|
|
||
| LANGUAGE_FEATURE_FORMAT = { | ||
| LANG_CS: "C#", | ||
| LANG_HTML: "HTML", | ||
| LANG_JS: "JavaScript", | ||
| LANG_PY: "Python", | ||
| LANG_TEM: "Embedded Template", | ||
| } | ||
|
|
||
|
|
||
| def extract_arch() -> Iterator[Tuple[Feature, Address]]: | ||
| yield Arch(ARCH_ANY), NO_ADDRESS | ||
|
|
||
|
|
||
| def extract_language(language: str, addr: FileOffsetRangeAddress) -> Iterator[Tuple[Feature, Address]]: | ||
| yield ScriptLanguage(LANGUAGE_FEATURE_FORMAT[language]), addr | ||
|
|
||
|
|
||
| def extract_os() -> Iterator[Tuple[Feature, Address]]: | ||
| yield OS(OS_ANY), NO_ADDRESS | ||
|
|
||
|
|
||
| def extract_format() -> Iterator[Tuple[Feature, Address]]: | ||
| yield Format(FORMAT_SCRIPT), NO_ADDRESS |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||||||||||||||||||||||||
| # Copyright 2022 Google LLC | ||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||
| # you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||
| # You may obtain a copy of the License at | ||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||
| # See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||
| # limitations under the License. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from typing import Optional | ||||||||||||||||||||||||||||
| from pathlib import Path | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from tree_sitter import Node, Tree, Query, Parser, Language, QueryCursor | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from capa.features.extractors.script import EXT_CS, EXT_PY, LANG_CS, LANG_PY, EXT_ASPX, EXT_HTML, LANG_TEM, LANG_HTML | ||||||||||||||||||||||||||||
| from capa.features.extractors.ts.query import TS_LANGUAGES | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def is_script(buf: bytes) -> bool: | ||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||
| return bool(get_language_ts(buf)) | ||||||||||||||||||||||||||||
| except ValueError: | ||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _parse(ts_language: Language, buf: bytes) -> Optional[Tree]: | ||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||
| parser = Parser(ts_language) | ||||||||||||||||||||||||||||
| return parser.parse(buf) | ||||||||||||||||||||||||||||
| except ValueError: | ||||||||||||||||||||||||||||
| return None | ||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+36
Contributor
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. In
Suggested change
Collaborator
Author
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. Parser constructor accepts Language argument. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def _contains_errors(ts_language, node: Node) -> bool: | ||||||||||||||||||||||||||||
| query = Query(ts_language, "(ERROR) @error") | ||||||||||||||||||||||||||||
| return bool(QueryCursor(query).captures(node)) | ||||||||||||||||||||||||||||
|
Comment on lines
+39
to
+41
Contributor
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. Since
Suggested change
Collaborator
Author
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. QueryCursor exists in the latest version. Verified with the official documentation. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def get_language_ts(buf: bytes) -> str: | ||||||||||||||||||||||||||||
| for language, ts_language in TS_LANGUAGES.items(): | ||||||||||||||||||||||||||||
| tree = _parse(ts_language, buf) | ||||||||||||||||||||||||||||
| if tree and not _contains_errors(ts_language, tree.root_node): | ||||||||||||||||||||||||||||
| return language | ||||||||||||||||||||||||||||
| raise ValueError("failed to parse the language") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def get_template_language_ts(buf: bytes) -> str: | ||||||||||||||||||||||||||||
| for language, ts_language in TS_LANGUAGES.items(): | ||||||||||||||||||||||||||||
| if language in [LANG_TEM, LANG_HTML]: | ||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||
| tree = _parse(ts_language, buf) | ||||||||||||||||||||||||||||
| if tree and not _contains_errors(ts_language, tree.root_node): | ||||||||||||||||||||||||||||
| return language | ||||||||||||||||||||||||||||
| raise ValueError("failed to parse the language") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def get_language_from_ext(path: str) -> str: | ||||||||||||||||||||||||||||
| if path.endswith(EXT_ASPX): | ||||||||||||||||||||||||||||
| return LANG_TEM | ||||||||||||||||||||||||||||
| if path.endswith(EXT_CS): | ||||||||||||||||||||||||||||
| return LANG_CS | ||||||||||||||||||||||||||||
| if path.endswith(EXT_HTML): | ||||||||||||||||||||||||||||
| return LANG_HTML | ||||||||||||||||||||||||||||
| if path.endswith(EXT_PY): | ||||||||||||||||||||||||||||
| return LANG_PY | ||||||||||||||||||||||||||||
| raise ValueError(f"{path} has an unrecognized or an unsupported extension.") | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| def get_language(path: Path) -> str: | ||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||
| with path.open("rb") as f: | ||||||||||||||||||||||||||||
| buf = f.read() | ||||||||||||||||||||||||||||
| return get_language_ts(buf) | ||||||||||||||||||||||||||||
| except ValueError: | ||||||||||||||||||||||||||||
| return get_language_from_ext(str(path)) | ||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
tree-sitter>= 0.21.0,QueryCursorhas been completely removed. Executing queries is now done directly viaQuery.capturesorLanguage.query. ImportingQueryCursorwill raise anImportErrorat runtime.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QueryCursor exists in the latest version. Verified with the official documentation.
Link - https://tree-sitter.github.io/py-tree-sitter/