Skip to content

Commit 336ee27

Browse files
authored
Merge pull request #1499 from RedisInsight/be/feature/RI-3898_Without_JSON.DEGUG
#RI-3898 - Do not use JSON.DEBUG MEMORY when it is not allowed
2 parents e94f1d1 + 732a717 commit 336ee27

File tree

3 files changed

+102
-26
lines changed

3 files changed

+102
-26
lines changed

redisinsight/api/src/modules/browser/services/rejson-rl-business/rejson-rl-business.service.spec.ts

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,23 +172,6 @@ describe('JsonBusinessService', () => {
172172
expect(err).toBeInstanceOf(BadRequestException);
173173
}
174174
});
175-
it('should throw Forbidden error when no perms for an action for getJson', async () => {
176-
const replyError: ReplyError = {
177-
...mockRedisNoPermError,
178-
command: 'JSON.DEBUG',
179-
};
180-
browserTool.execCommand.mockRejectedValue(replyError);
181-
182-
try {
183-
await service.getJson(mockBrowserClientMetadata, {
184-
keyName: testKey,
185-
path: testPath,
186-
});
187-
fail();
188-
} catch (err) {
189-
expect(err).toBeInstanceOf(ForbiddenException);
190-
}
191-
});
192175
it('should throw BadRequest error when module not loaded for getJson', async () => {
193176
const replyError: ReplyError = {
194177
name: 'ReplyError',
@@ -414,6 +397,76 @@ describe('JsonBusinessService', () => {
414397
});
415398
});
416399
});
400+
401+
describe('user has no PERM for JSON.DEBUG', () => {
402+
beforeEach(() => {
403+
const replyError: ReplyError = {
404+
...mockRedisNoPermError,
405+
command: 'JSON.DEBUG',
406+
};
407+
408+
when(browserTool.execCommand)
409+
.calledWith(
410+
mockBrowserClientMetadata,
411+
BrowserToolRejsonRlCommands.JsonDebug,
412+
['MEMORY', testKey, testPath],
413+
)
414+
.mockRejectedValue(replyError);
415+
});
416+
417+
it('should return data (string)', async () => {
418+
const testData = 'some string';
419+
when(browserTool.execCommand)
420+
.calledWith(mockBrowserClientMetadata, BrowserToolRejsonRlCommands.JsonGet, [
421+
testKey,
422+
testPath,
423+
], 'utf8')
424+
.mockReturnValue(JSON.stringify(testData));
425+
426+
const result = await service.getJson(mockBrowserClientMetadata, {
427+
keyName: testKey,
428+
path: testPath,
429+
});
430+
431+
expect(result).toEqual({
432+
downloaded: true,
433+
path: testPath,
434+
data: testData,
435+
});
436+
});
437+
438+
it('should return full json value even if size is above the limit', async () => {
439+
const testData = {arr:[randomBytes(2000).toString('hex')]};
440+
when(browserTool.execCommand)
441+
.calledWith(mockBrowserClientMetadata, BrowserToolRejsonRlCommands.JsonGet, [
442+
testKey,
443+
testPath,
444+
], 'utf8')
445+
.mockReturnValue(JSON.stringify(testData));
446+
447+
when(browserTool.execCommand)
448+
.calledWith(
449+
mockBrowserClientMetadata,
450+
BrowserToolRejsonRlCommands.JsonType, [
451+
testKey,
452+
testPath,
453+
],
454+
'utf8',
455+
).mockReturnValue('object');
456+
457+
const result = await service.getJson(mockBrowserClientMetadata, {
458+
keyName: testKey,
459+
path: testPath,
460+
});
461+
462+
expect(result).toEqual({
463+
downloaded: true,
464+
path: testPath,
465+
data: testData,
466+
});
467+
});
468+
469+
});
417470
describe('partial json download', () => {
418471
beforeEach(() => {
419472
when(browserTool.execCommand)

redisinsight/api/src/modules/browser/services/rejson-rl-business/rejson-rl-business.service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,17 @@ export class RejsonRlBusinessService {
6161
keyName: RedisString,
6262
path: string,
6363
): Promise<number | null> {
64-
const size = await this.browserTool.execCommand(
65-
clientMetadata,
66-
BrowserToolRejsonRlCommands.JsonDebug,
67-
['MEMORY', keyName, path],
68-
);
64+
let size = 0
65+
66+
try {
67+
size = await this.browserTool.execCommand(
68+
clientMetadata,
69+
BrowserToolRejsonRlCommands.JsonDebug,
70+
['MEMORY', keyName, path],
71+
);
72+
} catch (error) {
73+
this.logger.error('Failed to estimate size of json.', error);
74+
}
6975

7076
if (size === null) {
7177
throw new BadRequestException(

redisinsight/api/test/api/rejson-rl/POST-databases-id-rejson_rl-get.test.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,34 @@ describe('POST /databases/:instanceId/rejson-rl/get', () => {
229229
before: () => rte.data.setAclUserRules('~* +@all -json.get')
230230
},
231231
{
232-
name: 'Should throw error if no permissions for "json.debug" command',
232+
name: 'Should return regular item if no permissions for "json.debug" command',
233233
endpoint: () => endpoint(constants.TEST_INSTANCE_ACL_ID),
234234
data: {
235235
keyName: constants.TEST_REJSON_KEY_3,
236236
path: '.',
237237
forceRetrieve: false,
238238
},
239-
statusCode: 403,
239+
responseSchema,
240240
responseBody: {
241-
statusCode: 403,
242-
error: 'Forbidden',
241+
downloaded: true,
242+
path: '.',
243+
data: constants.TEST_REJSON_VALUE_3,
244+
},
245+
before: () => rte.data.setAclUserRules('~* +@all -json.debug')
246+
},
247+
{
248+
name: 'Should get full json if no permissions for "json.debug" command',
249+
endpoint: () => endpoint(constants.TEST_INSTANCE_ACL_ID),
250+
data: {
251+
keyName: constants.TEST_REJSON_KEY_3,
252+
path: '.',
253+
forceRetrieve: false,
254+
},
255+
responseSchema,
256+
responseBody: {
257+
downloaded: true,
258+
path: '.',
259+
data: constants.TEST_REJSON_VALUE_3,
243260
},
244261
before: () => rte.data.setAclUserRules('~* +@all -json.debug')
245262
},

0 commit comments

Comments
 (0)