-
Notifications
You must be signed in to change notification settings - Fork 705
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
652b6cf
af4fd99
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 | ||
|
mr-tz marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def _contains_errors(ts_language, node: Node) -> bool: | ||
| query = Query(ts_language, "(ERROR) @error") | ||
| return bool(QueryCursor(query).captures(node)) | ||
|
mr-tz marked this conversation as resolved.
|
||
|
|
||
|
|
||
| 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 | ||
|
Collaborator
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. Embedded template is not immediately clear to me, some doc on that would be good (maybe it is further down).
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. I haven’t added documentation yet, but I’ll add a section describing the script analysis feature in the near future. |
||
| 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)) | ||
|
Collaborator
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. I guess the order is debatable here, so flagging for further discussion
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. My thinking here was to prefer Tree-sitter’s language detection when possible, since it can inspect the file contents rather than relying only on the extension. The extension fallback is mainly for cases where content-based detection fails. That said, I agree that the order is debatable. Happy to discuss more on this! |
||
Uh oh!
There was an error while loading. Please reload this page.