-
Notifications
You must be signed in to change notification settings - Fork 714
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
95be027
2ec421b
9bab3be
6efd145
7d5edd3
3537a11
1f790be
9f3c4b9
4741b0a
25f7413
013a011
80d75af
77120fb
b147bbc
5229741
e5ff27e
70f8b75
0827c15
8ea817c
e3b7eb5
aba743d
b0e85c4
7919eb2
1152af9
3a535ee
9d9102b
a4ee12d
2147599
97075b5
d33f99d
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: | ||
| return get_language_from_ext(str(path)) | ||
| except ValueError: | ||
| with path.open("rb") as f: | ||
| buf = f.read() | ||
| return get_language_ts(buf) | ||
Uh oh!
There was an error while loading. Please reload this page.