Skip to content

Commit 70518a6

Browse files
committed
pam/utils: Use safe formatting also for the values passed as arguments
If safeMessageDebug is used to print multiple types, only the first one is safely parsed, while the others are not. This won't cover (and it's not even possible) to do it in case a format is used, but should allow to easily and safely print things around anyways
1 parent bce7ee8 commit 70518a6

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

pam/internal/adapter/authentication.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,16 @@ func (m authenticationModel) Update(msg tea.Msg) (authModel authenticationModel,
204204
return m, sendEvent(startAuthentication{})
205205

206206
case startAuthentication:
207-
safeMessageDebug(msg, "current model %v, focused %v",
208-
m.currentModel, m.Focused())
207+
safeMessageDebug(msg, "current model:", m.currentModel,
208+
"focused:", m.Focused())
209209
if !m.Focused() {
210210
return m, nil
211211
}
212212
m.inProgress = true
213213

214214
case stopAuthentication:
215-
safeMessageDebug(msg, "current model %v, focused %v",
216-
m.currentModel, m.Focused())
215+
safeMessageDebug(msg, "current model:", m.currentModel,
216+
"focused:", m.Focused())
217217
m.inProgress = false
218218

219219
case reselectAuthMode:

pam/internal/adapter/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func (m uiModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
271271

272272
// Events
273273
case BrokerListReceived:
274-
safeMessageDebug(msg, "brokers: %#v", m.availableBrokers())
274+
safeMessageDebug(msg, "brokers:", m.availableBrokers())
275275
if m.availableBrokers() == nil {
276276
return m, nil
277277
}

pam/internal/adapter/reselectbuttonmodel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (b authReselectButtonModel) Init() tea.Cmd {
2525
func (b authReselectButtonModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2626
switch msg := msg.(type) {
2727
case buttonSelectionEvent:
28-
safeMessageDebug(msg, "button: %#v", b)
28+
safeMessageDebug(msg, "button:", b)
2929
if msg.model == b.buttonModel {
3030
return b, sendEvent(reselectAuthMode{})
3131
}

pam/internal/adapter/utils.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,14 @@ func safeMessageDebugWithPrefix(prefix string, msg tea.Msg, formatAndArgs ...any
264264

265265
format, ok := formatAndArgs[0].(string)
266266
if !ok || !strings.Contains(format, "%") {
267-
log.Debug(context.Background(), append([]any{m, ", "}, formatAndArgs...)...)
267+
args := []any{m, ", "}
268+
for i, arg := range formatAndArgs {
269+
args = append(args, debugMessageFormatter(arg))
270+
if i < len(formatAndArgs)-1 {
271+
args = append(args, " ")
272+
}
273+
}
274+
log.Debug(context.Background(), args...)
268275
return
269276
}
270277

pam/internal/adapter/utils_test.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,22 @@ func TestSafeMessageDebug(t *testing.T) {
239239
formatAndArgs: []any{true, false},
240240
wantSafeString: "prefix: adapter.startAuthentication{}, true false",
241241
},
242+
"startAuthentication_message_with_prefix_and_multiple_formatted_values_suffix": {
243+
msg: startAuthentication{},
244+
prefix: "prefix",
245+
formatAndArgs: []any{
246+
newPasswordCheck{password: "password!"},
247+
UILayoutReceived{&authd.UILayout{Type: "Foo"}},
248+
authModesReceived{
249+
authModes: []*authd.GAMResponse_AuthenticationMode{
250+
{Id: "id1", Label: "Label1"},
251+
{Id: "id2", Label: "Label2"},
252+
},
253+
},
254+
},
255+
wantDebugString: `prefix: adapter.startAuthentication{}, adapter.newPasswordCheck{ctx:context.Context(nil), password:"password!"} adapter.UILayoutReceived{layouts:*authd.UILayout{{"type":"Foo"}}} adapter.authModesReceived{authModes:[]*authd.GAMResponse_AuthenticationMode{[{"id":"id1","label":"Label1"},{"id":"id2","label":"Label2"}]}}`,
256+
wantSafeString: `prefix: adapter.startAuthentication{}, adapter.newPasswordCheck{ctx:context.Context(nil), password:"***********"} adapter.UILayoutReceived{layouts:*authd.UILayout{{"type":"Foo"}}} adapter.authModesReceived{authModes:[]*authd.GAMResponse_AuthenticationMode{[{"id":"id1","label":"Label1"},{"id":"id2","label":"Label2"}]}}`,
257+
},
242258
"startAuthentication_message_with_prefix_and_single_string_suffix": {
243259
msg: startAuthentication{},
244260
prefix: "prefix",
@@ -329,11 +345,12 @@ func TestSafeMessageDebug(t *testing.T) {
329345
handlerCalled := false
330346
wantCtx := context.Background()
331347
log.SetLevelHandler(log.DebugLevel, func(ctx context.Context, l log.Level, format string, args ...interface{}) {
332-
t.Logf(format, args...)
348+
t.Logf("Format: %q", format)
349+
t.Log(append([]any{"Args:"}, args...)...)
333350
handlerCalled = true
334351
require.Equal(t, wantCtx, ctx, "Context should match expected")
335352
require.Equal(t, tc.wantSafeString, fmt.Sprintf(format, args...),
336-
"Format should match")
353+
"Format for safe usage should match")
337354
})
338355

339356
initialLogLevel := log.GetLevel()
@@ -363,11 +380,11 @@ func TestSafeMessageDebug(t *testing.T) {
363380
log.SetLevelHandler(log.DebugLevel, func(ctx context.Context, l log.Level, format string, args ...interface{}) {
364381
t.Logf(format, args...)
365382
handlerCalled = true
366-
t.Logf("Called with %q", format)
367-
t.Logf("Atgs are %#v", args)
383+
t.Logf("Format: %q", format)
384+
t.Log(append([]any{"Args:"}, args...)...)
368385
require.Equal(t, wantCtx, ctx, "Context should match expected")
369386
require.Equal(t, tc.wantDebugString, fmt.Sprintf(format, args...),
370-
"Format should match")
387+
"Format for debug usage should match")
371388
})
372389

373390
initialLogLevel := log.GetLevel()

0 commit comments

Comments
 (0)