Fix embed url param parsing

This commit is contained in:
Rafael 2022-01-31 12:24:38 -03:00 committed by Thomas Zarebczan
parent 92f0fd8745
commit b30a8568e5
3 changed files with 27 additions and 28 deletions

View file

@ -3,7 +3,7 @@ import * as ICONS from 'constants/icons';
import { FormField } from 'component/common/form';
import Button from 'component/button';
import React, { useRef } from 'react';
import { generateEmbedUrl, generateEmbedIframeData } from 'util/web';
import { generateEmbedUrlEncoded, generateEmbedIframeData } from 'util/web';
type Props = {
copyable: string,
@ -21,7 +21,7 @@ export default function EmbedTextArea(props: Props) {
const { claim_id: claimId, name } = claim;
const input = useRef();
const streamUrl = generateEmbedUrl(name, claimId, includeStartTime, startTime, referralCode);
const streamUrl = generateEmbedUrlEncoded(name, claimId, includeStartTime && startTime, referralCode);
const { html: embedText } = generateEmbedIframeData(streamUrl);
function copyToClipboard() {

View file

@ -9,9 +9,10 @@ function generateStreamUrl(claimName, claimId) {
.replace(/\)/g, '%29')}/${claimId}/stream`;
}
function generateEmbedUrl(claimName, claimId, includeStartTime, startTime, referralLink) {
function generateEmbedUrl(claimName, claimId, startTime, referralLink) {
let urlParams = new URLSearchParams();
if (includeStartTime && startTime) {
if (startTime) {
urlParams.append('t', startTime);
}
@ -19,26 +20,16 @@ function generateEmbedUrl(claimName, claimId, includeStartTime, startTime, refer
urlParams.append('r', referralLink);
}
return `${URL}/$/embed/${encodeURIComponent(claimName)
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')}/${claimId}?${urlParams.toString()}`;
const encodedUriName = encodeURIComponent(claimName).replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29');
const embedUrl = `${URL}/$/embed/${encodedUriName}/${claimId}`;
const embedUrlParams = urlParams.toString() ? `?${urlParams.toString()}` : '';
return `${embedUrl}${embedUrlParams}`;
}
function generateEmbedUrlEncoded(claimName, claimId, includeStartTime, startTime, referralLink) {
let urlParams = new URLSearchParams();
if (includeStartTime && startTime) {
urlParams.append('t', startTime);
}
if (referralLink) {
urlParams.append('r', referralLink);
}
return `${URL}/%24/embed/${encodeURIComponent(claimName)
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')}/${claimId}?${urlParams.toString()}`;
function generateEmbedUrlEncoded(claimName, claimId, startTime, referralLink) {
return generateEmbedUrl(claimName, claimId, startTime, referralLink).replace(/\$/g, '%24');
}
function generateEmbedIframeData(src) {

View file

@ -45,7 +45,7 @@ async function getClaim(requestUrl) {
// Generate
// ****************************************************************************
function generateOEmbedData(claim, referrerQuery) {
function generateOEmbedData(claim, embedlyReferrer, timestamp, referral) {
const { value, signing_channel: authorClaim } = claim;
const claimTitle = value.title;
@ -53,9 +53,10 @@ function generateOEmbedData(claim, referrerQuery) {
const authorUrlPath = authorClaim && authorClaim.canonical_url.replace('lbry://', '').replace('#', ':');
const authorUrl = authorClaim ? `${URL}/${authorUrlPath}` : null;
const thumbnailUrl = value && value.thumbnail && value.thumbnail.url && getThumbnailCdnUrl(value.thumbnail.url);
const embedUrl = generateEmbedUrlEncoded(claim.name, claim.claim_id, timestamp, referral);
const videoUrl =
generateEmbedUrlEncoded(claim.name, claim.claim_id) +
(referrerQuery ? `r=${encodeURIComponent(escapeHtmlProperty(referrerQuery))}` : '');
embedUrl + (embedlyReferrer ? `referrer=${encodeURIComponent(escapeHtmlProperty(embedlyReferrer))}` : '');
const { html, width, height } = generateEmbedIframeData(videoUrl);
@ -116,12 +117,19 @@ function generateXmlData(oEmbedData) {
async function getOEmbed(ctx) {
const requestUrl = ctx.request.url;
const urlQuery = getParameterByName('url', requestUrl);
const embedlyReferrer = getParameterByName('referrer', requestUrl);
const decodedQueryUri = decodeURIComponent(urlQuery);
const hasUrlParams = RegExp(/[?&]\w=/).test(decodedQueryUri);
const claimUrl = hasUrlParams ? decodedQueryUri.substring(0, decodedQueryUri.search(/[?&]\w=/)) : decodedQueryUri;
const { claim, error } = await getClaim(claimUrl);
const { claim, error } = await getClaim(urlQuery);
if (error) return error;
const referrerQuery = getParameterByName('referrer', requestUrl);
const oEmbedData = generateOEmbedData(claim, referrerQuery);
const queryTimestampParam = getParameterByName('t', decodedQueryUri);
const queryReferralParam = getParameterByName('r', decodedQueryUri);
const oEmbedData = generateOEmbedData(claim, embedlyReferrer, queryTimestampParam, queryReferralParam);
const formatQuery = getParameterByName('format', requestUrl);
if (formatQuery === 'xml') {