Skip to content

Commit

Permalink
fix: Unchecked undefined values in after timestamp rule
Browse files Browse the repository at this point in the history
  • Loading branch information
nickevansuk committed Sep 9, 2024
1 parent e3f9339 commit 3532871
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions src/rules/page/after-timestamp-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ class AfterTimestampRule extends RpdeRule {
const afterId = UrlHelper.getParam('afterId', node.data.next, node.url);
if (afterTimestamp !== null) {
const modified = _.get(node.data, 'items[0].modified');
if (modified.length !== 0) {
if (modified !== undefined && modified !== null && modified.length !== 0) {
if (
typeof modified === 'number'
|| modified.match(/^[1-9][0-9]*$/)
|| (typeof modified === 'string' && modified.match(/^[1-9][0-9]*$/))
) {
if (
typeof modified === 'string'
Expand All @@ -114,7 +114,7 @@ class AfterTimestampRule extends RpdeRule {
),
);
}
if (!afterTimestamp.match(/^[1-9][0-9]*$/)) {
if (typeof afterTimestamp === 'string' && !afterTimestamp.match(/^[1-9][0-9]*$/)) {
node.log.addPageError(
node.url,
this.createError(
Expand All @@ -137,7 +137,7 @@ class AfterTimestampRule extends RpdeRule {
},
),
);
if (!afterTimestamp.match(/^[1-9][0-9]*$/)) {
if (typeof afterTimestamp === 'string' && !afterTimestamp.match(/^[1-9][0-9]*$/)) {
node.log.addPageError(
node.url,
this.createError(
Expand All @@ -150,7 +150,7 @@ class AfterTimestampRule extends RpdeRule {
);
}
}
} else if (!afterTimestamp.match(/^[1-9][0-9]*$/)) {
} else if (typeof afterTimestamp === 'string' && !afterTimestamp.match(/^[1-9][0-9]*$/)) {
node.log.addPageError(
node.url,
this.createError(
Expand Down Expand Up @@ -186,40 +186,42 @@ class AfterTimestampRule extends RpdeRule {
}

// Do we have a last item that matches afterId and afterTimestamp?
if (node.data.items.length > 0) {
if (node.data.items && node.data.items.length > 0) {
const lastItem = _.get(node.data, `items[${node.data.items.length - 1}]`);
const lastItemModified = lastItem.modified;
const lastItemId = lastItem.id;
const lastItemCompare = {
afterTimestamp,
lastItemModified,
afterIdValue,
lastItemId,
};
for (const key in lastItemCompare) {
if (Object.prototype.hasOwnProperty.call(lastItemCompare, key)) {
if (
typeof lastItemCompare[key] === 'string'
&& lastItemCompare[key].match(/^[1-9][0-9]*$/)
) {
lastItemCompare[key] *= 1;
if (lastItem) {
const lastItemModified = lastItem.modified;
const lastItemId = lastItem.id;
const lastItemCompare = {
afterTimestamp,
lastItemModified,
afterIdValue,
lastItemId,
};
for (const key in lastItemCompare) {
if (Object.prototype.hasOwnProperty.call(lastItemCompare, key)) {
if (
typeof lastItemCompare[key] === 'string'
&& lastItemCompare[key].match(/^[1-9][0-9]*$/)
) {
lastItemCompare[key] *= 1;
}
}
}
}
if (
lastItemCompare.lastItemId !== lastItemCompare.afterIdValue
|| lastItemCompare.lastItemModified !== lastItemCompare.afterTimestamp
) {
node.log.addPageError(
node.url,
this.createError(
'lastItemMatch',
{
value: node.data,
url: node.url,
},
),
);
if (
lastItemCompare.lastItemId !== lastItemCompare.afterIdValue
|| lastItemCompare.lastItemModified !== lastItemCompare.afterTimestamp
) {
node.log.addPageError(
node.url,
this.createError(
'lastItemMatch',
{
value: node.data,
url: node.url,
},
),
);
}
}
}

Expand Down

0 comments on commit 3532871

Please sign in to comment.