fix: test git connection with stored token#22
Conversation
The test connection button now works with the saved encrypted token instead of requiring users to re-enter it. Server decrypts the stored token when no explicit token is provided in the request.
Greptile SummaryThis PR fixes the "Test Connection" button so it works with a previously saved (encrypted) Git token — previously, users with a saved token but a cleared input field were blocked by a warning. The server-side change makes
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant UI as GitSyncSection
participant tRPC as testGitConnection
participant MW as Middleware
participant DB as PostgreSQL
participant Git as Git Remote
UI->>tRPC: mutate({ environmentId, repoUrl, branch, token? })
tRPC->>MW: verify EDITOR access via environmentId
MW-->>tRPC: authorized
alt new access credential provided
tRPC->>tRPC: use input credential
else no credential in input
tRPC->>DB: findUnique(environmentId) select gitToken
DB-->>tRPC: encrypted value
tRPC->>tRPC: decrypt stored value
end
tRPC->>tRPC: embed credential in URL (percent-encoded)
tRPC->>Git: git clone --depth 1 --branch
alt clone succeeds
Git-->>tRPC: OK
tRPC-->>UI: { success: true }
else clone fails
Git-->>tRPC: error
tRPC->>tRPC: sanitize URL in error message
tRPC-->>UI: { success: false, error: sanitized }
end
MW->>DB: writeAuditLog (sensitive fields redacted)
Last reviewed commit: 4291c04 |
| if (!env?.gitToken) { | ||
| return { success: false, error: "No access token configured" }; | ||
| } | ||
| resolvedToken = decrypt(env.gitToken); |
There was a problem hiding this comment.
decrypt() call is outside the try/catch block
If the stored gitToken value is corrupted, truncated, or was encrypted with a different NEXTAUTH_SECRET, decrypt() will throw an Error. Since this call sits outside the try/catch block that wraps the git clone, the exception propagates as a raw tRPC INTERNAL_SERVER_ERROR rather than the { success: false, error: "..." } shape that the rest of this procedure returns. The client's onSuccess handler is never reached, so the user sees a generic "Connection test failed" toast with no useful description.
Wrap the decryption in a try/catch to keep the error path consistent:
| resolvedToken = decrypt(env.gitToken); | |
| resolvedToken = (() => { | |
| try { | |
| return decrypt(env.gitToken); | |
| } catch { | |
| return null; | |
| } | |
| })(); | |
| if (!resolvedToken) { | |
| return { success: false, error: "Stored token could not be decrypted. Please re-save your token." }; | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/server/routers/environment.ts
Line: 163
Comment:
**`decrypt()` call is outside the try/catch block**
If the stored `gitToken` value is corrupted, truncated, or was encrypted with a different `NEXTAUTH_SECRET`, `decrypt()` will throw an `Error`. Since this call sits outside the `try/catch` block that wraps the git clone, the exception propagates as a raw tRPC `INTERNAL_SERVER_ERROR` rather than the `{ success: false, error: "..." }` shape that the rest of this procedure returns. The client's `onSuccess` handler is never reached, so the user sees a generic "Connection test failed" toast with no useful description.
Wrap the decryption in a try/catch to keep the error path consistent:
```suggestion
resolvedToken = (() => {
try {
return decrypt(env.gitToken);
} catch {
return null;
}
})();
if (!resolvedToken) {
return { success: false, error: "Stored token could not be decrypted. Please re-save your token." };
}
```
How can I resolve this? If you propose a fix, please make it concise.
Summary
tokenoptional intestGitConnectioninput, fall back to decrypting storedgitTokenfrom environmentTest Plan