Nettacker icon indicating copy to clipboard operation
Nettacker copied to clipboard

Add dynamic random string generator for modules

Open Aarush289 opened this issue 9 months ago โ€ข 2 comments

Proposed change

This PR introduces support for dynamic placeholders {rand_str(n)} in module templates, allowing random strings of configurable length to be generated at runtime.

This is useful for implementing modules that require unpredictable parameters or tokens, such as cve_2025_64446, where static values may be filtered or cached by the target.

Type of change

  • [x] New core framework functionality

Checklist

  • [x] I've followed the contributing guidelines
  • [x] I've run make pre-commit, it didn't generate any changes
  • [x] I've run make test, all tests passed locally

Aarush289 avatar Feb 12 '26 20:02 Aarush289

Summary by CodeRabbit

  • New Features
    • Templates now support dynamic runtime placeholders (e.g., {rand_str(n)}); these are automatically replaced with generated random tokens when templates are processed.
    • Placeholder processing is applied before final template formatting, ensuring generated values are available to the formatted output.

Walkthrough

Adds runtime placeholder processing to TemplateLoader: a private _apply_dynamic_placeholders(content: str) method replaces patterns like {rand_str(n)} with generated random tokens, and format() now applies this processing before performing template string formatting.

Changes

Cohort / File(s) Summary
Template Placeholder Enhancement
nettacker/core/template.py
Adds _apply_dynamic_placeholders(self, content: str) -> str to replace {rand_str(n)} via common_utils.generate_random_token using re. Updates format() to run this helper on file content prior to .format(). Imports re and common_utils adjusted.

Estimated code review effort

๐ŸŽฏ 2 (Simple) | โฑ๏ธ ~12 minutes

๐Ÿšฅ Pre-merge checks | โœ… 3 | โŒ 1
โŒ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage โš ๏ธ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
โœ… Passed checks (3 passed)
Check name Status Explanation
Title check โœ… Passed The title clearly summarizes the main change: adding support for dynamic random string generation in module templates.
Description check โœ… Passed The description is directly related to the changeset, explaining the purpose of {rand_str(n)} placeholders and their use case in modules.
Merge Conflict Detection โœ… Passed โœ… No merge conflicts detected when merging into master

โœ๏ธ Tip: You can configure your own custom pre-merge checks in the settings.

โœจ Finishing touches
  • [ ] ๐Ÿ“ Generate docstrings
๐Ÿงช Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

No actionable comments were generated in the recent review. ๐ŸŽ‰

๐Ÿงน Recent nitpick comments
nettacker/core/template.py (1)

38-43: Silent capping may hide template authoring mistakes โ€” consider logging a warning or raising.

When a template specifies e.g. {rand_str(1024)}, the value is silently truncated to 256 characters. The module author gets no feedback that their requested length was ignored, which can make debugging difficult. A warning log (or a ValueError as previously suggested) would surface the problem early.

๐Ÿ›ก๏ธ Suggested improvement
+        import logging
+        logger = logging.getLogger(__name__)
+
         def _rand_str_replacer(match):
-            length = min(
-                int(match.group(1)),
-                256,  # maximum length allowed in rand_str
-            )
+            length = int(match.group(1))
+            if length > 256:
+                raise ValueError(
+                    f"rand_str length {length} exceeds maximum allowed length of 256"
+                )
             return common_utils.generate_random_token(length)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

โค๏ธ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Feb 12 '26 20:02 coderabbitai[bot]

@securestep9 Can you please review the PR

Aarush289 avatar Feb 16 '26 07:02 Aarush289

@codex review this PR using skill: https://github.com/nettacker-codex-ai/skills/blob/main/pr-review-recommender/SKILL.md

securestep9 avatar Jul 27 '26 22:07 securestep9