Skip to content

Add validation for dimension values #131

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 7 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 12 additions & 12 deletions src/serializers/LogSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,21 @@ export class LogSerializer implements ISerializer {
const dimensionKeys: string[][] = [];
let dimensionProperties = {};

context.getDimensions().forEach(d => {
// we can only take the first 9 defined dimensions
// the reason we do this in the serializer is because
// it is possible that other sinks or formats can
// support more dimensions
// in the future it may make sense to introduce a higher-order
// representation for sink-specific validations
const keys = Object.keys(d);
context.getDimensions().forEach(dimensionSet => {
const keys = Object.keys(dimensionSet);

if (keys.length > Constants.MAX_DIMENSION_SET_SIZE) {
const errMsg = `Maximum number of dimensions allowed are ${Constants.MAX_DIMENSION_SET_SIZE}.` +
`Account for default dimensions if not using set_dimensions.`;
throw new DimensionSetExceededError(errMsg)
const errMsg =
`Maximum number of dimensions allowed are ${Constants.MAX_DIMENSION_SET_SIZE}.` +
`Account for default dimensions if not using set_dimensions.`;
throw new DimensionSetExceededError(errMsg);
}

// Stringify and remove non-ascii characeters (allow only 0x20-0x7E)
keys.forEach(key => (dimensionSet[key] = String(dimensionSet[key]).replace(/[^\x20-\x7F]/g, '')));

dimensionKeys.push(keys);
dimensionProperties = { ...dimensionProperties, ...d };
dimensionProperties = { ...dimensionProperties, ...dimensionSet };
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
43 changes: 42 additions & 1 deletion src/serializers/__tests__/LogSerializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ test('cannot serialize more than 30 dimensions', () => {

// assert
expect(() => {
serializer.serialize(context)
serializer.serialize(context);
}).toThrow(DimensionSetExceededError);
});

Expand All @@ -189,6 +189,47 @@ const assertJsonEquality = (resultJson: string, expectedObj: any) => {
expect(actual).toStrictEqual(expectedObj);
};

test('remove non-ascii characters from dimension values', () => {
// arrange
const key = faker.random.word();
const value = '¡é詵ascii🚀♞∑';
const expectedValue = 'ascii';
const dimensions: any = {};
const expectedDimensions: any = {};
expectedDimensions[key] = expectedValue;
dimensions[key] = value;

const expected: any = { ...getEmptyPayload(), ...expectedDimensions };
expected._aws.CloudWatchMetrics[0].Dimensions.push([key]);

const context = getContext();
context.putDimensions(dimensions);

// act
const resultJson = serializer.serialize(context)[0];

// assert
assertJsonEquality(resultJson, expected);
});

test('convert dimension values to strings', () => {
// arrange
const key = faker.random.word();
const value = faker.random.number();
const expectedValue = String(value);
const dimensions: any = {};
dimensions[key] = value;

const context = getContext();
context.putDimensions(dimensions);

// act
const resultJson = serializer.serialize(context)[0];

// assert
expect(JSON.parse(resultJson)[key]).toStrictEqual(expectedValue);
});

const getEmptyPayload = () => {
return Object.assign(
{},
Expand Down