Skip to content

fix(templates): handle unlimited traffic limit in trafficLeft#185

Merged
kastov merged 1 commit into
remnawave:devfrom
mishkatik:fix/traffic-left-unlimited-template
Jun 17, 2026
Merged

fix(templates): handle unlimited traffic limit in trafficLeft#185
kastov merged 1 commit into
remnawave:devfrom
mishkatik:fix/traffic-left-unlimited-template

Conversation

@mishkatik

Copy link
Copy Markdown

No description provided.

@snyk-io

snyk-io Bot commented Jun 4, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@greptile-apps

greptile-apps Bot commented Jun 4, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes a display bug in the TemplateEngine where computing TRAFFIC_LEFT / TRAFFIC_LEFT_BYTES for users with unlimited traffic (represented internally as trafficLimitBytes === 0n) produced a large negative bigint rather than a meaningful value.

  • trafficLeft() now short-circuits to 0n when trafficLimitBytes === 0n, preventing negative traffic values from being rendered in subscription templates.
  • The non-unlimited branch (trafficLimitBytes > 0n) is unchanged, so if usedTrafficBytes ever exceeds trafficLimitBytes, the subtraction still yields a negative bigint — unlike DAYS_LEFT which already clamps to Math.max(0, …).

Confidence Score: 4/5

Safe to merge — the unlimited-traffic crash is fixed. The remaining gap (no clamp when used bytes exceed limit for non-unlimited users) is unlikely in normal operation but worth addressing.

The unlimited-traffic fix is correct. The non-unlimited subtraction can still return a negative bigint when usedTrafficBytes surpasses trafficLimitBytes, serializing a negative number into template strings like TRAFFIC_LEFT_BYTES. All other template values remain untouched.

src/common/utils/templates/replace-templates-values.ts — specifically the non-unlimited branch of trafficLeft()

Important Files Changed

Filename Overview
src/common/utils/templates/replace-templates-values.ts Adds unlimited-traffic guard to trafficLeft() (returns 0n when trafficLimitBytes===0n); non-unlimited case still produces a negative bigint if usedTrafficBytes exceeds trafficLimitBytes

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[trafficLeft called] --> B{trafficLimitBytes === 0n?}
    B -- Yes\nUnlimited --> C[return 0n]
    B -- No\nLimited --> D[trafficLimitBytes - usedTrafficBytes]
    D --> E{result < 0n?}
    E -- Yes\nover-limit --> F[⚠ returns negative bigint\ncurrent behaviour]
    E -- No --> G[return positive bigint]
    G --> H[prettyBytesUtil formats value]
    C --> H
Loading

Reviews (1): Last reviewed commit: "fix(templates): handle unlimited traffic..." | Re-trigger Greptile

Comment thread src/common/utils/templates/replace-templates-values.ts
@kastov kastov merged commit 8c184cf into remnawave:dev Jun 17, 2026
1 check passed
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.

2 participants