Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix!: use client ip when creating connection logs for workspace proxied app accesses#19788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ethanndickson merged 1 commit intomainfromethan/proxied-app-connection-log-fix
Sep 15, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
2 changes: 1 addition & 1 deletioncoderd/database/dump.sql
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
ALTER TABLE connection_logs ALTER COLUMN ip SET NOT NULL;
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
-- We can't guarantee that an IP will always be available, and omitting an IP
-- is preferable to not creating a connection log at all.
ALTERTABLE connection_logs ALTER COLUMN ip DROPNOT NULL;
2 changes: 1 addition & 1 deletioncodersdk/connectionlog.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,7 +20,7 @@ type ConnectionLog struct {
WorkspaceID uuid.UUID `json:"workspace_id" format:"uuid"`
WorkspaceName string `json:"workspace_name"`
AgentName string `json:"agent_name"`
IP netip.Addr`json:"ip"`
IP*netip.Addr `json:"ip,omitempty"`
Type ConnectionType `json:"type"`

// WebInfo is only set when `type` is one of:
Expand Down
8 changes: 7 additions & 1 deletionenterprise/coderd/connectionlog.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -93,7 +93,13 @@ func convertConnectionLogs(dblogs []database.GetConnectionLogsOffsetRow) []coder
}

funcconvertConnectionLog(dblog database.GetConnectionLogsOffsetRow) codersdk.ConnectionLog {
ip,_:=netip.AddrFromSlice(dblog.ConnectionLog.Ip.IPNet.IP)
varip*netip.Addr
ifdblog.ConnectionLog.Ip.Valid {
parsedIP,ok:=netip.AddrFromSlice(dblog.ConnectionLog.Ip.IPNet.IP)
ifok {
ip=&parsedIP
}
}

varuser*codersdk.User
ifdblog.ConnectionLog.UserID.Valid {
Expand Down
1 change: 1 addition & 0 deletionsenterprise/coderd/workspaceproxy.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -490,6 +490,7 @@ func (api *API) workspaceProxyIssueSignedAppToken(rw http.ResponseWriter, r *htt
return
}
userReq.Header.Set(codersdk.SessionTokenHeader,req.SessionToken)
userReq.RemoteAddr=r.Header.Get(wsproxysdk.CoderWorkspaceProxyRealIPHeader)

// Exchange the token.
token,tokenStr,ok:=api.AGPL.WorkspaceAppsProvider.Issue(ctx,rw,userReq,req)
Expand Down
52 changes: 49 additions & 3 deletionsenterprise/coderd/workspaceproxy_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@ package coderd_test
import (
"database/sql"
"fmt"
"net"
"net/http"
"net/http/httptest"
"net/http/httputil"
Expand All@@ -12,13 +13,15 @@ import (
"time"

"github.com/google/uuid"
"github.com/sqlc-dev/pqtype"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/agent/agenttest"
"github.com/coder/coder/v2/buildinfo"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/connectionlog"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/db2sdk"
"github.com/coder/coder/v2/coderd/database/dbgen"
Expand DownExpand Up@@ -610,13 +613,18 @@ func TestProxyRegisterDeregister(t *testing.T) {
func TestIssueSignedAppToken(t *testing.T) {
t.Parallel()

connectionLogger := connectionlog.NewFake()

client, user := coderdenttest.New(t, &coderdenttest.Options{
ConnectionLogging: true,
Options: &coderdtest.Options{
IncludeProvisionerDaemon: true,
ConnectionLogger: connectionLogger,
},
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureWorkspaceProxy: 1,
codersdk.FeatureConnectionLog: 1,
},
},
})
Expand DownExpand Up@@ -653,7 +661,7 @@ func TestIssueSignedAppToken(t *testing.T) {
// Invalid request.
AppRequest: workspaceapps.Request{},
SessionToken: client.SessionToken(),
})
}, "127.0.0.1")
require.Error(t, err)
})

