Skip to content

Commit b457cb4

Browse files
committed
validators: detect when an iframe redirects to canonical
1 parent 90168e3 commit b457cb4

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

lib/core.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,17 +1122,21 @@
11221122
var link = links[i];
11231123

11241124
// Remove canonical links.
1125-
if (typeof canonical === 'string' && link.href && typeof link.href === 'string' && link.rel && link.rel.indexOf(CONFIG.R.file) === -1 && link.rel.indexOf(CONFIG.R.iframely) === -1) {
1125+
if (typeof canonical === 'string' && link.href && typeof link.href === 'string'
1126+
&& link.rel && link.rel.indexOf(CONFIG.R.file) === -1
1127+
&& link.rel.indexOf(CONFIG.R.iframely) === -1 && link.rel.indexOf(CONFIG.R.oembed) == -1
1128+
&& !/\/embed\//i.test(canonical)) {
11261129

1127-
// Remove last / from url.
1130+
// Remove trailing `/`` from url.
11281131

11291132
var link1 = link.href.replace(/\/+$/, '');
11301133
var link2 = canonical.replace(/\/+$/, '');
1134+
var link3 = link._uri?.replace(/\/+$/, '');
11311135

1132-
if (link1 === link2 && link.rel.indexOf(CONFIG.R.oembed) == -1 && !/\/embed\//i.test(canonical)) {
1133-
// allow the canonical links for oEmbeds, as such mistakes are usually made for OG and Twitter:
1134-
// if publisher has oEmbed, he is most likely to have the valid embed codes
1135-
link.error = "Removed canonical link";
1136+
if (link1 === link2) {
1137+
link.error = "Canonical link is removed unlesss explicitely allowed";
1138+
} else if (link3 === link2) {
1139+
link.error = "Link redirected to the canonical";
11361140
}
11371141
}
11381142

@@ -1182,7 +1186,7 @@
11821186
link.sourceId = i;
11831187
}
11841188

1185-
if ('_imageMeta' in link || '_imageStatus' in link) {
1189+
if ('_imageMeta' in link || '_imageStatus' in link || '_uri' in link) {
11861190

11871191
var newLink;
11881192
if (options.debug) {
@@ -1193,6 +1197,7 @@
11931197

11941198
delete newLink._imageMeta;
11951199
delete newLink._imageStatus;
1200+
delete newLink._uri;
11961201
links[i] = newLink;
11971202
}
11981203

lib/plugins/validators/async/21_checkContentType.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export default {
134134

135135
if (!link.error && data.url !== link.href) {
136136

137+
// We'll use _url in the core to validate href against redirects to canonical
138+
link._uri = data.url;
139+
137140
// Catch https -> http redirects for iFrames
138141
if (/^(?:https:)?\/\//.test(link.href) && /^http:\/\//.test(data.url)) {
139142
link.href = data.url;

0 commit comments

Comments
 (0)