Skip to content

Feat: Increase dynamic API key expiration#10328

Merged
Meldiron merged 1 commit into1.7.xfrom
fix-dynamic-key-expiration
Aug 15, 2025
Merged

Feat: Increase dynamic API key expiration#10328
Meldiron merged 1 commit into1.7.xfrom
fix-dynamic-key-expiration

Conversation

@Meldiron
Copy link
Copy Markdown
Contributor

What does this PR do?

Ensures dynamic api keys work woth a bit of leeway, useful when timeouts are set to be very short, like 5 seconds, but cold-starts could take some of that time

Test Plan

existing tests should pass

Related PRs and Issues

x

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Aug 15, 2025

📝 Walkthrough

Walkthrough

The changes add a uniform 60-second buffer to JWT expiration calculations across multiple paths. In app/controllers/general.php, src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php, and src/Appwrite/Platform/Workers/Functions.php, jwtExpiry is now computed as the function timeout plus 60 seconds. This applies to both user-session and API-key JWT generation paths. No other logic, control flow, or public API signatures were modified.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-dynamic-key-expiration

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions
Copy link
Copy Markdown

Security Scan Results for PR

Docker Image Scan Results

Package Version Vulnerability Severity
binutils 2.42-r0 CVE-2025-0840 HIGH
git 2.45.3-r0 CVE-2025-48384 HIGH
git 2.45.3-r0 CVE-2025-48385 HIGH
git-init-template 2.45.3-r0 CVE-2025-48384 HIGH
git-init-template 2.45.3-r0 CVE-2025-48385 HIGH
icu 74.2-r0 CVE-2025-5222 HIGH
icu-data-en 74.2-r0 CVE-2025-5222 HIGH
icu-dev 74.2-r0 CVE-2025-5222 HIGH
icu-libs 74.2-r0 CVE-2025-5222 HIGH
libexpat 2.6.4-r0 CVE-2024-8176 HIGH
libxml2 2.12.7-r0 CVE-2024-56171 HIGH
libxml2 2.12.7-r0 CVE-2025-24928 HIGH
libxml2 2.12.7-r0 CVE-2025-27113 HIGH
libxml2 2.12.7-r0 CVE-2025-32414 HIGH
libxml2 2.12.7-r0 CVE-2025-32415 HIGH
pyc 3.12.9-r0 CVE-2024-12718 HIGH
pyc 3.12.9-r0 CVE-2025-4138 HIGH
pyc 3.12.9-r0 CVE-2025-4330 HIGH
pyc 3.12.9-r0 CVE-2025-4517 HIGH
python3 3.12.9-r0 CVE-2024-12718 HIGH
python3 3.12.9-r0 CVE-2025-4138 HIGH
python3 3.12.9-r0 CVE-2025-4330 HIGH
python3 3.12.9-r0 CVE-2025-4517 HIGH
python3-pyc 3.12.9-r0 CVE-2024-12718 HIGH
python3-pyc 3.12.9-r0 CVE-2025-4138 HIGH
python3-pyc 3.12.9-r0 CVE-2025-4330 HIGH
python3-pyc 3.12.9-r0 CVE-2025-4517 HIGH
python3-pycache-pyc0 3.12.9-r0 CVE-2024-12718 HIGH
python3-pycache-pyc0 3.12.9-r0 CVE-2025-4138 HIGH
python3-pycache-pyc0 3.12.9-r0 CVE-2025-4330 HIGH
python3-pycache-pyc0 3.12.9-r0 CVE-2025-4517 HIGH
sqlite-libs 3.45.3-r1 CVE-2025-29087 HIGH
xz 5.6.2-r0 CVE-2025-31115 HIGH
xz-libs 5.6.2-r0 CVE-2025-31115 HIGH
golang.org/x/crypto v0.31.0 CVE-2025-22869 HIGH
golang.org/x/oauth2 v0.24.0 CVE-2025-22868 HIGH
stdlib 1.22.10 CVE-2025-47907 HIGH

Source Code Scan Results

🎉 No vulnerabilities found!

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

🔭 Outside diff range comments (1)
src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php (1)

208-214: Apply a 1min (+60s) buffer to all short‑lived JWT issuances (cold‑start protection)

Short note: the repo scan shows several JWTs already use timeout+60, but multiple JWT constructions do not — please either add the +60 buffer where the token TTL is tied to a function/resource timeout (or confirm the current TTLs are intentional).

Files/locations that need attention (from scan):

  • src/Appwrite/Platform/Modules/Functions/Workers/Builds.php
    • ~lines 586-587: $jwtExpiry = (int)System::getEnv('_APP_COMPUTE_BUILD_TIMEOUT', 900); new JWT(..., $jwtExpiry, 0); — add +60.
    • ~line 918: new JWT(..., 900, 0); — change to use 900 + 60 (or a named $jwtExpiry +60).
  • src/Appwrite/Platform/Workers/Migrations.php:225
    • new JWT(..., 86400, 0) — review (long‑lived API key; if intentional, no change; otherwise apply buffer).
  • app/controllers/api/account.php:2724
    • new JWT(..., 900, 0) — verify whether this session JWT should include the +60 buffer.
  • app/controllers/api/users.php:2464
    • new JWT(..., $duration, 0) — confirm what $duration represents and add buffer if it maps to a short‑lived timeout.
  • app/controllers/api/projects.php:1761
    • new JWT(..., $duration, 0) — same as above.
  • src/Appwrite/Auth/Key.php:127-130
    • API key / dynamic key JWT construction — confirm TTL semantics and add buffer if applicable.
  • Tests and test helpers (examples: tests/e2e/... and tests/unit/...) create JWTs with 900 — update tests if you change production TTL behavior.

