Fix raw json validation (#15931)

Before validation in common layer, stringified json were accepted in raw
json field. Fix regression

related to
https://discord.com/channels/1130383047699738754/1130383048173682821/1440723288090218636
This commit is contained in:
Etienne
2025-11-19 19:05:02 +01:00
committed by GitHub
parent 0223851620
commit 674ddbd525
7 changed files with 54 additions and 10 deletions
@@ -6,12 +6,12 @@ import { transformRawJsonField } from 'src/engine/api/common/common-args-process
export const transformActorField = (
value: {
source?: FieldActorSource | null;
context?: object | null;
context?: object | string | null;
} | null,
isNullEquivalenceEnabled: boolean = false,
): {
source?: FieldActorSource | null;
context?: object | null;
context?: object | string | null;
} | null => {
if (isNull(value)) return null;
@@ -1,9 +1,10 @@
//Json.parse() for RawJsonField is done in formatFieldMetadataValue in ORM
import { isNull } from '@sniptt/guards';
export const transformRawJsonField = (
value: object | null,
value: object | string | null,
isNullEquivalenceEnabled: boolean = false,
): object | null => {
): object | string | null => {
return isNullEquivalenceEnabled &&
!isNull(value) &&
Object.keys(value).length === 0
@@ -28,6 +28,20 @@ describe('validateRawJsonFieldOrThrow', () => {
expect(result).toEqual(jsonArray);
});
it('should accept a valid JSON string', () => {
const jsonString = '{"key":"value","nested":{"prop":123}}';
const result = validateRawJsonFieldOrThrow(jsonString, 'testField');
expect(result).toBe(jsonString);
});
it('should accept a valid JSON array string', () => {
const jsonArrayString = '[1, 2, 3, "test"]';
const result = validateRawJsonFieldOrThrow(jsonArrayString, 'testField');
expect(result).toBe(jsonArrayString);
});
});
describe('invalid inputs', () => {
@@ -51,10 +65,16 @@ describe('validateRawJsonFieldOrThrow', () => {
);
});
it('should throw when value is a string', () => {
it('should throw when value is an invalid JSON string', () => {
expect(() =>
validateRawJsonFieldOrThrow('string value', 'testField'),
validateRawJsonFieldOrThrow('not valid json', 'testField'),
).toThrow(CommonQueryRunnerException);
});
it('should throw when value is a boolean', () => {
expect(() => validateRawJsonFieldOrThrow(true, 'testField')).toThrow(
CommonQueryRunnerException,
);
});
});
});
@@ -10,9 +10,22 @@ import {
export const validateRawJsonFieldOrThrow = (
value: unknown,
fieldName: string,
): object | null => {
): object | string | null => {
if (isNull(value)) return null;
if (typeof value === 'string') {
try {
JSON.parse(value);
} catch {
throw new CommonQueryRunnerException(
`Invalid object value ${inspect(value)} for field "${fieldName}"`,
CommonQueryRunnerExceptionCode.INVALID_ARGS_DATA,
);
}
return value;
}
if (!isObject(value)) {
throw new CommonQueryRunnerException(
`Invalid object value ${inspect(value)} for field "${fieldName}"`,