[all] Quality Improvement - Chunk 5 (#5541) #5548
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
fix(external-import): Comprehensive fixes for M-O connectors
Summary
Comprehensive review and fix of all external-import connectors from M to O, addressing deprecated API usage, exit code issues, error handling improvements, naming conventions, and code quality.
Connectors in scope:
📊 Statistics
🚨 Critical Fixes
Bug Fixes
datetime.utcfromtimestamp()inrun()datetime.fromtimestamp(timestamp, tz=timezone.utc)hashshadows built-inhash_valueexit(1)instead ofsys.exit(1)in main.pysys.exit(1)for consistencyimport sysin main.pyimport sysdatetime.utcnow()datetime.now(timezone.utc)datetime.utcfromtimestamp()in utilsdatetime.fromtimestamp(timestamp, tz=timezone.utc)sys.exit(0)after Exceptionsys.exit(1)print(e)instead of proper logging in maintraceback.print_exc()sys.exit(0)in main exceptionsys.exit(1)import tracebackimport tracebackdatetime.utcfromtimestamp()(18 occurrences)datetime.fromtimestamp(timestamp, tz=timezone.utc)timezoneimporttimezoneto datetime importdatetime.now()not timezone-awaredatetime.now(timezone.utc)datetime.fromtimestamp()not timezone-awaredatetime.fromtimestamp(..., tz=timezone.utc)exit(1)instead ofsys.exit(1)sys.exit(1)for consistencyexit(1)instead ofsys.exit(1)in main.pysys.exit(1)for consistencyimport sysin main.pyimport sysdatetime.now()not timezone-awaredatetime.now(timezone.utc)except:clause in config_loader.pyexcept Exception:create_AutonomousSystemuses PascalCasecreate_autonomous_system(snake_case)objectshadows built-inobjdatetime.now().astimezone()inconsistent patterndatetime.now(tz=pytz.UTC)["config", ""]for companies_file_url["config", "companies_file_url"]configparameter inget_config_variableconfigparameterzipshadows built-inzip_fileobjectshadows built-instix_objectif not "x" in ystyleif "x" not in y(2 occurrences)🎯 OpenCTI Modeling Fixes
STIX/OpenCTI Compliance Issues
self.identityassigned as tuple due to trailing commaIndicator.generate_id(hash_value)incorrect - ID should be from patternIndicator.generate_id(pattern)is_family=Truefor individual malware samplesis_family=False- samples are not malware familiesexcept:clauseexcept Exception:related-tofrom Artifact→Indicatorbased-onfrom Indicator→Artifact per STIX specwork_idinsend_stix2_bundle()callwork_id=work_idparameter[file:hashes.sha256 = ...]incorrect format[file:hashes.'SHA-256' = ...]per STIX specbased-onfrom Indicator→Malwareindicatesper OpenCTI modelingprint()statements for errorsself.helper.log_error()object_marking_refsnot a listself.tlp_marking.idto[self.tlp_marking.id]for consistencyobtain_location_name()never setslocation_typecreated_by_ref=self.authorpasses full objectself.author["id"]for correct referencewith open(...) as f:for proper resource cleanupwith open(...) as f:for proper resource cleanupwith open(...) as f:for proper resource cleanup.astimezone()uses local timezone instead of UTC.astimezone(datetime.timezone.utc)for consistency.astimezone()after UTC datetime creation.isoformat()directlyExplanation
Indicator ID Generation
Malware is_family Flag
Relationship Direction (Indicator → Observable)
STIX Hash Pattern Format
🐛 Bug Fixes
Deprecated DateTime APIs
Python 3.12 deprecated
datetime.utcnow()anddatetime.utcfromtimestamp(). These must be replaced with timezone-aware alternatives.Affected connectors: malcore, malpedia, misp-feed
Exit Code Corrections
Using
exit(0)orsys.exit(0)in exception handlers incorrectly signals successful termination. These must return non-zero exit codes.Affected connectors: malpedia, malwarebazaar-recent-additions
Code Consistency Fixes
Using bare
exit()instead ofsys.exit()is inconsistent and may not work correctly in all environments.Affected connectors: malcore, nti, mwdb
🔧 Code Quality
Naming Convention Fixes
core.pyhashhash_valuemalwarebazaar-recent-additions.pyrelation_shipcreate_relationshiputils.pycleanhtmlclean_htmlconverter_to_stix.pycreate_AutonomousSystemcreate_autonomous_systemopencsam.pyobjectobjorange-cyberdefense.pyzipzip_fileorange-cyberdefense.pyobjectstix_objectorange-cyberdefense.py_get_ranged_scored_get_ranged_scoreorange-cyberdefense.pyobject(param)stix_obj_process_objectparamorange-cyberdefense.pyobject(loop var)stix_obj_get_report_iocsloopSpelling & Grammar Fixes
base.pymwdb.pymwdb.pymapping.py# 补充修改的反诈(Chinese comment)# Supplementary fraud types📦 Connectors Modified
M (14 connectors)
hash→hash_value,exit(1)→sys.exit(1), MODELING: tuple bug fix, Indicator ID generation fix,is_family=Falsework_idparameter tosend_stix2_bundle()datetime.now()→datetime.now(timezone.utc)traceback.print_exc(), function rename, MODELING: bareexcept:fix, relationship direction/type fix (Indicator→Artifact, based-on)cleanhtml→clean_htmlexit(1)→sys.exit(1), MODELING: hash pattern format fix, relationship type fix (indicates), removed print statementsN (1 connector)
exit(1)→sys.exit(1),datetime.now()→datetime.now(timezone.utc), bareexcept:→except Exception:, function renamecreate_AutonomousSystem→create_autonomous_system, Chinese comment translated, MODELING: URL marking refs fix, location_type fix, created_by_ref fixNaming Convention Review (Pass 2): All functions, variables, classes, files, and constants in the NTI connector follow PEP 8 naming conventions. No additional naming issues found.
OpenCTI Modeling Review (Pass 3): Fixed 3 STIX/OpenCTI compliance issues in converter_to_stix.py.
O (3 connectors)
object→obj,datetime.now().astimezone()→datetime.now(tz=pytz.UTC), MODELING: config file context manager fixconfigparameter inget_config_variablefor companies_file_url, MODELING: config file context manager fixzip→zip_file, variableobject→stix_object, PEP 8not instyle (2 occurrences), function_get_ranged_scored→_get_ranged_score, parameterobject→stix_objin_process_object, loop variableobject→stix_objin_get_report_iocs, MODELING: config file context manager fix, timezone consistency fixes (4 occurrences)Naming Convention Review (Pass 2): All functions, variables, classes, files, and constants in opencsam and opencti connectors follow PEP 8 naming conventions. No additional naming issues found.
OpenCTI Modeling Review (Pass 3): Fixed file handle leaks (3 connectors) and timezone inconsistencies (4 occurrences in orange-cyberdefense).
📝 Files Changed Breakdown
src/malcore/core.py,src/main.pysrc/malpedia_connector/connector.py,src/malpedia_services/utils.pysrc/main.pysrc/malwarebazaar_connector/connector.pysrc/malwarebazaar-recent-additions.pysrc/connector/base.py,src/connector/utils.py,src/connector/reports.pysrc/connector/connector.pysrc/mwdb.pysrc/main.py,src/nti/connector.py,src/nti/config_loader.py,src/nti/converter_to_stix.py,src/nti/mapping.pysrc/opencsam.pysrc/connector.pysrc/orange-cyberdefense.pyConnectors Not Modified
montysecurity-c2-tracker: This connector uses direct OpenCTI API calls instead of the standard connector helper pattern. It requires a more significant refactor to align with OpenCTI connector best practices. The connector:
print()statements for all loggingOpenCTIApiClientinstead of usingOpenCTIConnectorHelperloop()function for error recoverymicrosoft-defender-incidents: Already uses proper timezone-aware datetime in main connector code.
microsoft-sentinel-incidents: Already uses proper timezone-aware datetime.
misp: Already uses modern datetime practices with
datetime.now(tz=timezone.utc).mitre, mitre-atlas: Already reviewed and use proper patterns.
Breaking Changes
None. All changes are backwards compatible.
File Naming
No file renames were performed. The following files use hyphenated names which is acceptable for main entry point scripts:
malwarebazaar-recent-additions/src/malwarebazaar-recent-additions.pyorange-cyberdefense/src/orange-cyberdefense.py✅ Testing Checklist
black .andisort --profile=black .pass on modified files