Skip to content
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
55 changes: 55 additions & 0 deletions packages/client/lib/client/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,61 @@ describe('Client', () => {
}
});

describe('read-only scripts', () => {
testUtils.isVersionGreaterThanHook([7]);

const READ_ONLY_SQUARE_SCRIPT = defineScript({
...SQUARE_SCRIPT,
IS_READ_ONLY: true
});

const options = {
...GLOBAL.SERVERS.OPEN,
clientOptions: {
scripts: {
square: READ_ONLY_SQUARE_SCRIPT
}
}
};

// runs fn as a user that may run EVAL_RO and EVALSHA_RO but not EVAL or EVALSHA,
// after SCRIPT FLUSH, so the script has to be loaded again
async function asReadOnlyScriptsUser(
client: RedisClientType<{}, {}, { square: typeof READ_ONLY_SQUARE_SCRIPT }, 2>,
fn: (user: typeof client) => Promise<void>
) {
await client.sendCommand([
'ACL', 'SETUSER', 'read-only-scripts', 'on', '>password', '~*', '+@all', '-eval', '-evalsha'
]);
const user = client.duplicate({
username: 'read-only-scripts',
password: 'password'
});
await user.connect();

try {
await client.set('key', '2');
await client.scriptFlush();
await fn(user);
} finally {
user.destroy();
await client.sendCommand(['ACL', 'DELUSER', 'read-only-scripts']);
}
}

testUtils.testWithClient('loads a missing script with EVAL_RO', async client => {
await asReadOnlyScriptsUser(client, async user => {
assert.equal(await user.square('key'), 4);
});
}, options);

testUtils.testWithClient('uses EVAL_RO in MULTI', async client => {
await asReadOnlyScriptsUser(client, async user => {
assert.deepEqual(await user.multi().square('key').exec(), [4]);
});
}, options);
});

const module = {
echo: {
parseCommand(parser: CommandParser, message: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,7 @@ export default class RedisClient<
} catch (err) {
if (!(err as Error)?.message?.startsWith?.('NOSCRIPT')) throw err;

args[0] = 'EVAL';
args[0] = script.IS_READ_ONLY ? 'EVAL_RO' : 'EVAL';
args[1] = script.SCRIPT;
reply = await this.sendCommand(args, options);
}
Expand Down
18 changes: 18 additions & 0 deletions packages/client/lib/multi-command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ describe('Multi Command', () => {
['EVALSHA', SQUARE_SCRIPT.SHA1, '2']
);
});

it('should use EVAL_RO and EVALSHA_RO for a read-only script', () => {
const multi = new RedisMultiCommand(),
script = {
...SQUARE_SCRIPT,
IS_READ_ONLY: true
};

multi.addScript(script, ['1']);
multi.addScript(script, ['2']);
assert.deepEqual(
multi.queue.map(({ args }) => Array.from(args)),
[
['EVAL_RO', SQUARE_SCRIPT.SCRIPT, '1', '1'],
['EVALSHA_RO', SQUARE_SCRIPT.SHA1, '1', '2']
]
);
});
});

describe('exec', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/client/lib/multi-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export default class RedisMultiCommand {
const redisArgs: CommandArguments = [];
redisArgs.preserve = args.preserve;
if (this.scriptsInUse.has(script.SHA1)) {
redisArgs.push('EVALSHA', script.SHA1);
redisArgs.push(script.IS_READ_ONLY ? 'EVALSHA_RO' : 'EVALSHA', script.SHA1);
} else {
this.scriptsInUse.add(script.SHA1);
redisArgs.push('EVAL', script.SCRIPT);
redisArgs.push(script.IS_READ_ONLY ? 'EVAL_RO' : 'EVAL', script.SCRIPT);
}

if (script.NUMBER_OF_KEYS !== undefined) {
Expand Down
Loading