Skip to content

fix(payment): validate subscription periods and tighten amount parsing#330

Open
faisalnugroho wants to merge 1 commit into
base:masterfrom
faisalnugroho:fix/validate-subscription-periods
Open

fix(payment): validate subscription periods and tighten amount parsing#330
faisalnugroho wants to merge 1 commit into
base:masterfrom
faisalnugroho:fix/validate-subscription-periods

Conversation

@faisalnugroho

Copy link
Copy Markdown

Closes #314
Closes #313

Changes

1. Tightened validateStringAmount() (#313)

  • Added regex validation (/^-?\d+(\.\d+)?$/) before parseFloat() to reject malformed strings like "1abc", "1.2.3", "1foo"
  • Added isFinite() check to reject Infinity values
  • Trimmed input before validation for consistency

2. Added period validation functions (#314)

  • validatePeriodInDays() — rejects zero, negative, non-integer, NaN, and Infinity values
  • validatePeriodInSeconds() — same validation for seconds-based periods

3. Applied period validation in subscribe()

  • Validates periodInDays before creating spend permission typed data
  • Validates overridePeriodInSecondsForTestnet when present (testnet mode)
  • Validation runs early, before wallet signing, to fail fast with clear error messages

Testing

// These now throw clear errors instead of reaching wallet signing:
await subscribe({ recurringCharge: "10", subscriptionOwner: "...", periodInDays: 0 });
// → Error: Invalid periodInDays: must be a positive integer (received 0)

await subscribe({ recurringCharge: "10", subscriptionOwner: "...", periodInDays: -5 });
// → Error: Invalid periodInDays: must be a positive integer (received -5)

await subscribe({ recurringCharge: "1abc", subscriptionOwner: "..." });
// → Error: Invalid amount: must be a valid decimal number

- Add validatePeriodInDays() and validatePeriodInSeconds() to reject
  zero, negative, non-integer, NaN, and Infinity values
- Fix validateStringAmount() to use regex before parseFloat(), preventing
  malformed strings like '1abc' or '1.2.3' from passing validation
- Apply period validation in subscribe() for both periodInDays and
  overridePeriodInSecondsForTestnet

Closes base#314
Closes base#313
@cb-heimdall

Copy link
Copy Markdown
Collaborator

🟡 Heimdall Review Status

Requirement Status More Info
Reviews 🟡 0/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 1
Sum 2

@faisalnugroho

Copy link
Copy Markdown
Author

Hi! I'd love to get this PR reviewed when you have a moment. This PR addresses validate subscription periods and tighten amount parsing. Happy to make any adjustments based on your feedback. Let me know if there's anything I can do to help move this forward.

@faisalnugroho

Copy link
Copy Markdown
Author

Hi! I'd love to get this PR reviewed when you have a moment. This PR addresses validate subscription periods and tighten amount parsing. Happy to make any adjustments based on your feedback. Thank you!

@cb-heimdall

Copy link
Copy Markdown
Collaborator

Review Error for kostyandragun1107-lang @ 2026-06-05 02:13:18 UTC
User failed mfa authentication, either user does not exist or public email is not set on your github profile. \ see go/mfa-help

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

subscribe() accepts zero, negative, or invalid subscription periods Bug: Payment amount validation accepts malformed numeric strings

3 participants