This is an automated email from the ASF dual-hosted git repository.

LinkinStars pushed a commit to branch fix/2.0.3/user
in repository https://gitbox.apache.org/repos/asf/answer.git

commit b80ad0a892f6a597108dd3ca37dd299bd82682ad
Author: LinkinStars <[email protected]>
AuthorDate: Fri Aug 14 16:34:33 2026 +0800

    fix: avoid sensitive auth logging
---
 internal/service/auth/auth.go      |  3 --
 internal/service/auth/auth_test.go | 99 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 99 insertions(+), 3 deletions(-)

diff --git a/internal/service/auth/auth.go b/internal/service/auth/auth.go
index 7d275105..a7827c78 100644
--- a/internal/service/auth/auth.go
+++ b/internal/service/auth/auth.go
@@ -26,7 +26,6 @@ import (
        "github.com/apache/answer/internal/service/apikey"
        "github.com/apache/answer/pkg/token"
        "github.com/apache/answer/plugin"
-       "github.com/segmentfault/pacman/log"
 )
 
 // AuthRepo auth repository
@@ -198,9 +197,7 @@ func (as *AuthService) AuthAPIKey(ctx context.Context, read 
bool, apiKey string)
        }
        // If the request is not read-only, check if the API key has write 
permissions
        if !read && apiKeyInfo.Scope == "read-only" {
-               log.Warnf("API key %s does not have write permissions", 
apiKeyInfo.AccessKey)
                return false, nil
        }
-       log.Infof("API key %s is valid, scope: %s", apiKeyInfo.AccessKey, 
apiKeyInfo.Scope)
        return true, nil
 }
diff --git a/internal/service/auth/auth_test.go 
b/internal/service/auth/auth_test.go
new file mode 100644
index 00000000..17cbd001
--- /dev/null
+++ b/internal/service/auth/auth_test.go
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 auth
+
+import (
+       "context"
+       "fmt"
+       "strings"
+       "testing"
+
+       "github.com/apache/answer/internal/entity"
+       "github.com/segmentfault/pacman/log"
+)
+
+func TestAuthAPIKeyDoesNotLogAccessKey(t *testing.T) {
+       const accessKey = "sk_sensitive-api-key-must-not-be-logged"
+
+       logger := &authTestLogger{}
+       previousLogger := log.GetLogger()
+       log.SetLogger(logger)
+       t.Cleanup(func() { log.SetLogger(previousLogger) })
+
+       service := NewAuthService(nil, &authTestAPIKeyRepo{
+               key: &entity.APIKey{AccessKey: accessKey, Scope: "read-only"},
+       })
+
+       pass, err := service.AuthAPIKey(context.Background(), true, accessKey)
+       if err != nil || !pass {
+               t.Fatalf("read-only API key should authenticate read request: 
pass=%v err=%v", pass, err)
+       }
+
+       pass, err = service.AuthAPIKey(context.Background(), false, accessKey)
+       if err != nil || pass {
+               t.Fatalf("read-only API key should not authenticate write 
request: pass=%v err=%v", pass, err)
+       }
+
+       if logs := logger.String(); strings.Contains(logs, accessKey) {
+               t.Fatalf("authentication logs contain API key: %s", logs)
+       }
+}
+
+type authTestAPIKeyRepo struct {
+       key *entity.APIKey
+}
+
+func (r *authTestAPIKeyRepo) GetAPIKeyList(context.Context) ([]*entity.APIKey, 
error) {
+       return nil, nil
+}
+
+func (r *authTestAPIKeyRepo) GetAPIKey(context.Context, string) 
(*entity.APIKey, bool, error) {
+       return r.key, true, nil
+}
+
+func (r *authTestAPIKeyRepo) UpdateAPIKey(context.Context, entity.APIKey) 
error { return nil }
+
+func (r *authTestAPIKeyRepo) AddAPIKey(context.Context, entity.APIKey) error { 
return nil }
+
+func (r *authTestAPIKeyRepo) DeleteAPIKey(context.Context, int) error { return 
nil }
+
+func (r *authTestAPIKeyRepo) DeleteAPIKeysByUserID(context.Context, string) 
error { return nil }
+
+type authTestLogger struct {
+       entries []string
+}
+
+func (l *authTestLogger) Debug(v ...any) { l.entries = append(l.entries, 
fmt.Sprint(v...)) }
+func (l *authTestLogger) Debugf(format string, v ...any) {
+       l.entries = append(l.entries, fmt.Sprintf(format, v...))
+}
+func (l *authTestLogger) Info(v ...any) { l.entries = append(l.entries, 
fmt.Sprint(v...)) }
+func (l *authTestLogger) Infof(format string, v ...any) {
+       l.entries = append(l.entries, fmt.Sprintf(format, v...))
+}
+func (l *authTestLogger) Warn(v ...any) { l.entries = append(l.entries, 
fmt.Sprint(v...)) }
+func (l *authTestLogger) Warnf(format string, v ...any) {
+       l.entries = append(l.entries, fmt.Sprintf(format, v...))
+}
+func (l *authTestLogger) Error(v ...any) { l.entries = append(l.entries, 
fmt.Sprint(v...)) }
+func (l *authTestLogger) Errorf(format string, v ...any) {
+       l.entries = append(l.entries, fmt.Sprintf(format, v...))
+}
+func (l *authTestLogger) String() string { return strings.Join(l.entries, 
"\n") }

Reply via email to