Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions desktop/cmd/lithe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import (
)

func main() {
// Git and SSH run this program as the AskPass helper of a Git command the user started in
// Lithe. The helper only relays one question to that window and prints the answer, so it
// returns before any window, log, store or login environment exists.
if prompt, helper := core.GitAskPassRequest(os.Args); helper {
os.Exit(core.GitAskPass(prompt))
}
// AppKit needs the process's main thread for the application and its windows. Pin the
// main goroutine before any setup that can yield, so the scheduler cannot move it.
runtime.LockOSThread()
Expand Down
11 changes: 11 additions & 0 deletions desktop/internal/core/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ import (
"unsafe"
)

// GitAskPass runs this process as the AskPass helper GitAskPassRequest detected: Core sends
// prompt to the Lithe request that started Git, waits for the user's answer, prints it on
// standard output for Git or SSH and returns the exit status, 0 when it printed an answer and 1
// when there is none (cancelled, expired or refused). Call it first in main and exit with its
// result: a helper process opens no window, log or store.
func GitAskPass(prompt string) int {
cPrompt := C.CString(prompt)
defer C.free(unsafe.Pointer(cPrompt))
return int(C.lithe_core_git_askpass(cPrompt))
}

// classifyTextBytes borrows the Go byte slice only for the synchronous Core
// call. The slice contains no pointers, and Core neither retains nor mutates it.
func classifyTextBytes(data []byte) int {
Expand Down
15 changes: 14 additions & 1 deletion desktop/internal/core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ type gitExecution struct {
// (WithProjectCodeAllowed): Core starts no Git process for it and answers
// CodeGitUntrustedWorkspace. Nil keeps Core's default, which allows Git.
ProjectCodeAllowed *bool `json:"projectCodeAllowed,omitempty"`
// Interactive lets the Git processes the user sees ask for credentials through Lithe
// (WithGitAuthentication). Core then requires the request to observe execution events,
// which carry the questions.
Interactive bool `json:"interactive,omitempty"`
}

// responseEnvelope is the response the kernel encodes (CoreResponse in the Rust protocol).
Expand Down Expand Up @@ -110,11 +114,12 @@ func (c *Client) execute(ctx context.Context, command string, payload, result an
payload = struct{}{}
}
operationID := fmt.Sprintf("desktop-%d", nextCoreOperation.Add(1))
execution := gitExecutionOf(ctx, command)
request, err := json.Marshal(requestEnvelope{
OperationID: operationID,
Command: command,
Payload: payload,
GitExecution: gitExecutionOf(ctx, command),
GitExecution: execution,
Origin: requestOriginOf(ctx),
})
if err != nil {
Expand All @@ -132,6 +137,14 @@ func (c *Client) execute(ctx context.Context, command string, payload, result an
}
// A Git request of a context with a console is observed so its commands are recorded.
observer = gitConsoleObserver(ctx, command, request, observer)
if execution != nil && execution.Interactive {
// The questions of an interactive request arrive as its events, and Core refuses one
// that observes none. Closing returned tells each open question that no answer reaches
// its request any more.
returned := make(chan struct{})
defer close(returned)
observer = gitAuthenticationObserver(gitAuthenticationOf(ctx, command), returned, observer)
}
if observer == nil {
return nil, decodeResponse(ctx, command, call(ctx, operationID, request), result)
}
Expand Down
135 changes: 135 additions & 0 deletions desktop/internal/core/gitauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package core

import (
"bytes"
"context"
"encoding/json"
"os"
"strings"
)

// Git authentication: Core lets a Git process the user started call this program as its
// AskPass helper (GIT_ASKPASS, SSH_ASKPASS). The helper process forwards the question over a
// channel only this account can reach to the request that started Git, which reports it as an
// execution event; the host asks the user and answers with git.authRespond. Core redacts the
// question, keeps the answer out of logs and zeroes its copies once Git has read it.

// GitAuthChallenge is a question a running Git request asks: a user name, a password, token or
// passphrase (Secret), or, after a transfer failed to authenticate, whether to retry it once
// with credentials typed in Lithe instead of the credential helper (Retry).
type GitAuthChallenge struct {
// RequestID names the challenge to GitAuthRespond.
RequestID string
// Prompt is Git's or SSH's own question with Core's redactions applied. It is upstream text,
// shown as it is. A Retry challenge carries Core's English wording, which the host replaces
// with its own translated question.
Prompt string
// Secret says the answer must not be shown or kept.
Secret bool
// Retry is the question whether to retry the transfer; only GitRetryAnswer accepts it.
Retry bool
// Attempt counts, from 1, how often the Git process asked the same question. A Retry
// challenge carries the number of the transfer a retry would start instead, from 2.
Attempt int
// WorkingDirectory is the directory the Git process runs in.
WorkingDirectory string
// Done is closed once the request that asked has returned; no answer reaches it after that.
Done <-chan struct{}
}

// GitRetryAnswer is the answer to a Retry challenge that retries the transfer.
const GitRetryAnswer = "retry"

type gitAuthenticationKey struct{}

// WithGitAuthentication returns a context whose Git requests may ask the user for credentials:
// Core lets the Git processes the user sees (not a request's internal queries) use this program
// as their AskPass helper, and each question reaches prompt. prompt runs on the request's
// goroutine and must return promptly, posting the question to the UI thread; the Git process
// waits for GitAuthRespond, for at most Core's prompt deadline, and fails authentication
// without an answer. Give it only to operations the user started: an automatic refresh must
// fail instead of asking.
func WithGitAuthentication(ctx context.Context, prompt func(GitAuthChallenge)) context.Context {
return context.WithValue(ctx, gitAuthenticationKey{}, prompt)
}

// gitAuthenticationOf is the prompt of a request of command, nil when ctx has none or command
// starts no Git process.
func gitAuthenticationOf(ctx context.Context, command string) func(GitAuthChallenge) {
if !strings.HasPrefix(command, "git.") || gitConsoleUnrecorded[command] {
return nil
}
prompt, _ := ctx.Value(gitAuthenticationKey{}).(func(GitAuthChallenge))
return prompt
}

// gitAuthenticationEvent is the execution event of a question Git asked.
var gitAuthenticationEvent = []byte(`"authentication"`)

// gitAuthenticationObserver hands each authentication event of a request to prompt, with done
// closed when the request returns, and passes every event on to next.
func gitAuthenticationObserver(prompt func(GitAuthChallenge), done <-chan struct{}, next func(json.RawMessage)) func(json.RawMessage) {
return func(raw json.RawMessage) {
// Git prints many output lines and asks rarely; only a line naming the kind is decoded.
if bytes.Contains(raw, gitAuthenticationEvent) {
var event struct {
Type string `json:"type"`
RequestID string `json:"requestId"`
Prompt string `json:"prompt"`
Secret bool `json:"secret"`
Retry bool `json:"retry"`
Attempt int `json:"attempt"`
WorkingDirectory string `json:"workingDirectory"`
}
if json.Unmarshal(raw, &event) == nil && event.Type == "authentication" && event.RequestID != "" {
prompt(GitAuthChallenge{
RequestID: event.RequestID,
Prompt: event.Prompt,
Secret: event.Secret,
Retry: event.Retry,
Attempt: event.Attempt,
WorkingDirectory: event.WorkingDirectory,
Done: done,
})
}
}
if next != nil {
next(raw)
}
}
}

// gitAuthReply is the payload of git.authRespond. A nil Answer cancels the question.
type gitAuthReply struct {
RequestID string `json:"requestId"`
Answer *string `json:"answer"`
}

// GitAuthRespond answers the challenge requestID with the text the user typed, GitRetryAnswer to
// retry, or nil to cancel. accepted is false when the challenge no longer waits: it was already
// answered, it expired, its request returned, or Core refused the answer (longer than 8192 bytes,
// or containing NUL, CR or LF). The answer is never logged. Core answers at once, so the call
// may run on any goroutine, but not on the UI thread.
func (c *Client) GitAuthRespond(ctx context.Context, requestID string, answer *string) (accepted bool, err error) {
var result struct {
Accepted bool `json:"accepted"`
}
err = c.Execute(ctx, "git.authRespond", gitAuthReply{RequestID: requestID, Answer: answer}, &result)
return result.Accepted, err
}

// gitAskPassModeVariable is set by Core in the environment of the helper it names to Git.
const gitAskPassModeVariable = "LITHE_GIT_ASKPASS_MODE"

// GitAskPassRequest reports whether this process was started as the AskPass helper of an
// interactive Git process, and the question Git or SSH passed as its first argument. args is
// os.Args.
func GitAskPassRequest(args []string) (prompt string, helper bool) {
if os.Getenv(gitAskPassModeVariable) != "1" {
return "", false
}
if len(args) > 1 {
prompt = args[1]
}
return prompt, true
}
Loading
Loading