diff --git a/functions/index.ts b/functions/index.ts index 0eec464ea..ae802ae29 100644 --- a/functions/index.ts +++ b/functions/index.ts @@ -1,4 +1,4 @@ -import { getProjectInfo, type Context, type PgFunction } from './utils.ts'; +import { encodeHTML, getProjectInfo, type Context, type PgFunction } from './utils.ts'; export const onRequest: PgFunction = async function (context) { const { request, env } = context; @@ -48,7 +48,7 @@ export const onRequest: PgFunction = async function (context) { .replace( /title" content="LiveCodes"/g, `title" content="${ - !title || title === 'Untitled Project' ? 'LiveCodes' : title + ' - LiveCodes' + !title || title === 'Untitled Project' ? 'LiveCodes' : encodeHTML(title) + ' - LiveCodes' }"`, ) .replace( @@ -56,7 +56,7 @@ export const onRequest: PgFunction = async function (context) { `content="${ !title && !description ? 'A Code Playground That Just Works!' - : description || 'A project on LiveCodes.' + : encodeHTML(description || 'A project on LiveCodes.') }"`, ) .replace(/content="https:\/\/livecodes.io\/"/g, `content="${request.url}"`) @@ -77,7 +77,7 @@ export const onRequest: PgFunction = async function (context) { context.waitUntil(logToAPI(context)); return response; - } catch (err) { + } catch (err: any) { context.data = { ...data, ok: false, diff --git a/functions/utils.ts b/functions/utils.ts index 7cf8e7b9e..e2772f8fb 100644 --- a/functions/utils.ts +++ b/functions/utils.ts @@ -80,3 +80,11 @@ export const getProjectInfo = async (url: URL): Promise => { description: '', }; }; + +export const encodeHTML = (html: string) => + html + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/'/g, ''') + .replace(/"/g, '"');