Files already using the +60 buffer (for reference):

  • src/Appwrite/Platform/Workers/Functions.php (lines ~104 and ~393)
  • src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php (lines ~208 and ~217)
  • app/controllers/general.php (line ~364)

Suggested minimal change example (Builds.php):

  • Before:
    $jwtExpiry = (int)System::getEnv('_APP_COMPUTE_BUILD_TIMEOUT', 900);
    $jwtObj = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', $jwtExpiry, 0);
  • After:
    $jwtExpiry = (int)System::getEnv('_APP_COMPUTE_BUILD_TIMEOUT', 900) + 60; // 1min extra to account for possible cold‑starts
    $jwtObj = new JWT(System::getEnv('_APP_OPENSSL_KEY_V1'), 'HS256', $jwtExpiry, 0);

Action requested: apply the +60 buffer to the short‑lived JWT issuances above (or confirm why each omission is intentional).

🧹 Nitpick comments (5)
app/controllers/general.php (1)

364-371: Adding a 60s leeway to dynamic API key expiry is sensible for cold starts

The buffer aligns token validity with real-world startup delays and should reduce spurious expirations on short timeouts.

To avoid magic numbers and keep consistency across all call sites that build dynamic JWTs, consider centralizing the buffer:

-        $jwtExpiry = $resource->getAttribute('timeout', 900) + 60; // 1min extra to account for possible cold-starts
+        $buffer = (int) System::getEnv('_APP_DYNAMIC_JWT_BUFFER_SECONDS', 60);
+        $jwtExpiry = (int) $resource->getAttribute('timeout', 900) + $buffer; // buffer to account for possible cold-starts
src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php (2)

208-214: User-session JWT: 60s buffer looks good

This should prevent premature expiry when functions cold-start. No functional issues spotted.

To reduce duplication and magic numbers, consider using an env-configured buffer:

-                $jwtExpiry = $function->getAttribute('timeout', 900) + 60; // 1min extra to account for possible cold-starts
+                $buffer = (int) System::getEnv('_APP_DYNAMIC_JWT_BUFFER_SECONDS', 60);
+                $jwtExpiry = (int) $function->getAttribute('timeout', 900) + $buffer; // buffer to account for possible cold-starts

217-223: Dynamic API key JWT: consistent 60s buffer

Matches the user JWT path; good consistency within this file.

Consider centralizing the leeway value and applying light input sanitation:

-        $jwtExpiry = $function->getAttribute('timeout', 900) + 60; // 1min extra to account for possible cold-starts
+        $buffer = (int) System::getEnv('_APP_DYNAMIC_JWT_BUFFER_SECONDS', 60);
+        $jwtExpiry = (int) $function->getAttribute('timeout', 900) + $buffer; // buffer to account for possible cold-starts
src/Appwrite/Platform/Workers/Functions.php (2)

104-109: User JWT path: +60s buffer is appropriate

This mirrors the HTTP path behavior and helps under cold-starts when JWT is minted on the worker side.

Consider pulling the buffer from an env var for consistency:

-            $jwtExpiry = $function->getAttribute('timeout', 900) + 60; // 1min extra to account for possible cold-starts
+            $buffer = (int) System::getEnv('_APP_DYNAMIC_JWT_BUFFER_SECONDS', 60);
+            $jwtExpiry = (int) $function->getAttribute('timeout', 900) + $buffer; // buffer to account for possible cold-starts

393-399: Dynamic API key JWT: +60s buffer matches the PR intent

Good alignment with the other code paths updating expiry to reduce flakes on short timeouts.

Avoid magic numbers and ensure future consistency:

-        $jwtExpiry = $function->getAttribute('timeout', 900) + 60; // 1min extra to account for possible cold-starts
+        $buffer = (int) System::getEnv('_APP_DYNAMIC_JWT_BUFFER_SECONDS', 60);
+        $jwtExpiry = (int) $function->getAttribute('timeout', 900) + $buffer; // buffer to account for possible cold-starts
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between bb407a6 and 23bc392.

⛔ Files ignored due to path filters (1)
  • composer.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • app/controllers/general.php (1 hunks)
  • src/Appwrite/Platform/Modules/Functions/Http/Executions/Create.php (2 hunks)
  • src/Appwrite/Platform/Workers/Functions.php (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Setup & Build Appwrite Image
  • GitHub Check: Setup & Build Appwrite Image
  • GitHub Check: scan

@github-actions
Copy link
Copy Markdown

✨ Benchmark results

  • Requests per second: 1,027
  • Requests with 200 status code: 184,876
  • P99 latency: 0.179347826

⚡ Benchmark Comparison

Metric This PR Latest version
RPS 1,027 959
200 184,876 172,665
P99 0.179347826 0.202217193

@Meldiron Meldiron merged commit 2ffdfd1 into 1.7.x Aug 15, 2025
40 checks passed
@stnguyen90 stnguyen90 deleted the fix-dynamic-key-expiration branch October 1, 2025 23:58
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