Add a secure repository Prompts review surface and prompt metadata API
Hop-State: A_06FN6MRSHG4B64ZV13086G8 Hop-Proposal: R_06FN6MQDE14FFNR65H1D28G Hop-Task: T_06FN6J8ECE1Z9XQ5PQ3Z3D8 Hop-Attempt: AT_06FN6J8ECC1SC794JMZ0TF8
This commit is contained in:
@@ -78,3 +78,33 @@ func (c *Client) Repository(ctx context.Context, owner, name string) (Repository
|
||||
}
|
||||
return repo, nil
|
||||
}
|
||||
|
||||
// ViewerCanReadRepository checks a browser's existing Gitea session before
|
||||
// returning prompt data. Prompts may contain sensitive intent, so the Hop API
|
||||
// must never turn a private repository into a public metadata feed.
|
||||
func (c *Client) ViewerCanReadRepository(ctx context.Context, cookie, owner, name string) (bool, error) {
|
||||
if strings.TrimSpace(cookie) == "" {
|
||||
return false, nil
|
||||
}
|
||||
u := c.baseURL.JoinPath("api", "v1", "repos", owner, name)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
req.Header.Set("Cookie", cookie)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("check viewer repository access: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
return true, nil
|
||||
case http.StatusUnauthorized, http.StatusForbidden, http.StatusNotFound:
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("gitea viewer repository check returned %s", resp.Status)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,3 +42,41 @@ func TestRepositoryRequiresToken(t *testing.T) {
|
||||
t.Fatalf("error = %v, want %v", err, ErrTokenNotConfigured)
|
||||
}
|
||||
}
|
||||
|
||||
func TestViewerCanReadRepositoryForwardsSessionCookie(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/v1/repos/hop/private" {
|
||||
t.Fatalf("path = %q", r.URL.Path)
|
||||
}
|
||||
if got := r.Header.Get("Cookie"); got != "i_like_gitea=session" {
|
||||
t.Fatalf("cookie = %q", got)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
allowed, err := client.ViewerCanReadRepository(context.Background(), "i_like_gitea=session", "hop", "private")
|
||||
if err != nil || !allowed {
|
||||
t.Fatalf("allowed = %t, err = %v", allowed, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestViewerCannotReadMissingRepository(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewClient(server.URL, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
allowed, err := client.ViewerCanReadRepository(context.Background(), "i_like_gitea=session", "hop", "missing")
|
||||
if err != nil || allowed {
|
||||
t.Fatalf("allowed = %t, err = %v", allowed, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user