Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pam/gdm: Keep debugging all the events, sanitizing them if needed #565

Merged
merged 3 commits into from
Oct 3, 2024
Merged
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
10 changes: 3 additions & 7 deletions pam/internal/adapter/gdmmodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,9 @@ func (m *gdmModel) pollGdm() tea.Cmd {
})
}

for _, result := range gdmPollResults {
// Don't log EventData_IsAuthenticatedRequested because it contains
// the user password
if result.GetIsAuthenticatedRequested() != nil {
log.Debugf(context.TODO(), "GDM poll returned: IsAuthenticatedRequested")
} else {
log.Debugf(context.TODO(), "GDM poll returned: %s", result.Data)
if log.IsLevelEnabled(log.DebugLevel) {
for _, result := range gdmPollResults {
log.Debugf(context.TODO(), "GDM poll response: %v", result.SafeString())
}
}

Expand Down
13 changes: 11 additions & 2 deletions pam/internal/gdm/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"context"
"errors"
"fmt"
"regexp"
"sync/atomic"

"github.com/msteinert/pam/v2"
"github.com/ubuntu/authd/internal/log"
)

var conversations atomic.Int32
var challengeRegex = regexp.MustCompile(`"challenge"\s*:\s*"(?:[^"\\]|\\.)*"`)

// ConversationInProgress checks if conversations are currently active.
func ConversationInProgress() bool {
Expand Down Expand Up @@ -63,9 +65,16 @@ func SendData(pamMTx pam.ModuleTransaction, d *Data) (*Data, error) {
}

gdmData, err := NewDataFromJSON(jsonValue)
// Log unless it's a poll, which are so frequently that it would be
// Log unless it's an empty poll, which are so frequently that it would be
// too verbose to log them.
if d.Type != DataType_poll {
if gdmData.Type == DataType_pollResponse && len(gdmData.GetPollResponse()) == 0 {
jsonValue = nil
}
if log.IsLevelEnabled(log.DebugLevel) && jsonValue != nil &&
gdmData != nil && gdmData.Type == DataType_pollResponse {
jsonValue = challengeRegex.ReplaceAll(jsonValue, []byte(`"challenge":"**************"`))
}
if jsonValue != nil {
log.Debugf(context.TODO(), "Got from GDM: %s", jsonValue)
}
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pam/internal/gdm/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ package gdm
func init() {
checkMembersFunc = checkMembersDebug
validateJSONFunc = validateJSONDebug
stringifyEventDataFunc = stringifyEventDataDebug
}
1 change: 1 addition & 0 deletions pam/internal/gdm/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package gdm
func init() {
checkMembersFunc = checkMembersDebug
validateJSONFunc = validateJSONDebug
stringifyEventDataFunc = stringifyEventDataDebug
}
37 changes: 37 additions & 0 deletions pam/internal/gdm/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"slices"

"github.com/ubuntu/authd"
"google.golang.org/protobuf/encoding/protojson"
)

Expand Down Expand Up @@ -167,3 +168,39 @@ func (d *Data) JSON() ([]byte, error) {

return bytes, err
}

var stringifyEventDataFunc = stringifyEventDataFiltered

func stringifyEventDataDebug(ed *EventData) string {
return ed.String()
}

func stringifyEventDataFiltered(ed *EventData) string {
authReq, ok := ed.GetData().(*EventData_IsAuthenticatedRequested)
if !ok {
return ed.String()
}

item := authReq.IsAuthenticatedRequested.GetAuthenticationData().Item
if _, ok = item.(*authd.IARequest_AuthenticationData_Challenge); !ok {
return ed.String()
}

return (&EventData{
Type: ed.Type,
Data: &EventData_IsAuthenticatedRequested{
IsAuthenticatedRequested: &Events_IsAuthenticatedRequested{
AuthenticationData: &authd.IARequest_AuthenticationData{
Item: &authd.IARequest_AuthenticationData_Challenge{
Challenge: "**************",
},
},
},
},
}).String()
}

// SafeString creates a string of EventData with confidential content removed.
func (ed *EventData) SafeString() string {
return stringifyEventDataFunc(ed)
}
Loading