Skip to content
11 changes: 11 additions & 0 deletions internal/tests/integration/javascript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func javascriptDockerDisabled() bool {

var jsTestcases = []integrationCase{
{Path: "protocols/javascript/redis-pass-brute.yaml", TestCase: &javascriptRedisPassBrute{}, DisableOn: javascriptDockerDisabled, Serial: true},
{Path: "protocols/javascript/redis-lua-script.yaml", TestCase: &javascriptRedisLuaScript{}, DisableOn: javascriptDockerDisabled, Serial: true},
{Path: "protocols/javascript/ssh-server-fingerprint.yaml", TestCase: &javascriptSSHServerFingerprint{}, DisableOn: javascriptDockerDisabled, Serial: true},
{Path: "protocols/javascript/net-multi-step.yaml", TestCase: &networkMultiStep{}},
{Path: "protocols/javascript/net-https.yaml", TestCase: &javascriptNetHttps{}},
Expand Down Expand Up @@ -107,6 +108,16 @@ func (j *javascriptRedisPassBrute) Execute(filePath string) error {
}, javascriptServiceReadyTimeout, 0, nil))
}

type javascriptRedisLuaScript struct{}

func (j *javascriptRedisLuaScript) Execute(filePath string) error {
return runJavascriptDockerCase(filePath, newJavascriptDockerSpec("6379/tcp", &dockertest.RunOptions{
Repository: "redis",
Tag: "latest",
Cmd: []string{"redis-server", "--requirepass", "iamadmin"},
Comment thread
Mzack9999 marked this conversation as resolved.
Resolved
}, javascriptServiceReadyTimeout, 0, nil))
}

type javascriptSSHServerFingerprint struct{}

func (j *javascriptSSHServerFingerprint) Execute(filePath string) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
id: redis-lua-script

info:
name: Redis RunLuaScript - Detect
author: DhiyaneshDK
severity: info

javascript:
- code: |
const redis = require('nuclei/redis');
// Set a key via KEYS with a string and a numeric arg via ARGV: validates
// that keys are passed through and non-string args are not dropped
const setResult = redis.RunLuaScript(Host, Port, Password, 'return redis.call("set", KEYS[1], ARGV[1] .. ":" .. ARGV[2])', ['testkey'], ['testvalue', 123]);
log(to_json(setResult));

// Read the value back via KEYS
const getResult = redis.RunLuaScript(Host, Port, Password, 'return redis.call("get", KEYS[1])', ['testkey'], []);
log(to_json(getResult));
getResult;

args:
Host: "{{Host}}"
Port: "6379"
# must match the --requirepass value of the dockerized redis instance
# started by internal/tests/integration/javascript_test.go
Password: "iamadmin"

matchers:
- type: dsl
dsl:
- 'success == true && contains(response, "testvalue:123")'
7 changes: 5 additions & 2 deletions pkg/js/generated/ts/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ export function IsAuthenticated(host: string, port: number): boolean | null {
* @example
* ```javascript
* const redis = require('nuclei/redis');
* const result = redis.RunLuaScript('acme.com', 6379, 'password', 'return redis.call("get", KEYS[1])');
* // Old signature (backwards compatible) - keys and args are optional
* const result = redis.RunLuaScript('acme.com', 6379, 'password', 'return redis.call("ping")');
* // New signature with keys and args
* const result = redis.RunLuaScript('acme.com', 6379, 'password', 'return redis.call("get", KEYS[1])', ['mykey'], []);
* ```
*/
export function RunLuaScript(host: string, port: number, password: string, script: string): any | null {
export function RunLuaScript(host: string, port: number, password: string, script: string, keys?: string[] | any, args?: string[] | any): any | null {
return null;
}

61 changes: 57 additions & 4 deletions pkg/js/libs/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,12 @@ func isAuthenticated(ctx context.Context, executionId string, host string, port
// @example
// ```javascript
// const redis = require('nuclei/redis');
// const result = redis.RunLuaScript('acme.com', 6379, 'password', 'return redis.call("get", KEYS[1])');
// // Old signature (backwards compatible) - keys and args are optional
// const result = redis.RunLuaScript('acme.com', 6379, 'password', 'return redis.call("ping")');
// // New signature with keys and args
// const result = redis.RunLuaScript('acme.com', 6379, 'password', 'return redis.call("get", KEYS[1])', ['mykey'], []);
// ```
func RunLuaScript(ctx context.Context, host string, port int, password string, script string) (interface{}, error) {
func RunLuaScript(ctx context.Context, host string, port int, password string, script string, keys interface{}, args interface{}) (interface{}, error) {
executionId := ctx.Value("executionId").(string)
if !protocolstate.IsHostAllowed(executionId, host) {
// host is not valid according to network policy
Expand All @@ -199,8 +202,58 @@ func RunLuaScript(ctx context.Context, host string, port int, password string, s
return "", err
}

// Get Redis server info
infoCmd := client.Eval(ctx, script, []string{})
// Convert interface{} to []string for keys (handle backwards compatibility)
keysSlice := []string{}
if keys != nil {
switch v := keys.(type) {
case []string:
keysSlice = v
case []interface{}:
keysSlice = make([]string, 0, len(v))
for _, item := range v {
keysSlice = append(keysSlice, fmt.Sprintf("%v", item))
}
case string:
// the goja runtime zero-fills omitted trailing JS arguments with
// the previous parameter's type, so a 4-arg call (old signature)
// delivers "" here: treat it as no keys (backwards compat)
if v != "" {
return nil, fmt.Errorf("keys must be []string or []interface{}, got string")
}
default:
return nil, fmt.Errorf("keys must be []string or []interface{}, got %T", keys)
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// Convert interface{} args to []interface{} for Eval (handle backwards
// compatibility). Non-string items (numbers, booleans from JavaScript)
// are stringified rather than dropped: Redis ARGV values are always
// bulk strings on the Lua side, so "123"/"true" match JS expectations.
argsInterface := []interface{}{}
if args != nil {
switch v := args.(type) {
case []string:
argsInterface = make([]interface{}, len(v))
for i, arg := range v {
argsInterface[i] = arg
}
case []interface{}:
argsInterface = make([]interface{}, len(v))
for i, item := range v {
argsInterface[i] = fmt.Sprintf("%v", item)
}
case string:
// zero-value padding from omitted JS arguments (see keys above)
if v != "" {
return nil, fmt.Errorf("args must be []string or []interface{}, got string")
}
default:
return nil, fmt.Errorf("args must be []string or []interface{}, got %T", args)
}
}

// Execute the Lua script with keys and args
infoCmd := client.Eval(ctx, script, keysSlice, argsInterface...)

if infoCmd.Err() != nil {
return "", infoCmd.Err()
Expand Down
Loading