Skip to content
Open
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
52 changes: 52 additions & 0 deletions api/v2/uri_redaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2026 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v2

import (
"testing"

"github.com/pingcap/ticdc/pkg/common"
"github.com/pingcap/ticdc/pkg/config"
"github.com/stretchr/testify/require"
)

func TestCfInfoToAPIModelOmitsNestedStorageCredentials(t *testing.T) {
replicaConfig := config.GetDefaultReplicaConfig()
replicaConfig.Consistent.Storage = new("s3://bucket/redo?ENDPOINT=" +
"https%3A%2F%2Fuser%3Aredo-endpoint-secret-sentinel%40storage.example")
replicaConfig.Sink.KafkaConfig = &config.KafkaConfig{
LargeMessageHandle: &config.LargeMessageHandleConfig{
ClaimCheckStorageURI: "s3://bucket/claim?endpoint=" +
"https%3A%2F%2Fuser%3Aclaim-endpoint-secret-sentinel%40storage.example",
},
}
info := &config.ChangeFeedInfo{
ChangefeedID: common.NewChangeFeedIDWithName("test", common.DefaultKeyspaceName),
Config: replicaConfig,
}
status := &config.ChangeFeedStatus{CheckpointTs: 123}
original, err := info.Marshal()
require.NoError(t, err)

apiInfo := CfInfoToAPIModel(info, status, nil)
response, err := apiInfo.Marshal()
require.NoError(t, err)
require.NotContains(t, response, "endpoint-secret-sentinel")
require.Nil(t, apiInfo.Config.Consistent.Storage)
require.Nil(t, apiInfo.Config.Sink.KafkaConfig.LargeMessageHandle.ClaimCheckStorageURI)
after, err := info.Marshal()
require.NoError(t, err)
require.Equal(t, original, after)
}
3 changes: 2 additions & 1 deletion cmd/pulsar-consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pingcap/log"
"github.com/pingcap/ticdc/pkg/errors"
tpulsar "github.com/pingcap/ticdc/pkg/sink/pulsar"
"github.com/pingcap/ticdc/pkg/util"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -62,7 +63,7 @@ func newConsumer(ctx context.Context, option *option) *consumer {
auth.ConfigParamScope: option.oauth2Scope,
auth.ConfigParamType: auth.ConfigParamTypeClientCredentials,
})
log.Info("oauth2 authentication is enabled", zap.String("issuerUrl", option.oauth2IssuerURL))
log.Info("oauth2 authentication is enabled", zap.String("issuerUrl", util.MaskSensitiveDataInURIForError(option.oauth2IssuerURL)))
clientOption.Authentication = authentication
}
if len(option.mtlsAuthTLSCertificatePath) != 0 {
Expand Down
5 changes: 3 additions & 2 deletions cmd/pulsar-consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/pingcap/log"
"github.com/pingcap/ticdc/pkg/config"
"github.com/pingcap/ticdc/pkg/logger"
"github.com/pingcap/ticdc/pkg/util"
"github.com/pingcap/ticdc/pkg/version"
"github.com/spf13/cobra"
"go.uber.org/zap"
Expand Down Expand Up @@ -81,13 +82,13 @@ func run(_ *cobra.Command, _ []string) {

upstreamURI, err := url.Parse(upstreamURIStr)
if err != nil {
log.Panic("invalid upstream-uri", zap.Error(err))
log.Panic("invalid upstream-uri", zap.Error(util.MaskSensitiveDataInURLError(err)))
}
scheme := strings.ToLower(upstreamURI.Scheme)
if !config.IsPulsarScheme(scheme) {
log.Panic("invalid upstream-uri scheme, the scheme of upstream-uri must be pulsar schema",
zap.String("schema", scheme),
zap.String("upstreamURI", upstreamURIStr))
zap.String("upstreamURI", util.MaskSensitiveDataInURIForError(upstreamURIStr)))
}

consumerOption.Adjust(upstreamURI, configFile)
Expand Down
1 change: 1 addition & 0 deletions logservice/logpuller/span_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func (span *subscribedSpan) resolveStaleLocks(targetTs uint64) {
if log.GetLevel() <= zapcore.DebugLevel {
log.Debug("subscription client finds slow locked ranges",
zap.Uint64("subscriptionID", uint64(span.subID)),
zap.Int64("tableID", span.span.TableID),
zap.Int("lockedRegionCount", res.LockedRegionCount),
zap.Strings("holes", formatUnlockedRanges(res.UnLockedRanges)),
zap.Any("fastestRegion", res.FastestRegion),
Expand Down
8 changes: 7 additions & 1 deletion pkg/config/changefeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,13 @@ func (cfg *ChangefeedConfig) String() string {
log.Error("failed to unmarshal changefeed config", zap.Error(err))
return ""
}
cloned.SinkConfig.MaskSensitiveData()
cloned.SinkURI = util.MaskSensitiveDataInURI(cloned.SinkURI)
if cloned.SinkConfig != nil {
cloned.SinkConfig.MaskSensitiveData()
}
if cloned.Consistent != nil {
cloned.Consistent.MaskSensitiveData()
}
res, err := json.Marshal(cloned)
if err != nil {
log.Error("failed to marshal changefeed config", zap.Error(err))
Expand Down
119 changes: 119 additions & 0 deletions pkg/config/changefeed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,125 @@
require.Equal(t, original, after)
}

func TestChangefeedConfigStringMasksSensitiveData(t *testing.T) {
cfg := GetDefaultReplicaConfig()
cfg.Sink.KafkaConfig = &KafkaConfig{
SASLPassword: new("plain-password-sentinel"),
}
changefeedConfig := &ChangefeedConfig{
SinkURI: "mysql://user:sink-password-sentinel@127.0.0.1:3306/?secret=uri-secret-sentinel",
SinkConfig: cfg.Sink,
Consistent: &ConsistentConfig{Storage: util.AddressOf("s3://bucket/prefix?secret-access-key=redo-secret-sentinel")},

Check failure on line 275 in pkg/config/changefeed_test.go

View workflow job for this annotation

GitHub Actions / Check

newexpr: call of AddressOf(x) can be simplified to new(x) (modernize)
}
original, err := json.Marshal(changefeedConfig)
require.NoError(t, err)

output := changefeedConfig.String()
for _, secret := range []string{
"sink-password-sentinel",
"uri-secret-sentinel",
"plain-password-sentinel",
"redo-secret-sentinel",
} {
require.NotContains(t, output, secret)
}
require.Contains(t, output, "mysql://user:xxxxx@127.0.0.1:3306/?secret=xxxxx")
require.Contains(t, output, "secret-access-key=xxxxx")
after, err := json.Marshal(changefeedConfig)
require.NoError(t, err)
require.Equal(t, original, after)
}

func TestChangefeedConfigStringMasksNestedRedoStorageCredential(t *testing.T) {
tests := []struct {
name string
storage string
secret string
wantOuter string
wantDisplay string
}{
{
name: "Azure SAS endpoint",
storage: "azblob://container/prefix?account-name=account&endpoint=" +
"https%3A%2F%2Faccount.blob.core.windows.net%2F%3Fsv%3D2020%26sig%3Dnested-sas-secret-sentinel",
secret: "nested-sas-secret-sentinel",
wantOuter: "account-name=account",
wantDisplay: "endpoint=xxxxxx",
},
{
name: "S3 endpoint userinfo",
storage: "s3://bucket/prefix?region=us-east-1&endpoint=https%3A%2F%2Fuser%3Aendpoint-secret-sentinel%40storage.example",
secret: "endpoint-secret-sentinel",
wantOuter: "region=us-east-1",
wantDisplay: "endpoint=xxxxxx",
},
{
name: "S3 uppercase endpoint userinfo",
storage: "s3://bucket/prefix?region=us-east-1&ENDPOINT=https%3A%2F%2Fuser%3Auppercase-endpoint-secret-sentinel%40storage.example",
secret: "uppercase-endpoint-secret-sentinel",
wantOuter: "region=us-east-1",
wantDisplay: "ENDPOINT=xxxxxx",
},
{
name: "S3 plain endpoint",
storage: "s3://bucket/prefix?region=us-east-1&endpoint=https%3A%2F%2Fstorage.example",
wantOuter: "region=us-east-1",
wantDisplay: "endpoint=https%3A%2F%2Fstorage.example",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &ChangefeedConfig{Consistent: &ConsistentConfig{Storage: &tt.storage}}
original, err := json.Marshal(cfg)
require.NoError(t, err)

output := cfg.String()
if tt.secret != "" {
require.NotContains(t, output, tt.secret)
}
require.Contains(t, output, tt.wantDisplay)
require.Contains(t, output, tt.wantOuter)
after, err := json.Marshal(cfg)
require.NoError(t, err)
require.Equal(t, original, after)
})
}
}

// TestChangeFeedInfoRmUnusedFieldsEncoderConcurrency sets a custom concurrency
// and removes unused fields, retaining it only for MQ sinks or blackhole.
func TestChangeFeedInfoRmUnusedFieldsEncoderConcurrency(t *testing.T) {
tests := []struct {
sinkURI string
keep bool
}{
{sinkURI: "kafka://127.0.0.1:9092/topic", keep: true},
{sinkURI: "pulsar://127.0.0.1:6650/topic", keep: true},
{sinkURI: "file:///tmp/cdc"},
{sinkURI: "s3://bucket/cdc"},
{sinkURI: "gcs://bucket/cdc"},
{sinkURI: "azblob://bucket/cdc"},
{sinkURI: "mysql://127.0.0.1:3306"},
{sinkURI: "tidb://127.0.0.1:4000"},
{sinkURI: "blackhole://", keep: true},
}
for _, tt := range tests {
// Cleanup must preserve the configured value for MQ sinks and blackhole.
t.Run(tt.sinkURI, func(t *testing.T) {
cfg := GetDefaultReplicaConfig()
cfg.Sink.EncoderConcurrency = util.AddressOf(16)

Check failure on line 373 in pkg/config/changefeed_test.go

View workflow job for this annotation

GitHub Actions / Check

newexpr: call of AddressOf(x) can be simplified to new(x) (modernize)
info := &ChangeFeedInfo{SinkURI: tt.sinkURI, Config: cfg}
info.RmUnusedFields()
if tt.keep {
require.NotNil(t, cfg.Sink.EncoderConcurrency)
require.Equal(t, 16, *cfg.Sink.EncoderConcurrency)
} else {
require.Nil(t, cfg.Sink.EncoderConcurrency)
}
})
}
}

func TestChangeFeedInfoRmUnusedFieldsKeepsTableRouting(t *testing.T) {
for _, sinkURI := range []string{"mysql://127.0.0.1:3306", "file:///tmp/cdc"} {
t.Run(sinkURI, func(t *testing.T) {
Expand Down
25 changes: 24 additions & 1 deletion pkg/config/consistent.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import (
"fmt"
"net/url"

"github.com/pingcap/ticdc/pkg/compression"
"github.com/pingcap/ticdc/pkg/errors"
Expand Down Expand Up @@ -158,5 +159,27 @@

// MaskSensitiveData masks sensitive data in ConsistentConfig
func (c *ConsistentConfig) MaskSensitiveData() {
c.Storage = util.AddressOf(util.MaskSensitiveDataInURI(util.GetOrZero(c.Storage)))
redactedStorage := objstore.RedactURL(util.GetOrZero(c.Storage))
parsed, err := url.Parse(redactedStorage)
if err == nil {
query := parsed.Query()
maskedEndpoint := false
for key, endpoints := range query {
if objstore.NormalizeQueryParameterKey(key) != "endpoint" {
continue
}
for _, endpoint := range endpoints {
if objstore.RedactURL(endpoint) != endpoint {
query.Set(key, "xxxxxx")
maskedEndpoint = true
break
}
}
}
if maskedEndpoint {
parsed.RawQuery = query.Encode()
redactedStorage = parsed.String()
}
}
c.Storage = util.AddressOf(util.MaskSensitiveDataInURI(redactedStorage))

Check failure on line 184 in pkg/config/consistent.go

View workflow job for this annotation

GitHub Actions / Check

newexpr: call of AddressOf(x) can be simplified to new(x) (modernize)
}
46 changes: 42 additions & 4 deletions pkg/util/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@

func isSensitiveQueryParameter(name string) bool {
name = strings.ToLower(name)
if name == "sig" {
return true
}
if name == "kafka-client" {
return false // Selects the Kafka client implementation, not a credential.
}
Expand All @@ -108,6 +111,16 @@
return false
}

// endpointContainsSensitiveData treats nested endpoint URL parameters as
// credentials because storage providers can use arbitrary authentication keys.
func endpointContainsSensitiveData(endpoint string) bool {
parsed, err := url.Parse(endpoint)
if err != nil {
return true
}
return parsed.User != nil || parsed.RawQuery != "" || parsed.Fragment != ""
}

// maskSensitiveDataInQueryLikeText handles fragment-like request target text
// without rewriting ordinary URI fragments.
func maskSensitiveDataInQueryLikeText(text string) (string, bool) {
Expand All @@ -122,14 +135,23 @@
parts := strings.Split(queryText, "&")
masked := false
for i, part := range parts {
name, _, hasValue := strings.Cut(part, "=")
name, value, hasValue := strings.Cut(part, "=")
if !hasValue {
continue
}
decodedName, err := url.QueryUnescape(name)
if err != nil || !isSensitiveQueryParameter(decodedName) {
if err != nil {
continue
}
if !isSensitiveQueryParameter(decodedName) {
if !strings.EqualFold(decodedName, "endpoint") {
continue
}
endpoint, err := url.QueryUnescape(value)
if err == nil && !endpointContainsSensitiveData(endpoint) {
continue
}
}
parts[i] = name + "=xxxxx"
masked = true
}
Expand Down Expand Up @@ -161,10 +183,17 @@
if err != nil {
return true
}
for name := range query {
for name, values := range query {
if isSensitiveQueryParameter(name) {
return true
}
if strings.EqualFold(name, "endpoint") {
for _, endpoint := range values {

Check failure on line 191 in pkg/util/uri.go

View workflow job for this annotation

GitHub Actions / Check

slicescontains: Loop can be simplified using slices.ContainsFunc (modernize)
if endpointContainsSensitiveData(endpoint) {
return true
}
}
}
}
if _, sensitive := maskSensitiveDataInQueryLikeText(parsed.Fragment); sensitive {
return true
Expand All @@ -179,9 +208,18 @@
return ""
}
queries := uriParsed.Query()
for key := range queries {
for key, values := range queries {
if isSensitiveQueryParameter(key) {
queries.Set(key, "xxxxx")
continue
}
if strings.EqualFold(key, "endpoint") {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
for _, endpoint := range values {

Check failure on line 217 in pkg/util/uri.go

View workflow job for this annotation

GitHub Actions / Check

slicescontains: Loop can be simplified using slices.ContainsFunc (modernize)
if endpointContainsSensitiveData(endpoint) {
queries.Set(key, "xxxxx")
break
}
}
}
}
uriParsed.RawQuery = queries.Encode()
Expand Down
Loading
Loading