Add dynamic random string generator for modules
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
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.
- Templates now support dynamic runtime placeholders (e.g.,
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 aValueErroras 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.
Comment @coderabbitai help to get the list of available commands and usage tips.
@securestep9 Can you please review the PR
@codex review this PR using skill: https://github.com/nettacker-codex-ai/skills/blob/main/pr-review-recommender/SKILL.md