-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.tsx
More file actions
250 lines (235 loc) · 8.87 KB
/
Copy pathindex.tsx
File metadata and controls
250 lines (235 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import React, { useMemo } from "react";
import DocViewer, {
DocViewerRenderers,
type IConfig,
type ITheme,
type IDocument,
} from "@cyntler/react-doc-viewer";
import { cn } from "../../utils";
import MarkdownDocRenderer from "./markdown-viewer";
import SvgDocRenderer from "./svg-viewer";
import "@cyntler/react-doc-viewer/dist/index.css";
interface FileViewerProps {
/** URL of the file to display. Supports https:, http:, blob:, and data: URIs. */
url: string;
/** Optional file name override (used for download). Useful when the URL path lacks a meaningful filename. */
fileName?: string;
/** Override the DocViewer config. Merged shallowly over the defaults. */
config?: IConfig;
/** Class applied to the outer wrapper. */
className?: string;
/**
* Opt-in allowlist of `data:` URI MIME types that bypass the script-execution
* gate. By default the viewer rejects `data:` URLs whose MIME can execute
* code on top-frame navigation: `text/html`, `application/xhtml+xml`,
* `application/xml`, `text/xml`, `image/svg+xml`.
*
* Pass entries here only when the URL source is trusted. `image/svg+xml`
* is rendered through `<img>` (W3C secure static mode — scripts and
* external references are disabled by the browser) so opting it in stays
* safe; the other entries lose the defense-in-depth gate against the
* fallback "open in new tab" link.
*/
allowedDataMimes?: readonly string[];
}
const SAFE_URL_SCHEMES = new Set(["http:", "https:", "blob:", "data:"]);
// Data-URL MIMEs that execute script on top-frame navigation. Modern Firefox
// and Chrome already block most of these via their data:-navigation
// restrictions, but coverage varies by version and type (XHTML and XML have
// historically slipped through), so re-enforcing at the gate keeps the threat
// model trivially auditable. SVG is included because `<svg onload>` runs
// script when navigated to (it does not when loaded via `<img src>`). XML and
// XHTML can execute script via xml-stylesheet processing instructions or
// inline `<script>` once parsed as a document.
const UNSAFE_DATA_MIMES = new Set([
"text/html",
"application/xhtml+xml",
"application/xml",
"text/xml",
"image/svg+xml",
]);
/**
* Returns true if `raw` is a relative URL or uses an allowlisted scheme.
* Blocks `javascript:`, `vbscript:`, `file:`, `about:`, and anything else
* that could execute code or escape sandboxing when clicked.
*
* `allowedDataMimes` is a consumer-supplied override for the default
* `UNSAFE_DATA_MIMES` blocklist — entries here pass even if blocklisted.
*/
const isSafeUrl = (
raw: string,
allowedDataMimes: ReadonlySet<string>,
): boolean => {
if (typeof raw !== "string" || raw.length === 0) return false;
let parsed: URL;
try {
parsed = new URL(raw);
} catch {
// Relative URLs (e.g. "/foo", "./bar.pdf") throw — they resolve against
// the page origin and inherit its safety, so they're allowed.
return true;
}
const protocol = parsed.protocol.toLowerCase();
if (!SAFE_URL_SCHEMES.has(protocol)) return false;
if (protocol === "data:") {
const rawMime = parsed.pathname.split(/[,;]/)[0].trim().toLowerCase();
// Defense-in-depth: spec-compliant browsers do NOT percent-decode the
// data:-URL mediatype (WHATWG Fetch § data URL processor parses it raw;
// Chromium's DataURL::Parse only unescapes the body), so `text%2Fhtml`
// fails MIME parsing and falls back to `text/plain` — already safe. We
// decode anyway to harden against legacy or non-conforming runtimes;
// reject if decoding throws so malformed inputs can't slip through.
let mime: string;
try {
mime = decodeURIComponent(rawMime);
} catch {
return false;
}
if (allowedDataMimes.has(mime)) return true;
if (UNSAFE_DATA_MIMES.has(mime)) return false;
}
return true;
};
const defaultConfig: IConfig = {
header: {
disableHeader: true,
disableFileName: true,
},
pdfZoom: {
defaultZoom: 0.8,
zoomJump: 0.1,
},
pdfVerticalScrollByDefault: true,
};
const docTheme: ITheme = {
primary: "var(--klerosUIComponentsWhiteBackground)",
secondary: "var(--klerosUIComponentsLightBackground)",
tertiary: "var(--klerosUIComponentsLightBackground)",
textPrimary: "var(--klerosUIComponentsPrimaryText)",
textSecondary: "var(--klerosUIComponentsSecondaryText)",
textTertiary: "var(--klerosUIComponentsSecondaryText)",
};
const UnsupportedUrlMessage = ({ url }: { url: string }) => (
<div
className={cn(
"text-klerosUIComponentsSecondaryText text-sm",
"flex flex-col gap-2 p-6",
)}
>
<p>Unable to display this file.</p>
<p className="font-mono text-xs break-all">{url}</p>
</div>
);
const NoRendererFallback = ({
document,
fileName,
}: {
document: IDocument | undefined;
fileName: string;
}) => (
<div
className={cn(
"text-klerosUIComponentsPrimaryText text-sm",
"flex flex-col items-start gap-3 p-6",
)}
>
<p>This file type can't be previewed.</p>
<a
className="text-klerosUIComponentsPrimaryBlue underline"
href={document?.uri ?? ""}
download={fileName}
rel="noopener noreferrer"
target="_blank"
>
Open in a new tab
</a>
</div>
);
/**
* Displays a file from a URL inside the application. Supports PDFs, images,
* markdown, plaintext, and common document formats.
*
* Security: rejects `javascript:`, `vbscript:`, `file:`, and other unlisted
* schemes up front so a hostile `url` can't deliver code execution through
* the underlying viewer or its fallback download link.
*/
function FileViewer({
url,
fileName,
config,
className,
allowedDataMimes,
}: Readonly<FileViewerProps>) {
const allowedDataMimesSet = useMemo(
() => new Set((allowedDataMimes ?? []).map((m) => m.toLowerCase())),
[allowedDataMimes],
);
const safe = isSafeUrl(url, allowedDataMimesSet);
const docs = useMemo(() => [{ uri: url, fileName }], [url, fileName]);
const pluginRenderers = useMemo(
() => [...DocViewerRenderers, MarkdownDocRenderer, SvgDocRenderer],
[],
);
const mergedConfig = useMemo<IConfig>(
() => ({
...defaultConfig,
...config,
noRenderer: {
...config?.noRenderer,
overrideComponent:
config?.noRenderer?.overrideComponent ?? NoRendererFallback,
},
}),
[config],
);
return (
<div
className={cn(
"bg-klerosUIComponentsWhiteBackground shadow-default",
"rounded-base max-h-[80vh] overflow-auto",
className,
)}
>
{safe ? (
<DocViewer
documents={docs}
pluginRenderers={pluginRenderers}
config={mergedConfig}
theme={docTheme}
className={cn(
"!bg-klerosUIComponentsWhiteBackground",
"[&_#pdf-controls]:!z-[3]",
"[&_#pdf-controls]:!bg-klerosUIComponentsPrimaryPurple/15",
"dark:[&_#pdf-controls]:!bg-klerosUIComponentsLightBackground/50",
"[&_#pdf-controls]:!backdrop-saturate-150",
"[&_#pdf-controls_svg_path]:!fill-klerosUIComponentsPrimaryText",
"[&_#pdf-controls_svg_polygon]:!fill-klerosUIComponentsPrimaryText",
"[&_#image-renderer]:!bg-klerosUIComponentsWhiteBackground",
"[&_#image-renderer]:!h-auto",
"[&_#image-renderer]:!flex-none",
"[&_#image-renderer]:!px-6",
// Transparency checkerboard. The gradient is inlined in the
// arbitrary property (rather than a theme token) so the inner
// `var(--klerosUIComponentsImageCheckerColor)` resolves at this
// element's cascade — consumers can override that single variable
// anywhere up the tree and the color propagates. A theme-token
// wrapper would bake the inner var() at `:root` and the override
// would silently no-op for descendants. Full arbitrary-property
// syntax (not `bg-*` shortcut) so `tailwind-merge` recognizes this
// as background-image and doesn't collide with the bg-color above.
// eslint-disable-next-line max-len
"[&_#image-renderer]:![background-image:linear-gradient(45deg,var(--klerosUIComponentsImageCheckerColor)_25%,transparent_25%),linear-gradient(-45deg,var(--klerosUIComponentsImageCheckerColor)_25%,transparent_25%),linear-gradient(45deg,transparent_75%,var(--klerosUIComponentsImageCheckerColor)_75%),linear-gradient(-45deg,transparent_75%,var(--klerosUIComponentsImageCheckerColor)_75%)]",
"[&_#image-renderer]:![background-size:20px_20px]",
"[&_#image-renderer]:![background-position:0_0,0_10px,10px_-10px,-10px_0px]",
"[&_#image-img]:!max-w-full",
"[&_#image-img]:!max-h-[80vh]",
"[&_[class*='--loading']]:text-klerosUIComponentsSecondaryText",
)}
/>
) : (
<UnsupportedUrlMessage url={url} />
)}
</div>
);
}
export default FileViewer;