Skip to content
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

FEAT: add exports-prefix #103

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion blint/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def demangle_symbolic_name(symbol, lang=None, no_args=False):
.replace("$u27$", "'")
)
# In case of rust symbols, try and trim the hash part from the end of the symbols
if demangled_symbol.count("::") > 3:
if demangled_symbol.count("::") > 2:
last_part = demangled_symbol.split("::")[-1]
if len(last_part) == 17:
demangled_symbol = demangled_symbol.removesuffix(f"::{last_part}")
Expand Down
10 changes: 9 additions & 1 deletion blint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ def build_args():
dest="stdout_mode",
help="Print the SBOM to stdout instead of a file.",
)
sbom_parser.add_argument(
"--exports-prefix",
default=[],
action="extend",
nargs="+",
dest="exports_prefix",
help="prefixes for the exports to be included in the SBOM.",
)
return parser.parse_args()


Expand Down Expand Up @@ -182,7 +190,7 @@ def main():
sbom_output_dir = os.path.dirname(sbom_output)
if sbom_output_dir and not os.path.exists(sbom_output_dir):
os.makedirs(sbom_output_dir)
generate(src_dirs, sbom_output, args.deep_mode)
generate(src_dirs, sbom_output, args.deep_mode, args.exports_prefix)
# Default case
else:
if reports_dir and not os.path.exists(reports_dir):
Expand Down
51 changes: 45 additions & 6 deletions blint/sbom.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def default_metadata(src_dirs):
return metadata


def generate(src_dirs: list[str], output_file: str, deep_mode: bool) -> bool:
def generate(src_dirs: list[str], output_file: str, deep_mode: bool, export_prefixes: list[str]) -> bool:
"""
Generates an SBOM for the given source directories.

Expand Down Expand Up @@ -131,7 +131,7 @@ def generate(src_dirs: list[str], output_file: str, deep_mode: bool) -> bool:
)
for exe in exe_files:
progress.update(task, description=f"Processing [bold]{exe}[/bold]", advance=1)
components += process_exe_file(dependencies_dict, deep_mode, exe, sbom)
components += process_exe_file(dependencies_dict, deep_mode, exe, sbom, export_prefixes)
if android_files:
task = progress.add_task(
f"[green] Parsing {len(android_files)} android apps",
Expand Down Expand Up @@ -266,6 +266,7 @@ def process_exe_file(
deep_mode: bool,
exe: str,
sbom: CycloneDX,
export_prefixes: list[str] = None,
) -> list[Component]:
"""
Processes an executable file, extracts metadata, and generates a Software Bill of Materials.
Expand Down Expand Up @@ -346,7 +347,7 @@ def process_exe_file(
)
)
internal_functions = sorted(
{f["name"] for f in metadata.get("functions", []) if f["name"]}
{f["name"] for f in metadata.get("functions", []) if not any(f["name"].startswith(p) for p in export_prefixes)}
)
if internal_functions:
parent_component.properties.append(
Expand All @@ -355,8 +356,18 @@ def process_exe_file(
value=SYMBOL_DELIMITER.join(internal_functions),
)
)
export_functions = sorted(
{f["name"] for f in metadata.get("functions", []) if any(f["name"].startswith(p) for p in export_prefixes)}
)
if export_functions:
parent_component.properties.append(
Property(
name="internal:export_functions",
value=SYMBOL_DELIMITER.join(export_functions),
)
)
symtab_symbols = sorted(
{f["name"] for f in metadata.get("symtab_symbols", []) if f["name"]}
{f["name"] for f in metadata.get("symtab_symbols", []) if not any(f["name"].startswith(p) for p in export_prefixes)}
)
if symtab_symbols:
parent_component.properties.append(
Expand All @@ -365,16 +376,34 @@ def process_exe_file(
value=SYMBOL_DELIMITER.join(symtab_symbols),
)
)
all_imports = sorted({f["name"] for f in metadata.get("imports", [])})
exported_symtab_symbols = sorted(
{f["name"] for f in metadata.get("symtab_symbols", []) if any(f["name"].startswith(p) for p in export_prefixes)}
)
if exported_symtab_symbols:
parent_component.properties.append(
Property(
name="internal:exported_symtab_symbols",
value=SYMBOL_DELIMITER.join(exported_symtab_symbols),
)
)
all_imports = sorted({f["name"] for f in metadata.get("imports", []) if not any(f["name"].startswith(p) for p in export_prefixes)})
if all_imports:
parent_component.properties.append(
Property(
name="internal:imports",
value=SYMBOL_DELIMITER.join(all_imports),
)
)
all_exports = sorted({f["name"] for f in metadata.get("imports", []) if any(f["name"].startswith(p) for p in export_prefixes)})
if all_imports:
parent_component.properties.append(
Property(
name="internal:exports",
value=SYMBOL_DELIMITER.join(all_exports),
)
)
dynamic_symbols = sorted(
{f["name"] for f in metadata.get("dynamic_symbols", []) if f["name"]}
{f["name"] for f in metadata.get("dynamic_symbols", []) if not any(f["name"].startswith(p) for p in export_prefixes)}
)
if dynamic_symbols:
parent_component.properties.append(
Expand All @@ -383,6 +412,16 @@ def process_exe_file(
value=SYMBOL_DELIMITER.join(dynamic_symbols),
)
)
exported_dynamic_symbols = sorted(
{f["name"] for f in metadata.get("dynamic_symbols", []) if any(f["name"].startswith(p) for p in export_prefixes)}
)
if exported_dynamic_symbols:
parent_component.properties.append(
Property(
name="internal:exported_dynamic_symbols",
value=SYMBOL_DELIMITER.join(exported_dynamic_symbols),
)
)
if not sbom.metadata.component.components:
sbom.metadata.component.components = []
_add_to_parent_component(sbom.metadata.component.components, parent_component)
Expand Down