OP-Engineering / link-preview-js

⛓ Extract web links information: title, description, images, videos, etc. [via OpenGraph], runs on mobiles and node.
MIT License
770 stars 124 forks source link

getVideos crashes when there are video tags but no type or secure_url tags #131

Closed lautarovc closed 2 years ago

lautarovc commented 2 years ago

Describe the bug When a webpage has an "og:video" meta tag, but it doesn't have any "og:video:type" or "og:video:secure_url" meta tags, the library crashes with a "Error: link-preview-js could not fetch link information TypeError: Cannot read property '0' of null".

By browsing the code, I found that in the getVideos function, the nodeTypes and nodeSecureUrls variables can be null. Later, in the same function, there's a loop that goes over the video tags and unsafely accesses the corresponding index of nodeTypes and nodeSecureUrls, crashing in this particular case.

To Reproduce Steps to reproduce the behavior:

  1. Try to access https://newpathtitle.com/falling-markets-how-to-stop-buyer-from-getting-out/ with link preview. This is the example that crashed for me.

Expected behavior Not crashing. Either the metaTag function returns an empty array when no tags found, or access the nodeTypes inside the loop in the getVideos function in a safer way, e.g: nodeType = nodeTypes ? nodeTypes[index] : ''; and nodeSecureUrl = nodeSecureUrls ? nodeSecureUrls[index] : '';

Additional context Here's the getVideos function: https://github.com/ospfranco/link-preview-js/blob/3ef3f895cd003af2046a84241db58f1cbf665939/index.ts#L132

Here are the lines that crash: https://github.com/ospfranco/link-preview-js/blob/3ef3f895cd003af2046a84241db58f1cbf665939/index.ts#L167 https://github.com/ospfranco/link-preview-js/blob/3ef3f895cd003af2046a84241db58f1cbf665939/index.ts#L172

Thank you for your work! 😀

ospfranco commented 2 years ago

it seems you got the issue pretty much figured out :) care to submit a PR?

lautarovc commented 2 years ago

Sure! Done 🙂: https://github.com/ospfranco/link-preview-js/pull/132

ospfranco commented 2 years ago

published in 3.0.4