Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/pages/api/library/autofill/cover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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({
Expand Down
16 changes: 6 additions & 10 deletions src/pages/user/[userId]/certificate.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/uxcore/api/uxcat/certificate.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
39 changes: 37 additions & 2 deletions src/uxcore/components/ContentParser/ContentParser.tsx
Original file line number Diff line number Diff line change
@@ -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'],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XSS gap: download / trello url is not validated. protocols in rehype-sanitize only applies to href, src, cite and longDesc. A url key does nothing here, so url on <download> and <trello> passes through unchecked. useContentType.tsx renders it as <a href={url}> (lines 117 and 133), so <trello url="javascript:alert(1)">x</trello> in persona text becomes a clickable javascript: link. Inside accordions the string is rebuilt and sanitised again, with the same result.

Add a pattern for the two tags, like the one used for file:

const SAFE_URL = /^(https?:\/\/|\/(?![\/\\]))/i;
// ...
download: [['url', SAFE_URL]],
trello: [['url', SAFE_URL]],

You can then drop the url entry from protocols, which is a no-op.

},
};

type ContentParserProps = {
data: any;
Expand All @@ -31,7 +66,7 @@ const ContentParser: FC<ContentParserProps> = ({
className={styles.content}
components={componentList}
remarkPlugins={[[remarkBreaks]]}
rehypePlugins={[rehypeRaw]}
rehypePlugins={[rehypeRaw, [rehypeSanitize, sanitizeSchema]]}
>
{modifiedData}
</ReactMarkdown>
Expand Down
9 changes: 5 additions & 4 deletions src/uxcore/hooks/useContentType.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -12,6 +8,9 @@ import {
Span,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import reorder in this file, in ContentParser.tsx and in certificate.tsx goes against the AGENTS.md import order: third-party first, then aliases grouped, then relative imports, then styles. It looks like an auto-sort with the wrong config. Run eslint --fix with the repo config to restore the order. It also makes the diff harder to review.

} 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';

Expand Down Expand Up @@ -93,6 +92,8 @@ const useContentType = (styles: any, usePTag: boolean) => {
li: (props: any) => <li className={styles.li}>{props.children}</li>,
ul: (props: any) => <ul className={styles.ul}>{props.children}</ul>,
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 (
<Image
styles={styles}
Expand Down
Loading