Skip to content
This repository was archived by the owner on Mar 17, 2026. It is now read-only.

fix(engines): reduce valid token chars#114

Merged
jyecusch merged 1 commit intomainfrom
NIT-417
Oct 13, 2025
Merged

fix(engines): reduce valid token chars#114
jyecusch merged 1 commit intomainfrom
NIT-417

Conversation

@tjholm
Copy link
Copy Markdown
Member

@tjholm tjholm commented Oct 10, 2025

No description provided.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Oct 10, 2025

📝 Walkthrough

Walkthrough

The PR tightens Terraform token regexes in engines/terraform/token.go to require the first character after the prefix be a letter, underscore, or dash, with subsequent characters limited to letters, digits, underscores, dashes, or dots. Tests in engines/terraform/resolve_test.go were updated: TestResolveStringInterpolation now accepts a setupGlobalVars map and an expectedTokens slice, populates td.terraformVariables from setupGlobalVars, extracts token strings from TokenMatch results and asserts token sequences, and adds "string with coalesce function" and "string with ternary operator" scenarios.

Possibly related PRs

Suggested labels

released

Suggested reviewers

  • jyecusch
  • davemooreuws
  • HomelessDinosaur

Pre-merge checks

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning There is no pull request description provided, so reviewers lack any context or overview of the changes, making the description completely uninformative. Please add a description summarizing the adjustments to the token regular expressions and associated test updates to give reviewers clear guidance on the purpose and scope of this pull request.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly captures the primary change of tightening valid token characters in the engines module and follows the conventional commit format without extraneous details, making it clear and focused.

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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
engines/terraform/token.go (1)

48-49: Approve regex changes. Stricter pattern validated against existing usage; no broken tokens detected. Consider consolidating duplicate tokenPattern and allTokensPattern.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e31d3c5 and 99a7077.

📒 Files selected for processing (2)
  • engines/terraform/resolve_test.go (1 hunks)
  • engines/terraform/token.go (1 hunks)
🔇 Additional comments (1)
engines/terraform/resolve_test.go (1)

449-457: LGTM! Test validates coalesce with multiple tokens.

The test case correctly exercises token interpolation with Terraform's coalesce function and ensures the stricter regex still handles this use case.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
engines/terraform/token.go (1)

48-49: Unify identical regex compilation

tokenPattern and allTokensPattern both compile the same pattern—use a single package-level regex for both single- and multi-match use cases.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1593676 and bc3286c.

📒 Files selected for processing (2)
  • engines/terraform/resolve_test.go (6 hunks)
  • engines/terraform/token.go (1 hunks)
🔇 Additional comments (5)
engines/terraform/resolve_test.go (4)

399-405: LGTM! Test struct improvements enhance validation.

The addition of setupGlobalVars and expectedTokens fields makes the tests more comprehensive by explicitly validating token extraction alongside resolution.


456-466: Good addition: coalesce function test case.

This test validates that tokens can be extracted from within function calls like coalesce(self.project_id, var.project_id), ensuring the regex handles complex interpolation scenarios.


480-487: LGTM! Global variables setup is correct.

The nil-check and initialization of td.terraformVariables before populating from setupGlobalVars prevents nil pointer dereferences.


498-504: LGTM! Token extraction and assertion logic is sound.

The extraction of token strings from TokenMatch results and comparison against expectedTokens provides clear validation that the regex patterns are working as intended.

engines/terraform/token.go (1)

48-49: Tightened regex is safe: no tokens start with digits or dots after prefix.

Copy link
Copy Markdown
Contributor

@sean-nitric sean-nitric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would also be cool to have a ternary test case
${self.project_id == null ? self.project_id : var.project_id}

Copy link
Copy Markdown
Member

@raksiv raksiv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - The regex fix doesn't change how ternaries work, so probably not required unless you're wanting to add tests outside the scope of the change.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
engines/terraform/token.go (1)

48-49: Consider consolidating duplicate regex patterns.

Both tokenPattern and allTokensPattern are identical. If they're meant to remain the same, consider using a single compiled pattern or adding a comment explaining why they're separate.

Apply this diff if consolidation is desired:

+var tokenPatternRegex = regexp.MustCompile(`((?:infra|var|self)\.[a-zA-Z_\-][a-zA-Z0-9_\-\.]*)`)
-var tokenPattern = regexp.MustCompile(`((?:infra|var|self)\.[a-zA-Z_\-][a-zA-Z0-9_\-\.]*)`)
-var allTokensPattern = regexp.MustCompile(`((?:infra|var|self)\.[a-zA-Z_\-][a-zA-Z0-9_\-\.]*)`)
+var tokenPattern = tokenPatternRegex
+var allTokensPattern = tokenPatternRegex
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bc3286c and 5714fa3.

📒 Files selected for processing (2)
  • engines/terraform/resolve_test.go (6 hunks)
  • engines/terraform/token.go (1 hunks)
🔇 Additional comments (9)
engines/terraform/token.go (2)

48-49: Regex logic correctly implements tightened token requirements.

The patterns correctly enforce that the first character after the prefix must be a letter, underscore, or dash, with subsequent characters limited to letters, digits, underscores, dashes, or dots. This prevents tokens like self.123var or self..nested.

Note: The escaping of - and . inside character classes (\- and \.) is unnecessary but harmless—inside brackets, . is literal by default, and - at the end of a class is unambiguous.


48-49: Regex validation tightened and verified
The updated patterns enforce stricter token rules without breaking existing usage; tests for invalid tokens (e.g., self., var.) pass as expected.

engines/terraform/resolve_test.go (7)

399-405: Test structure improvements enhance validation precision.

The addition of expectedTokens and setupGlobalVars fields allows more precise validation of token extraction behavior and enables testing of both self.* and var.* references in the same test case.


455-478: Valuable test coverage for complex expressions.

The new test cases for coalesce functions and ternary operators validate that the tightened regex correctly extracts tokens from real-world Terraform expressions. These scenarios are critical for ensuring the regex changes don't break legitimate use cases.


508-516: Token sequence validation improves test accuracy.

Extracting and comparing token strings directly from TokenMatch results ensures the regex not only finds tokens but finds them in the correct order and with the correct values. This catches subtle ordering or extraction bugs.


399-405: LGTM!

Test structure correctly updated to support token sequence validation and global variable setup.


455-478: LGTM!

New test cases for coalesce functions and ternary operators provide valuable coverage for complex token extraction scenarios.


492-499: LGTM!

Global variable setup correctly enables testing of var.* references in the new test cases.


508-517: LGTM!

Token extraction assertion ensures the regex changes produce the expected token sequences.

@tjholm
Copy link
Copy Markdown
Member Author

tjholm commented Oct 12, 2025

Looks good - The regex fix doesn't change how ternaries work, so probably not required unless you're wanting to add tests outside the scope of the change.

Added the test case anyway as it doesn't hurt to have and will catch potential regressions if we need to refine this behaviour further.

@jyecusch jyecusch merged commit d915ba6 into main Oct 13, 2025
3 checks passed
@jyecusch jyecusch deleted the NIT-417 branch October 13, 2025 03:15
@nitric-bot
Copy link
Copy Markdown

🎉 This PR is included in version 0.1.23 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants