Skip to content

Commit 02c6b21

Browse files
authored
Postgres v2 state: handle invalid etag format (dapr#3279)
Signed-off-by: ItalyPaleAle <[email protected]>
1 parent 73997a2 commit 02c6b21

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

state/postgresql/v2/postgresql.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strconv"
2323
"time"
2424

25+
"github.com/google/uuid"
2526
"github.com/jackc/pgx/v5"
2627
"github.com/jackc/pgx/v5/pgconn"
2728
"github.com/jackc/pgx/v5/pgxpool"
@@ -237,7 +238,15 @@ func (p *PostgreSQL) doSet(parentCtx context.Context, db pginterfaces.DBQuerier,
237238
)
238239

239240
if req.HasETag() {
240-
params = []any{req.Key, value, *req.ETag}
241+
// Check if the etag is valid
242+
var etag uuid.UUID
243+
etag, err = uuid.Parse(*req.ETag)
244+
if err != nil {
245+
// Return an etag mismatch error right away if the etag is invalid
246+
return state.NewETagError(state.ETagMismatch, err)
247+
}
248+
249+
params = []any{req.Key, value, etag.String()}
241250
} else {
242251
params = []any{req.Key, value}
243252
}
@@ -434,7 +443,15 @@ func (p *PostgreSQL) doDelete(parentCtx context.Context, db pginterfaces.DBQueri
434443
defer cancel()
435444
var result pgconn.CommandTag
436445
if req.HasETag() {
437-
result, err = db.Exec(ctx, "DELETE FROM "+p.metadata.TableName(pgTableState)+" WHERE key = $1 AND etag = $2", req.Key, *req.ETag)
446+
// Check if the etag is valid
447+
var etag uuid.UUID
448+
etag, err = uuid.Parse(*req.ETag)
449+
if err != nil {
450+
// Return an etag mismatch error right away if the etag is invalid
451+
return state.NewETagError(state.ETagMismatch, err)
452+
}
453+
454+
result, err = db.Exec(ctx, "DELETE FROM "+p.metadata.TableName(pgTableState)+" WHERE key = $1 AND etag = $2", req.Key, etag)
438455
} else {
439456
result, err = db.Exec(ctx, "DELETE FROM "+p.metadata.TableName(pgTableState)+" WHERE key = $1", req.Key)
440457
}

0 commit comments

Comments
 (0)