Skip to content

Commit

Permalink
Outbound link previews
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn-Signal authored and josh-signal committed Oct 12, 2020
1 parent bb3ab81 commit 313faab
Show file tree
Hide file tree
Showing 25 changed files with 2,134 additions and 639 deletions.
4 changes: 4 additions & 0 deletions .storybook/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ module.exports = ({ config }) => {

config.resolve.extensions = ['.tsx', '.ts', '.jsx', '.js'];

config.externals = {
net: 'net',
};

return config;
};
47 changes: 24 additions & 23 deletions ACKNOWLEDGMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ Signal Desktop makes use of the following open source projects.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## abort-controller

MIT License

Copyright (c) 2017 Toru Nagashima

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## array-move

MIT License
Expand Down Expand Up @@ -1145,29 +1169,6 @@ Signal Desktop makes use of the following open source projects.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## he

Copyright Mathias Bynens <https://mathiasbynens.be/>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

## history

MIT License
Expand Down
180 changes: 6 additions & 174 deletions js/modules/link_previews.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
/* global URL */

const { isNumber, compact, isEmpty } = require('lodash');
const he = require('he');
const { isIP } = require('net');
const nodeUrl = require('url');
const LinkifyIt = require('linkify-it');

const linkify = LinkifyIt();
const { concatenateBytes, getViewOfArrayBuffer } = require('../../ts/Crypto');

module.exports = {
assembleChunks,
findLinks,
getChunkPattern,
getDomain,
getTitleMetaTag,
getImageMetaTag,
isLinkSafeToPreview,
isLinkInWhitelist,
isMediaLinkInWhitelist,
isLinkSneaky,
isStickerPack,
};
Expand All @@ -32,101 +25,10 @@ function isLinkSafeToPreview(link) {
return url.protocol === 'https:' && !isLinkSneaky(link);
}

const SUPPORTED_DOMAINS = [
'youtube.com',
'www.youtube.com',
'm.youtube.com',
'youtu.be',
'reddit.com',
'www.reddit.com',
'm.reddit.com',
'imgur.com',
'www.imgur.com',
'm.imgur.com',
'instagram.com',
'www.instagram.com',
'm.instagram.com',
'pinterest.com',
'www.pinterest.com',
'pin.it',
'signal.art',
];

// This function will soon be removed in favor of `isLinkSafeToPreview`. It is
// currently used because outbound-from-Desktop link previews only support a
// few domains (see the list above). We will soon remove this restriction to
// allow link previews from all domains, making this function obsolete.
function isLinkInWhitelist(link) {
try {
const url = new URL(link);

if (url.protocol !== 'https:') {
return false;
}

if (!url.pathname || url.pathname.length < 2) {
return false;
}

const lowercase = url.host.toLowerCase();
if (!SUPPORTED_DOMAINS.includes(lowercase)) {
return false;
}

return true;
} catch (error) {
return false;
}
}

function isStickerPack(link) {
return (link || '').startsWith('https://signal.art/addstickers/');
}

const SUPPORTED_MEDIA_DOMAINS = /^([^.]+\.)*(ytimg\.com|cdninstagram\.com|redd\.it|imgur\.com|fbcdn\.net|pinimg\.com)$/i;

// This function will soon be removed. See the comment in `isLinkInWhitelist`
// for more info.
function isMediaLinkInWhitelist(link) {
try {
const url = new URL(link);

if (url.protocol !== 'https:') {
return false;
}

if (!url.pathname || url.pathname.length < 2) {
return false;
}

if (!SUPPORTED_MEDIA_DOMAINS.test(url.host)) {
return false;
}

return true;
} catch (error) {
return false;
}
}

const META_TITLE = /<meta\s+property="og:title"[^>]+?content="([\s\S]+?)"[^>]*>/im;
const META_IMAGE = /<meta\s+property="og:image"[^>]+?content="([\s\S]+?)"[^>]*>/im;
function _getMetaTag(html, regularExpression) {
const match = regularExpression.exec(html);
if (match && match[1]) {
return he.decode(match[1]).trim();
}

return null;
}

function getTitleMetaTag(html) {
return _getMetaTag(html, META_TITLE);
}
function getImageMetaTag(html) {
return _getMetaTag(html, META_IMAGE);
}

function findLinks(text, caretLocation) {
const haveCaretLocation = isNumber(caretLocation);
const textLength = text ? text.length : 0;
Expand Down Expand Up @@ -169,81 +71,6 @@ function getDomain(url) {
}
}

