Skip to content

Simple support for DUMP and RESTORE #405

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

Merged
merged 4 commits into from
Jun 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 1 addition & 6 deletions cmd_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,7 @@ func (m *Miniredis) cmdEcho(c *server.Peer, cmd string, args []string) {

// SELECT
func (m *Miniredis) cmdSelect(c *server.Peer, cmd string, args []string) {
if len(args) != 1 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.isValidCMD(c, cmd) {
if !m.isValidCMD(c, cmd, args, exactly(1)) {
return
}

Expand Down
163 changes: 16 additions & 147 deletions cmd_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,7 @@ func expireParse(cmd string, args []string) (*expireOpts, error) {
// converted to a duration.
func makeCmdExpire(m *Miniredis, unix bool, d time.Duration) func(*server.Peer, string, []string) {
return func(c *server.Peer, cmd string, args []string) {
if len(args) < 2 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, atLeast(2)) {
return
}

Expand Down Expand Up @@ -178,16 +170,7 @@ func makeCmdExpire(m *Miniredis, unix bool, d time.Duration) func(*server.Peer,
// [pexpiretime]: https://redis.io/commands/pexpiretime/
func (m *Miniredis) makeCmdExpireTime(timeResultStrategy func(time.Time) int) server.Cmd {
return func(c *server.Peer, cmd string, args []string) {
if len(args) != 1 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}

if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, exactly(1)) {
return
}

Expand All @@ -213,16 +196,7 @@ func (m *Miniredis) makeCmdExpireTime(timeResultStrategy func(time.Time) int) se

// TOUCH
func (m *Miniredis) cmdTouch(c *server.Peer, cmd string, args []string) {
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
return
}

if len(args) == 0 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
if !m.isValidCMD(c, cmd, args, atLeast(1)) {
return
}

Expand All @@ -241,15 +215,7 @@ func (m *Miniredis) cmdTouch(c *server.Peer, cmd string, args []string) {

// TTL
func (m *Miniredis) cmdTTL(c *server.Peer, cmd string, args []string) {
if len(args) != 1 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, exactly(1)) {
return
}

Expand All @@ -276,15 +242,7 @@ func (m *Miniredis) cmdTTL(c *server.Peer, cmd string, args []string) {

// PTTL
func (m *Miniredis) cmdPTTL(c *server.Peer, cmd string, args []string) {
if len(args) != 1 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, exactly(1)) {
return
}

Expand All @@ -311,15 +269,7 @@ func (m *Miniredis) cmdPTTL(c *server.Peer, cmd string, args []string) {

// PERSIST
func (m *Miniredis) cmdPersist(c *server.Peer, cmd string, args []string) {
if len(args) != 1 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, exactly(1)) {
return
}

Expand Down Expand Up @@ -347,16 +297,7 @@ func (m *Miniredis) cmdPersist(c *server.Peer, cmd string, args []string) {

// DEL and UNLINK
func (m *Miniredis) cmdDel(c *server.Peer, cmd string, args []string) {
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
return
}

if len(args) == 0 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
if !m.isValidCMD(c, cmd, args, atLeast(1)) {
return
}

Expand All @@ -376,15 +317,7 @@ func (m *Miniredis) cmdDel(c *server.Peer, cmd string, args []string) {

// TYPE
func (m *Miniredis) cmdType(c *server.Peer, cmd string, args []string) {
if len(args) != 1 {
setDirty(c)
c.WriteError("usage error")
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, exactly(1)) {
return
}

Expand All @@ -405,15 +338,7 @@ func (m *Miniredis) cmdType(c *server.Peer, cmd string, args []string) {

// EXISTS
func (m *Miniredis) cmdExists(c *server.Peer, cmd string, args []string) {
if len(args) < 1 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, atLeast(1)) {
return
}

Expand All @@ -432,15 +357,7 @@ func (m *Miniredis) cmdExists(c *server.Peer, cmd string, args []string) {

// MOVE
func (m *Miniredis) cmdMove(c *server.Peer, cmd string, args []string) {
if len(args) != 2 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, exactly(2)) {
return
}

Expand Down Expand Up @@ -470,15 +387,7 @@ func (m *Miniredis) cmdMove(c *server.Peer, cmd string, args []string) {

// KEYS
func (m *Miniredis) cmdKeys(c *server.Peer, cmd string, args []string) {
if len(args) != 1 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, exactly(1)) {
return
}

Expand All @@ -497,15 +406,7 @@ func (m *Miniredis) cmdKeys(c *server.Peer, cmd string, args []string) {

// RANDOMKEY
func (m *Miniredis) cmdRandomkey(c *server.Peer, cmd string, args []string) {
if len(args) != 0 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, exactly(0)) {
return
}

Expand All @@ -529,15 +430,7 @@ func (m *Miniredis) cmdRandomkey(c *server.Peer, cmd string, args []string) {

// RENAME
func (m *Miniredis) cmdRename(c *server.Peer, cmd string, args []string) {
if len(args) != 2 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, exactly(2)) {
return
}

Expand All @@ -564,15 +457,7 @@ func (m *Miniredis) cmdRename(c *server.Peer, cmd string, args []string) {

// RENAMENX
func (m *Miniredis) cmdRenamenx(c *server.Peer, cmd string, args []string) {
if len(args) != 2 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, exactly(2)) {
return
}

Expand Down Expand Up @@ -658,15 +543,7 @@ func scanParse(cmd string, args []string) (*scanOpts, error) {

// SCAN
func (m *Miniredis) cmdScan(c *server.Peer, cmd string, args []string) {
if len(args) < 1 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, atLeast(1)) {
return
}

Expand Down Expand Up @@ -766,15 +643,7 @@ func copyParse(cmd string, args []string) (*copyOpts, error) {

// COPY
func (m *Miniredis) cmdCopy(c *server.Peer, cmd string, args []string) {
if len(args) < 2 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
if !m.isValidCMD(c, cmd, args, atLeast(2)) {
return
}

Expand Down
8 changes: 1 addition & 7 deletions cmd_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ import (

// Command 'INFO' from https://redis.io/commands/info/
func (m *Miniredis) cmdInfo(c *server.Peer, cmd string, args []string) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one and cmdSelect don't use handleAuth, but with isValidCMD they now do. That looks like it shouldn't change. Otherwise it looks like a nice change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these were using isValidCMD before my change, I just moved the args validation into it (no auth behaviour has changed here).

In any case, I've tested locally with a dockerised redis and these two commands do produce auth errors, so I think the current code is correct:

$ docker run -d --name redis -p 6379:6379 redis:6.2.7 --requirepass "SUPER_SECRET_PASSWORD"
$ docker exec -it redis redis-cli
127.0.0.1:6379> info
NOAUTH Authentication required.
127.0.0.1:6379> select 0
(error) NOAUTH Authentication required.
127.0.0.1:6379> 

if !m.isValidCMD(c, cmd) {
return
}

if len(args) > 1 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
if !m.isValidCMD(c, cmd, args, between(0, 1)) {
return
}

Expand Down
25 changes: 24 additions & 1 deletion miniredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,38 @@ func (m *Miniredis) SetError(msg string) {
m.srv.SetPreHook(cb)
}

type argRequirements struct {
minimum int
maximum *int
}

func atLeast(n int) argRequirements {
return argRequirements{n, nil}
}

func between(a int, b int) argRequirements {
return argRequirements{a, &b}
}

func exactly(n int) argRequirements {
return argRequirements{n, &n}
}

// isValidCMD returns true if command is valid and can be executed.
func (m *Miniredis) isValidCMD(c *server.Peer, cmd string) bool {
func (m *Miniredis) isValidCMD(c *server.Peer, cmd string, args []string, argReqs argRequirements) bool {
if !m.handleAuth(c) {
return false
}
if m.checkPubsub(c, cmd) {
return false
}

if len(args) < argReqs.minimum || (argReqs.maximum != nil && len(args) > *argReqs.maximum) {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return false
}

return true
}

Expand Down
Loading