diff --git a/src/pages/api/library/autofill/cover.ts b/src/pages/api/library/autofill/cover.ts index 3c44cbc4..dbad4c84 100644 --- a/src/pages/api/library/autofill/cover.ts +++ b/src/pages/api/library/autofill/cover.ts @@ -16,6 +16,16 @@ const ALLOWED_HOSTS = [ /^img\.youtube\.com$/, ]; +// Raster types only. An SVG can carry script, and this route answers from our +// own origin, so anything else is refused. +const RASTER_TYPES = new Set([ + 'image/jpeg', + 'image/png', + 'image/webp', + 'image/gif', + 'image/avif', +]); + // The full-resolution Google scan (zoom=0) is the cover worth having, but it is // not always there: some volumes answer it with an error, some rate-limit the // image endpoint, and a large scan blows the 550 KB cap. When it fails, the @@ -112,9 +122,12 @@ async function pull( return null; } - const contentType = upstream.headers.get('content-type') ?? ''; + const contentType = (upstream.headers.get('content-type') ?? '') + .split(';')[0] + .trim() + .toLowerCase(); if ( - !contentType.startsWith('image/') || + !RASTER_TYPES.has(contentType) || Number(upstream.headers.get('content-length')) > COVER_MAX_BYTES ) { await upstream.body?.cancel(); @@ -167,6 +180,8 @@ export default async function handler( if (!image) continue; res.setHeader('Content-Type', image.contentType); + res.setHeader('X-Content-Type-Options', 'nosniff'); + res.setHeader('Content-Security-Policy', "default-src 'none'; sandbox"); res.setHeader('Cache-Control', 'public, max-age=86400'); console.info( JSON.stringify({ diff --git a/src/pages/user/[userId]/certificate.tsx b/src/pages/user/[userId]/certificate.tsx index 659c8ab4..277756bd 100644 --- a/src/pages/user/[userId]/certificate.tsx +++ b/src/pages/user/[userId]/certificate.tsx @@ -1,16 +1,11 @@ -import { useRouter } from 'next/router'; -import React, { useContext } from 'react'; - -import type { TRouter } from '@uxcore/local-types/global'; - import { getCertificate } from '@uxcore/api/uxcat/certificate'; - -import pageNotFoundData from '@uxcore/data/404'; - import { GlobalContext } from '@uxcore/components/Context/GlobalContext'; import Spinner from '@uxcore/components/Spinner'; - +import pageNotFoundData from '@uxcore/data/404'; import CertificateLayout from '@uxcore/layouts/CertificateLayout'; +import type { TRouter } from '@uxcore/local-types/global'; +import { useRouter } from 'next/router'; +import React, { useContext } from 'react'; import NotFoundPage from '../../404'; @@ -20,7 +15,7 @@ const Certificate = ({ userId, certificate }) => { const { uxcatUserInfo } = useContext(GlobalContext); const name = - uxcatUserInfo.user.id === 1034 + uxcatUserInfo?.user?.id === 1034 ? 'Кузнецов Тимофей Юрьевич' : `${certificate?.name} ${certificate?.surname}`; const date = new Date(certificate?.certificatedAt); @@ -53,6 +48,7 @@ export default Certificate; export async function getServerSideProps(context) { const userId = context.params.userId; const certificate = await getCertificate(userId); + if (!certificate) context.res.statusCode = 404; return { props: { certificate, diff --git a/src/uxcore/api/uxcat/certificate.ts b/src/uxcore/api/uxcat/certificate.ts index 60e0c4e0..a7f9ddd4 100644 --- a/src/uxcore/api/uxcat/certificate.ts +++ b/src/uxcore/api/uxcat/certificate.ts @@ -1,7 +1,7 @@ import { UxCatRoute } from '@uxcore/api/uxcat/configs'; export const getCertificate = async username => { - const url = `${UxCatRoute}users/${username}/certificate`; + const url = `${UxCatRoute}users/${encodeURIComponent(username)}/certificate`; try { const headers = { diff --git a/src/uxcore/components/ContentParser/ContentParser.tsx b/src/uxcore/components/ContentParser/ContentParser.tsx index 0263dcfa..dc0be2c4 100644 --- a/src/uxcore/components/ContentParser/ContentParser.tsx +++ b/src/uxcore/components/ContentParser/ContentParser.tsx @@ -1,10 +1,45 @@ +import useContentType from '@uxcore/hooks/useContentType'; import unescape from 'lodash.unescape'; import { FC, Fragment, memo, useMemo } from 'react'; import ReactMarkdown from 'react-markdown'; import rehypeRaw from 'rehype-raw'; +import rehypeSanitize, { defaultSchema } from 'rehype-sanitize'; import remarkBreaks from 'remark-breaks'; -import useContentType from '@uxcore/hooks/useContentType'; +// Persona text is saved by visitors and shown on public pages, so raw HTML is +// filtered to the tags our content uses. Custom tags are the ones +// useContentType renders. +// A backslash is refused because CSS escapes spell a blocked word past the list. +const SAFE_STYLE = + /^(?![\s\S]*(\\|position|url\s*\(|expression|@import))[\s\S]*$/i; +// The accordion file is appended to the CMS address, so it must be a path on it. +const CMS_PATH = /^\/(?![\/\\])/; +const sanitizeSchema = { + ...defaultSchema, + tagNames: [ + ...(defaultSchema.tagNames ?? []), + 'u', + 'span', + 'accordion', + 'download', + 'trello', + ], + attributes: { + ...defaultSchema.attributes, + '*': [ + ...(defaultSchema.attributes?.['*'] ?? []), + 'className', + ['style', SAFE_STYLE], + ], + accordion: ['title', ['file', CMS_PATH], 'open'], + download: ['url'], + trello: ['url'], + }, + protocols: { + ...defaultSchema.protocols, + url: ['http', 'https'], + }, +}; type ContentParserProps = { data: any; @@ -31,7 +66,7 @@ const ContentParser: FC = ({ className={styles.content} components={componentList} remarkPlugins={[[remarkBreaks]]} - rehypePlugins={[rehypeRaw]} + rehypePlugins={[rehypeRaw, [rehypeSanitize, sanitizeSchema]]} > {modifiedData} diff --git a/src/uxcore/hooks/useContentType.tsx b/src/uxcore/hooks/useContentType.tsx index 2f01a3e1..dcf7bad1 100644 --- a/src/uxcore/hooks/useContentType.tsx +++ b/src/uxcore/hooks/useContentType.tsx @@ -1,7 +1,3 @@ -import cn from 'classnames'; -import { useCallback, useMemo, useState } from 'react'; -import ReactDomServer from 'react-dom/server'; - import Accordion from '@uxcore/components/Accordion'; import { Div, @@ -12,6 +8,9 @@ import { Span, } from '@uxcore/components/ContentGenerator/elements'; import ContentParser from '@uxcore/components/ContentParser'; +import cn from 'classnames'; +import { useCallback, useMemo, useState } from 'react'; +import ReactDomServer from 'react-dom/server'; import useGlobals from './useGlobals'; @@ -93,6 +92,8 @@ const useContentType = (styles: any, usePTag: boolean) => { li: (props: any) =>
  • {props.children}
  • , ul: (props: any) => , img: (props: any) => { + // The sanitiser drops a source it does not trust; nothing to draw then. + if (typeof props.src !== 'string' || !props.src) return null; return (