Skip to content
Open
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
2 changes: 2 additions & 0 deletions goldens/public-api/common/errors.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const enum RuntimeErrorCode {
// (undocumented)
EQUALITY_NG_SWITCH_DIFFERENCE = 2001,
// (undocumented)
INVALID_DATE_FORMAT = 2312,
// (undocumented)
INVALID_DIGIT_INFO = 2306,
// (undocumented)
INVALID_INPUT = 2952,
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const enum RuntimeErrorCode {
VALUE_NOT_A_NUMBER = 2309,
UNKNOWN_ZONE_WIDTH = 2310,
INVALID_TO_DATE_CONVERSION = 2311,
INVALID_DATE_FORMAT = 2312,

// Miscellaneous errors
SCROLL_RESTORATION_UNSUPPORTED = 2400,
Expand Down
15 changes: 15 additions & 0 deletions packages/common/src/i18n/format_date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const ISO8601_DATE_REGEX =
const NAMED_FORMATS: {[localeId: string]: {[format: string]: string}} = {};
const DATE_FORMATS_SPLIT =
/((?:[^BEGHLMOSWYZabcdhmswyz']+)|(?:'(?:[^']|'')*')|(?:G{1,5}|y{1,4}|Y{1,4}|M{1,5}|L{1,5}|w{1,2}|W{1}|d{1,2}|E{1,6}|c{1,6}|a{1,5}|b{1,5}|B{1,5}|h{1,2}|H{1,2}|m{1,2}|s{1,2}|S{1,3}|z{1,4}|Z{1,5}|O{1,4}))([\s\S]*)/;
const MAX_DATE_FORMAT_LENGTH = 1024;

const enum ZoneWidth {
Short,
Expand Down Expand Up @@ -89,8 +90,12 @@ export function formatDate(
timezone?: string,
): string {
let date = toDate(value);
assertValidDateFormatLength(format);
const namedFormat = getNamedFormat(locale, format);
format = namedFormat || format;
if (namedFormat) {
assertValidDateFormatLength(format);
}

let parts: string[] = [];
let match;
Expand Down Expand Up @@ -132,6 +137,16 @@ export function formatDate(
return text;
}

function assertValidDateFormatLength(format: string) {
if (format.length > MAX_DATE_FORMAT_LENGTH) {
throw new RuntimeError(
RuntimeErrorCode.INVALID_DATE_FORMAT,
ngDevMode &&
`Date format is too long. Exceeded maximum length of ${MAX_DATE_FORMAT_LENGTH} characters.`,
);
}
}

/**
* Asserts that the given date format is free from common mistakes. Throws an
* error if one is found (except for the case of all "Y", in which case we just
Expand Down
10 changes: 10 additions & 0 deletions packages/common/test/i18n/format_date_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,16 @@ describe('Format date', () => {
});
});

it('should throw if the date format exceeds 1024 characters to prevent DoS', () => {
const expectedError = /Exceeded maximum length of 1024 characters/;
expect(() => formatDate(date, 'y'.repeat(1025), ɵDEFAULT_LOCALE_ID)).toThrowError(
expectedError,
);
expect(() => formatDate(date, "'literal'".repeat(129), ɵDEFAULT_LOCALE_ID)).toThrowError(
expectedError,
);
});

it('should format invalid in IE ISO date', () =>
expect(formatDate('2017-01-11T12:00:00.014-0500', defaultFormat, ɵDEFAULT_LOCALE_ID)).toEqual(
'Jan 11, 2017',
Expand Down
6 changes: 6 additions & 0 deletions packages/common/test/pipes/date_pipe_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ describe('DatePipe', () => {
it('should give precedence to the passed in format', () =>
expect(pipe.transform('2017-01-11T10:14:39+0000', 'shortDate')).toEqual('1/11/17'));

it('should reject date formats that are too long', () => {
expect(() => pipe.transform(date, 'y'.repeat(1025))).toThrowError(
/InvalidPipeArgument: 'NG02312: Date format is too long/,
);
});

it('should use format provided in component as default format when no format is passed in', () => {
@Component({
selector: 'test-component',
Expand Down
Loading