Expand All@@ -669,41 +677,70 @@ func TestIssueSignedAppToken(t *testing.T) {
t.Parallel()
proxyClient := wsproxysdk.New(client.URL, proxyRes.ProxyToken)

fakeClientIP := "13.37.13.37"
parsedFakeClientIP := pqtype.Inet{
Valid: true, IPNet: net.IPNet{
IP: net.ParseIP(fakeClientIP),
Mask: net.CIDRMask(32, 32),
},
}

ctx := testutil.Context(t, testutil.WaitLong)
_, err := proxyClient.IssueSignedAppToken(ctx, goodRequest)
_, err := proxyClient.IssueSignedAppToken(ctx, goodRequest, fakeClientIP)
require.NoError(t, err)

require.True(t, connectionLogger.Contains(t, database.UpsertConnectionLogParams{
Ip: parsedFakeClientIP,
}))
})

t.Run("OKHTML", func(t *testing.T) {
t.Parallel()
proxyClient := wsproxysdk.New(client.URL, proxyRes.ProxyToken)

fakeClientIP := "192.168.1.100"
parsedFakeClientIP := pqtype.Inet{
Valid: true, IPNet: net.IPNet{
IP: net.ParseIP(fakeClientIP),
Mask: net.CIDRMask(32, 32),
},
}

rw := httptest.NewRecorder()
ctx := testutil.Context(t, testutil.WaitLong)
_, ok := proxyClient.IssueSignedAppTokenHTML(ctx, rw, goodRequest)
_, ok := proxyClient.IssueSignedAppTokenHTML(ctx, rw, goodRequest, fakeClientIP)
if !assert.True(t, ok, "expected true") {
resp := rw.Result()
defer resp.Body.Close()
dump, err := httputil.DumpResponse(resp, true)
require.NoError(t, err)
t.Log(string(dump))
}

require.True(t, connectionLogger.Contains(t, database.UpsertConnectionLogParams{
Ip: parsedFakeClientIP,
}))
})
}

func TestReconnectingPTYSignedToken(t *testing.T) {
t.Parallel()

connectionLogger := connectionlog.NewFake()

db, pubsub := dbtestutil.NewDB(t)
client, closer, api, user := coderdenttest.NewWithAPI(t, &coderdenttest.Options{
ConnectionLogging: true,
Options: &coderdtest.Options{
Database: db,
Pubsub: pubsub,
IncludeProvisionerDaemon: true,
ConnectionLogger: connectionLogger,
},
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureWorkspaceProxy: 1,
codersdk.FeatureConnectionLog: 1,
},
},
})
Expand DownExpand Up@@ -887,6 +924,15 @@ func TestReconnectingPTYSignedToken(t *testing.T) {

// The token is validated in the apptest suite, so we don't need to
// validate it here.

require.True(t, connectionLogger.Contains(t, database.UpsertConnectionLogParams{
Ip: pqtype.Inet{
Valid: true, IPNet: net.IPNet{
IP: net.ParseIP("127.0.0.1"),
Mask: net.CIDRMask(32, 32),
},
},
}))
})
}

Expand Down
2 changes: 1 addition & 1 deletionenterprise/wsproxy/tokenprovider.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -39,7 +39,7 @@ func (p *TokenProvider) Issue(ctx context.Context, rw http.ResponseWriter, r *ht
}
issueReq.AppRequest = appReq

resp, ok := p.Client.IssueSignedAppTokenHTML(ctx, rw, issueReq)
resp, ok := p.Client.IssueSignedAppTokenHTML(ctx, rw, issueReq, r.RemoteAddr)
if !ok {
return nil, "", false
}
Expand Down
12 changes: 10 additions & 2 deletionsenterprise/wsproxy/wsproxysdk/wsproxysdk.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,12 @@ import (
"github.com/coder/websocket"
)

const (
// CoderWorkspaceProxyAuthTokenHeader is the header that contains the
// resolved real IP address of the client that made the request to the proxy.
CoderWorkspaceProxyRealIPHeader = "Coder-Workspace-Proxy-Real-IP"
)

