Skip to content

Commit

Permalink
Add len checks for lobby and peer ids (#53)
Browse files Browse the repository at this point in the history
We are getting some weird errors in our database that are exceeding the
type length. This should help us learn more about the issue.
  • Loading branch information
koenbollen authored Aug 30, 2023
1 parent 678769e commit 8c2b172
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions internal/signaling/stores/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ func (s *PostgresStore) Publish(ctx context.Context, topic string, data []byte)
}

func (s *PostgresStore) CreateLobby(ctx context.Context, game, lobbyCode, peerID string) error {
if len(lobbyCode) > 20 {
logger := logging.GetLogger(ctx)
logger.Warn("lobby code too long", zap.String("lobbyCode", lobbyCode))
return ErrInvalidLobbyCode
}
if len(peerID) > 20 {
logger := logging.GetLogger(ctx)
logger.Warn("peer id too long", zap.String("peerID", peerID))
return ErrInvalidPeerID
}
res, err := s.DB.Exec(ctx, `
INSERT INTO lobbies (code, game, public)
VALUES ($1, $2, true)
Expand All @@ -156,6 +166,11 @@ func (s *PostgresStore) CreateLobby(ctx context.Context, game, lobbyCode, peerID
}

func (s *PostgresStore) JoinLobby(ctx context.Context, game, lobbyCode, peerID string) ([]string, error) {
if len(peerID) > 20 {
logger := logging.GetLogger(ctx)
logger.Warn("peer id too long", zap.String("peerID", peerID))
return nil, ErrInvalidPeerID
}
tx, err := s.DB.Begin(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -284,6 +299,13 @@ func (s *PostgresStore) ListLobbies(ctx context.Context, game, filter string) ([
}

func (s *PostgresStore) TimeoutPeer(ctx context.Context, peerID, secret, gameID string, lobbies []string) error {

if len(peerID) > 20 {
logger := logging.GetLogger(ctx)
logger.Warn("peer id too long", zap.String("peerID", peerID))
return ErrInvalidPeerID
}

now := util.Now(ctx)
_, err := s.DB.Exec(ctx, `
INSERT INTO timeouts (peer, secret, game, lobbies, created_at, updated_at)
Expand Down
2 changes: 2 additions & 0 deletions internal/signaling/stores/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var ErrAlreadyInLobby = errors.New("peer already in lobby")
var ErrLobbyExists = errors.New("lobby already exists")
var ErrNotFound = errors.New("lobby not found")
var ErrNoSuchTopic = errors.New("no such topic")
var ErrInvalidLobbyCode = errors.New("invalid lobby code")
var ErrInvalidPeerID = errors.New("invalid peer id")

type SubscriptionCallback func(context.Context, []byte)

Expand Down

0 comments on commit 8c2b172

Please sign in to comment.