const MB = 1024 * 1024;
const KB = 1024;

function getChunkPattern(size, initialOffset) {
if (size > MB) {
return _getRequestPattern(size, MB, initialOffset);
}
if (size > 500 * KB) {
return _getRequestPattern(size, 500 * KB, initialOffset);
}
if (size > 100 * KB) {
return _getRequestPattern(size, 100 * KB, initialOffset);
}
if (size > 50 * KB) {
return _getRequestPattern(size, 50 * KB, initialOffset);
}
if (size > 10 * KB) {
return _getRequestPattern(size, 10 * KB, initialOffset);
}
if (size > KB) {
return _getRequestPattern(size, KB, initialOffset);
}

return {
start: {
start: initialOffset,
end: size - 1,
},
};
}

function _getRequestPattern(size, increment, initialOffset) {
const results = [];

let offset = initialOffset || 0;
while (size - offset > increment) {
results.push({
start: offset,
end: offset + increment - 1,
overlap: 0,
});
offset += increment;
}

if (size - offset > 0) {
results.push({
start: size - increment,
end: size - 1,
overlap: increment - (size - offset),
});
}

return results;
}

function assembleChunks(chunkDescriptors) {
const chunks = chunkDescriptors.map((chunk, index) => {
if (index !== chunkDescriptors.length - 1) {
return chunk.data;
}

if (!chunk.overlap) {
return chunk.data;
}

return getViewOfArrayBuffer(
chunk.data,
chunk.overlap,
chunk.data.byteLength
);
});

return concatenateBytes(...chunks);
}

const ASCII_PATTERN = new RegExp('[\\u0020-\\u007F]', 'g');

function isLinkSneaky(link) {
Expand Down Expand Up @@ -272,6 +99,11 @@ function isLinkSneaky(link) {
return true;
}

// Domain cannot be an IP address.
if (isIP(domain)) {
return true;
}

// There must be at least 2 domain labels, and none of them can be empty.
const labels = domain.split('.');
if (labels.length < 2 || labels.some(isEmpty)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"dependencies": {
"@journeyapps/sqlcipher": "https://github.com/scottnonnenberg-signal/node-sqlcipher.git#b10f232fac62ba7f8775c9e086bb5558fe7d948b",
"@sindresorhus/is": "0.8.0",
"abort-controller": "3.0.0",
"array-move": "2.1.0",
"backbone": "1.3.3",
"blob-util": "1.3.0",
Expand All @@ -88,7 +89,6 @@
"glob": "7.1.6",
"google-libphonenumber": "3.2.6",
"got": "8.2.0",
"he": "1.2.0",
"history": "4.9.0",
"intl-tel-input": "12.1.15",
"jquery": "3.5.0",
Expand Down
12 changes: 12 additions & 0 deletions preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,20 @@ try {
});

if (config.environment === 'test') {
// This is a hack to let us run TypeScript tests in the renderer process. See the
// code in `test/index.html`.
const pendingDescribeCalls = [];
window.describe = (...args) => {
pendingDescribeCalls.push(args);
};

/* eslint-disable global-require, import/no-extraneous-dependencies */
require('./ts/test-electron/linkPreviews/linkPreviewFetch_test');

delete window.describe;

window.test = {
pendingDescribeCalls,
fastGlob: require('fast-glob'),
normalizePath: require('normalize-path'),
fse: require('fs-extra'),
Expand Down
3 changes: 2 additions & 1 deletion protos/SignalService.proto
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,9 @@ message SyncMessage {
optional bool readReceipts = 1;
optional bool unidentifiedDeliveryIndicators = 2;
optional bool typingIndicators = 3;
optional bool linkPreviews = 4;
// 4 is reserved
optional uint32 provisioningVersion = 5;
optional bool linkPreviews = 6;
}

message StickerPackOperation {
Expand Down
4 changes: 3 additions & 1 deletion protos/SignalStorage.proto
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,7 @@ message AccountRecord {
optional bool readReceipts = 6;
optional bool sealedSenderIndicators = 7;
optional bool typingIndicators = 8;
optional bool linkPreviews = 9;
optional bool proxiedLinkPreviews = 9;
optional bool noteToSelfUnread = 10;
optional bool linkPreviews = 11;
}
Loading

0 comments on commit 313faab

Please sign in to comment.