// Client is a HTTP client for a subset of Coder API routes that external
// proxies need.
type Client struct {
Expand DownExpand Up@@ -84,10 +90,11 @@ type IssueSignedAppTokenResponse struct {
// IssueSignedAppToken issues a new signed app token for the provided app
// request. The error page will be returned as JSON. For use in external
// proxies, use IssueSignedAppTokenHTML instead.
func (c *Client) IssueSignedAppToken(ctx context.Context, req workspaceapps.IssueTokenRequest) (IssueSignedAppTokenResponse, error) {
func (c *Client) IssueSignedAppToken(ctx context.Context, req workspaceapps.IssueTokenRequest, clientIP string) (IssueSignedAppTokenResponse, error) {
resp, err := c.RequestIgnoreRedirects(ctx, http.MethodPost, "/api/v2/workspaceproxies/me/issue-signed-app-token", req, func(r *http.Request) {
// This forces any HTML error pages to be returned as JSON instead.
r.Header.Set("Accept", "application/json")
r.Header.Set(CoderWorkspaceProxyRealIPHeader, clientIP)
})
if err != nil {
return IssueSignedAppTokenResponse{}, xerrors.Errorf("make request: %w", err)
Expand All@@ -105,7 +112,7 @@ func (c *Client) IssueSignedAppToken(ctx context.Context, req workspaceapps.Issu
// IssueSignedAppTokenHTML issues a new signed app token for the provided app
// request. The error page will be returned as HTML in most cases, and will be
// written directly to the provided http.ResponseWriter.
func (c *Client) IssueSignedAppTokenHTML(ctx context.Context, rw http.ResponseWriter, req workspaceapps.IssueTokenRequest) (IssueSignedAppTokenResponse, bool) {
func (c *Client) IssueSignedAppTokenHTML(ctx context.Context, rw http.ResponseWriter, req workspaceapps.IssueTokenRequest, clientIP string) (IssueSignedAppTokenResponse, bool) {
writeError := func(rw http.ResponseWriter, err error) {
res := codersdk.Response{
Message: "Internal server error",
Expand All@@ -117,6 +124,7 @@ func (c *Client) IssueSignedAppTokenHTML(ctx context.Context, rw http.ResponseWr

resp, err := c.RequestIgnoreRedirects(ctx, http.MethodPost, "/api/v2/workspaceproxies/me/issue-signed-app-token", req, func(r *http.Request) {
r.Header.Set("Accept", "text/html")
r.Header.Set(CoderWorkspaceProxyRealIPHeader, clientIP)
})
if err != nil {
writeError(rw, xerrors.Errorf("perform issue signed app token request: %w", err))
Expand Down
6 changes: 4 additions & 2 deletionsenterprise/wsproxy/wsproxysdk/wsproxysdk_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,8 @@ import (
func Test_IssueSignedAppTokenHTML(t *testing.T) {
t.Parallel()

fakeClientIP := "127.0.0.1"

t.Run("OK", func(t *testing.T) {
t.Parallel()

Expand DownExpand Up@@ -68,7 +70,7 @@ func Test_IssueSignedAppTokenHTML(t *testing.T) {
tokenRes, ok := client.IssueSignedAppTokenHTML(ctx, rw, workspaceapps.IssueTokenRequest{
AppRequest: expectedAppReq,
SessionToken: expectedSessionToken,
})
}, fakeClientIP)
if !assert.True(t, ok) {
t.Log("issue request failed when it should've succeeded")
t.Log("response dump:")
Expand DownExpand Up@@ -118,7 +120,7 @@ func Test_IssueSignedAppTokenHTML(t *testing.T) {
tokenRes, ok := client.IssueSignedAppTokenHTML(ctx, rw, workspaceapps.IssueTokenRequest{
AppRequest: workspaceapps.Request{},
SessionToken: "user-session-token",
})
}, fakeClientIP)
require.False(t, ok)
require.Empty(t, tokenRes)
require.True(t, rw.WasWritten())
Expand Down
2 changes: 1 addition & 1 deletionsite/src/api/typesGenerated.ts
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp