react-smart-copy 1.19.1 → 1.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +165 -0
- package/dist/files.cjs +948 -0
- package/dist/files.cjs.map +1 -0
- package/dist/files.d.cts +311 -0
- package/dist/files.d.ts +311 -0
- package/dist/files.js +932 -0
- package/dist/files.js.map +1 -0
- package/dist/secrets.cjs +551 -0
- package/dist/secrets.cjs.map +1 -0
- package/dist/secrets.d.cts +152 -0
- package/dist/secrets.d.ts +152 -0
- package/dist/secrets.js +544 -0
- package/dist/secrets.js.map +1 -0
- package/package.json +67 -4
package/README.md
CHANGED
|
@@ -32,6 +32,8 @@ ID PLT-29018 [Copy]
|
|
|
32
32
|
- **`react-smart-copy/markdown`**: paste from Google Docs, Word, Notion or any web page into a Markdown field and get clean Markdown, with undo intact: `<textarea onPaste={pasteAsMarkdown} />`
|
|
33
33
|
- **`react-smart-copy/code`**: copy code blocks the way developers expect (a terminal session copies as its commands, gutters and prompts stay behind, colours survive in Docs and Slack), paste code cleaned and indented, and catch Trojan Source, invisible identifiers and lookalike letters
|
|
34
34
|
- **`react-smart-copy/image`**: get pasted photos and screenshots ready to upload. It removes GPS location, camera data and hidden thumbnails without touching a pixel, and fits size and byte limits, re-encoding only when it has to
|
|
35
|
+
- **`react-smart-copy/files`**: take pasted and dropped files and whole folders safely. Types come from each file's content, so a program named `invoice.pdf.exe` or a web page named `report.pdf` is turned away with a reason, names are made safe for every operating system, and limits cap count, size and depth
|
|
36
|
+
- **`react-smart-copy/secrets`**: keep API keys, tokens, private keys, passwords, card numbers and IBANs out of pastes and copies. Paste guards for chat boxes and forms, and redaction of what people copy from logs and consoles. Every pattern runs in linear time, and no finding holds the secret
|
|
35
37
|
- **`react-smart-copy/link`**: "Copy link" buttons that paste as a titled link in Slack, Docs and Notion and as the URL in editors, links that scroll to a highlighted passage, tracking parameters removed, and pasting a URL over selected text to make a link
|
|
36
38
|
- **`react-smart-copy/rich`**: copy styled content so it pastes as it looks in Gmail, Outlook, Apple Mail, Google Docs, Word, Notion and Slack: sanitized HTML with inline styles, email-safe colours and absolute links, plus readable plain text. Works for buttons and for Ctrl/⌘+C. And the other way: paste from Docs, Word and mail into a rich-text editor (TipTap, Lexical, `contenteditable`) as clean, semantic HTML
|
|
37
39
|
- **`react-smart-copy/color`**: copy colours in any CSS format (hex to `oklch()` and Display P3) with a swatch for Docs and Slack, paste any format into a colour field, and check WCAG contrast. Wide-gamut colours are mapped the way CSS specifies, never clipped to a wrong hue
|
|
@@ -856,6 +858,167 @@ const { blob, removed } = await stripMetadata(file); // bit-for-bit the same pic
|
|
|
856
858
|
their orientation, and Chromium doesn't turn WebP. `removed` is read back from the
|
|
857
859
|
output, not assumed, because some browsers' encoders add their own colour profile.
|
|
858
860
|
|
|
861
|
+
## Pasted and dropped files: safe by content
|
|
862
|
+
|
|
863
|
+
`react-smart-copy/files` takes the files people paste or drop, whole folders included, and
|
|
864
|
+
decides for each one whether it is safe and wanted. The type comes from the file's content,
|
|
865
|
+
never from its name or the type the browser claims. So an HTML page named `invoice.pdf`, a
|
|
866
|
+
program named `invoice.pdf.exe`, or a name that hides its real extension behind a
|
|
867
|
+
right-to-left character is turned away, with a reason you can show. No dependencies, about
|
|
868
|
+
6.9 kB (about 2.3 kB for `safeFileName` alone). It works the same on a server.
|
|
869
|
+
|
|
870
|
+
```tsx
|
|
871
|
+
import { acceptFiles } from "react-smart-copy/files";
|
|
872
|
+
|
|
873
|
+
<div
|
|
874
|
+
onDragOver={(event) => event.preventDefault()}
|
|
875
|
+
onDrop={(event) => {
|
|
876
|
+
event.preventDefault();
|
|
877
|
+
// Call it in the handler, before any await: the browser empties the drop when the event ends.
|
|
878
|
+
void acceptFiles(event, { accept: ".pdf,image/*", maxFileBytes: 20_000_000, digest: true }).then(({ accepted, rejected }) => {
|
|
879
|
+
for (const file of accepted) upload(file.upload, file.path); // safe name, real type
|
|
880
|
+
for (const file of rejected) toast(file.message); // "invoice.pdf.exe is a program or script, which can't be added."
|
|
881
|
+
});
|
|
882
|
+
}}
|
|
883
|
+
/>
|
|
884
|
+
```
|
|
885
|
+
|
|
886
|
+
It takes a drop or paste event (DOM or React), its `DataTransfer`, the `FileList` of an
|
|
887
|
+
`<input type="file">`, or any list of files. With `usePaste` and `PasteField`, pass the
|
|
888
|
+
result's `files`.
|
|
889
|
+
|
|
890
|
+
**Known by content.** The first 8 KB of each file is read, and more than 50 formats are
|
|
891
|
+
recognised by their signatures: PDF; images (JPEG, PNG, GIF, WebP, AVIF, HEIC, TIFF, BMP,
|
|
892
|
+
ICO, PSD, SVG); audio and video; Office and OpenDocument files, told apart inside their
|
|
893
|
+
ZIP; archives; fonts; SQLite; HTML, XML, JSON and other text; and programs (Windows, Linux
|
|
894
|
+
and macOS executables, WebAssembly, Java and Android archives, shortcuts and scripts). Each
|
|
895
|
+
accepted file's `type` is the detected one, never the claimed one: `upload` is the same
|
|
896
|
+
bytes as a `File` with that type and the safe name.
|
|
897
|
+
|
|
898
|
+
**Risky files are rejected by default.** Each file is checked for these risks, and by
|
|
899
|
+
default any of them rejects it. Pass `block` to choose which ones do.
|
|
900
|
+
|
|
901
|
+
| Risk | What it catches |
|
|
902
|
+
| --- | --- |
|
|
903
|
+
| `executable` | Programs and scripts, by content or by a name the system runs (`.exe`, `.bat`, `.ps1`, `.js`, `.jar`, `.iso`, `.lnk`, ...) |
|
|
904
|
+
| `active-content` | HTML and SVG, and XML with XHTML or scripts: they run script when opened from your site |
|
|
905
|
+
| `macros` | Office files that can hold macros (`.docm`, `.xlsm`, or a `vbaProject.bin` inside) |
|
|
906
|
+
| `type-mismatch` | The content is not what the extension says: `report.pdf` holding HTML |
|
|
907
|
+
| `double-extension` | `invoice.pdf.exe`, or `invoice.pdf .exe` |
|
|
908
|
+
| `hidden-characters` | Bidirectional controls and invisible characters in the name: `photo\u202Egpj.exe` shows as `photoexe.jpg` |
|
|
909
|
+
| `empty` | No bytes: often a folder dropped where folders aren't read |
|
|
910
|
+
|
|
911
|
+
**Folders, within limits.** Dropped folders are walked depth-first, and each file's
|
|
912
|
+
`path` keeps where it was (`photos/2024/a.jpg`). `maxFiles` (100), `maxFileBytes`
|
|
913
|
+
(100 MiB), `maxTotalBytes` (1 GiB) and `maxDepth` (8) bound the walk, so a dropped home
|
|
914
|
+
folder can't hang the page. With `digest: true`, each file's SHA-256 is computed and a
|
|
915
|
+
repeated file is rejected as a `duplicate`.
|
|
916
|
+
|
|
917
|
+
**Names safe everywhere.** `safeFileName` keeps only the last path segment (no `../`),
|
|
918
|
+
removes hidden and control characters, replaces the characters Windows, macOS and Linux
|
|
919
|
+
reject, prefixes Windows device names (`CON`, `NUL`, `COM1`), and fits the name to 255
|
|
920
|
+
UTF-8 bytes, keeping its extension.
|
|
921
|
+
|
|
922
|
+
```ts
|
|
923
|
+
import { inspectFile, safeFileName } from "react-smart-copy/files";
|
|
924
|
+
|
|
925
|
+
// In the browser or on a server (Node 18+), for a file you already have.
|
|
926
|
+
const inspection = await inspectFile(file);
|
|
927
|
+
// { name: "invoice.pdf", safeName: "invoice.pdf", extension: "pdf", type: "text/html",
|
|
928
|
+
// detected: { mime: "text/html", family: "text", ... }, mismatch: true,
|
|
929
|
+
// risks: ["active-content", "type-mismatch"], ... }
|
|
930
|
+
|
|
931
|
+
safeFileName("../../uploads/CON.txt"); // "_CON.txt"
|
|
932
|
+
```
|
|
933
|
+
|
|
934
|
+
| Option (`acceptFiles`) | Default | Notes |
|
|
935
|
+
| --- | --- | --- |
|
|
936
|
+
| `accept` | any | as in `<input accept>`: `".pdf,image/*"`, judged by content |
|
|
937
|
+
| `block` | every risk | `[]` accepts risky files; their `inspection.risks` still say |
|
|
938
|
+
| `maxFiles` | 100 | the walk stops at the next file, and `truncated` is `true` |
|
|
939
|
+
| `maxFileBytes` / `maxTotalBytes` | 100 MiB / 1 GiB | |
|
|
940
|
+
| `folders` / `maxDepth` | `true` / 8 | |
|
|
941
|
+
| `digest` | `false` | SHA-256 for each file, and `duplicate` rejections; needs Web Crypto (a secure page, or Node.js 20+) |
|
|
942
|
+
| `names` | | `maxBytes`, `replacement` and `fallback` for `safeFileName` |
|
|
943
|
+
| `signal` | none | aborts with an `aborted` CopyFailure |
|
|
944
|
+
|
|
945
|
+
Each rejection has a `reason` (`blocked`, `not-accepted`, `too-large`, `total-too-large`,
|
|
946
|
+
`too-many`, `too-deep`, `unreadable` or `duplicate`), the file's `risks`, and a `message`
|
|
947
|
+
that names the file by its safe name. Checking files in the browser is for the people
|
|
948
|
+
using your app: check them again on your server, where `inspectFile` works the same, and
|
|
949
|
+
serve uploads with `Content-Disposition: attachment` and `X-Content-Type-Options: nosniff`.
|
|
950
|
+
|
|
951
|
+
## Secrets: keep keys out of pastes and copies
|
|
952
|
+
|
|
953
|
+
`react-smart-copy/secrets` keeps secrets from travelling through the clipboard. It finds API
|
|
954
|
+
keys, tokens, private keys, passwords, card numbers and IBANs in text people paste or copy,
|
|
955
|
+
and redacts or blocks them: in a chat box, a support ticket, or a log someone copies from
|
|
956
|
+
your app. No dependencies, about 4.1 kB (about 3.3 kB for `scanSecrets` alone), and it works
|
|
957
|
+
the same on a server.
|
|
958
|
+
|
|
959
|
+
```tsx
|
|
960
|
+
import { pasteWithoutSecrets } from "react-smart-copy/secrets";
|
|
961
|
+
|
|
962
|
+
<textarea
|
|
963
|
+
onPaste={(event) =>
|
|
964
|
+
pasteWithoutSecrets(event, {
|
|
965
|
+
// "redact" (the default) inserts the paste with each secret replaced; "block" inserts nothing.
|
|
966
|
+
action: "redact",
|
|
967
|
+
onSecrets: (found) => toast(`Removed a ${found[0].label} from your message.`),
|
|
968
|
+
})
|
|
969
|
+
}
|
|
970
|
+
/>
|
|
971
|
+
```
|
|
972
|
+
|
|
973
|
+
**What it finds.** Each finding has a kind, a confidence, and a masked preview.
|
|
974
|
+
|
|
975
|
+
- **Service keys and tokens, by their documented formats:** AWS access key IDs, GitHub
|
|
976
|
+
(classic and fine-grained), GitLab, Slack tokens and webhooks, Stripe live keys, Google API
|
|
977
|
+
keys, npm, PyPI, SendGrid, Twilio, Azure storage keys, and JSON Web Tokens (whose
|
|
978
|
+
header must decode).
|
|
979
|
+
- **Private keys** (PEM and OpenSSH), from their `BEGIN` line to their `END` line.
|
|
980
|
+
- **Passwords inside other text:** in URLs (`postgres://app:password@db`) and in connection
|
|
981
|
+
strings (`;Password=...`).
|
|
982
|
+
- **Card numbers and IBANs, by checksum:** Luhn plus the card brand's ranges and lengths,
|
|
983
|
+
and mod-97 plus the country's IBAN length.
|
|
984
|
+
- **Random-looking values assigned to secret names:** `API_KEY="..."`, `"password": "..."`,
|
|
985
|
+
`client_secret: ...`, including an AWS secret key next to its name. Placeholders
|
|
986
|
+
(`changeme`, `<token>`, `${TOKEN}`) and references (`process.env.TOKEN`) are left alone.
|
|
987
|
+
|
|
988
|
+
**Safe to run on anything.** Every pattern runs in linear time: no nested quantifiers, no
|
|
989
|
+
lookbehind. Hostile text of hundreds of kilobytes is scanned in milliseconds. A finding never
|
|
990
|
+
holds the secret: it holds where the secret is (`start`, `end`), its `kind` and `label`, a
|
|
991
|
+
`confidence`, and `masked`. `masked` keeps at most a known prefix and the last 4 characters
|
|
992
|
+
(`ghp_` and the last 4, or `•••• 4242` for a card), and nothing at all for passwords. Text
|
|
993
|
+
past `maxLength` (1,000,000 characters) is not scanned, and `redactSecrets` drops it rather
|
|
994
|
+
than pass it on unchecked.
|
|
995
|
+
|
|
996
|
+
```ts
|
|
997
|
+
import { redactSecrets, scanSecrets, secretRedactor } from "react-smart-copy/secrets";
|
|
998
|
+
|
|
999
|
+
const { findings } = scanSecrets(text, { minConfidence: "high" });
|
|
1000
|
+
// [{ kind: "github-token", label: "GitHub token", confidence: "high", start: 13, end: 53, masked: "ghp_…a1B2" }]
|
|
1001
|
+
|
|
1002
|
+
redactSecrets("token=ghp_…").text; // "token=[redacted GitHub token]"
|
|
1003
|
+
|
|
1004
|
+
// Mask secrets in what people copy from a log viewer or an admin console.
|
|
1005
|
+
useCopyInterceptor(logRef, { transform: secretRedactor() });
|
|
1006
|
+
```
|
|
1007
|
+
|
|
1008
|
+
| Option | Default | Notes |
|
|
1009
|
+
| --- | --- | --- |
|
|
1010
|
+
| `kinds` | every kind | the kinds to look for |
|
|
1011
|
+
| `minConfidence` | `"medium"` | `"high"` leaves out values that only look like secrets |
|
|
1012
|
+
| `maxLength` | 1,000,000 | characters scanned |
|
|
1013
|
+
| `replace` | `[redacted <label>]` | `redactSecrets`, `pasteWithoutSecrets` and `secretRedactor` |
|
|
1014
|
+
| `action` | `"redact"` | `pasteWithoutSecrets`: `"block"` inserts nothing |
|
|
1015
|
+
| `onSecrets` | | `pasteWithoutSecrets`: called with the findings and what was done |
|
|
1016
|
+
|
|
1017
|
+
A paste is redacted in a `<textarea>` or text `<input>`, with undo intact; anywhere else, a
|
|
1018
|
+
paste with secrets is blocked. Scanning in the browser protects the people using your app.
|
|
1019
|
+
Scan again on your server before you store or forward text, because `scanSecrets` works the
|
|
1020
|
+
same there.
|
|
1021
|
+
|
|
859
1022
|
## Copy links
|
|
860
1023
|
|
|
861
1024
|
`react-smart-copy/link` is for "Copy link" and "Share" buttons, and for fields people
|
|
@@ -1673,6 +1836,8 @@ embedded webviews. Where a capability is missing you get a typed error, never a
|
|
|
1673
1836
|
- ~~**1.17** `react-smart-copy/rich` — styled content that pastes as it looks in mail, Docs, Word and Slack, for buttons and Ctrl/⌘+C~~ shipped
|
|
1674
1837
|
- ~~**1.18** One shared copy of the core across entry points: smaller bundles when you use several modules, and one `CopyFailure` class~~ shipped
|
|
1675
1838
|
- ~~**1.19** `react-smart-copy/rich`, paste side: paste from Word, Google Docs and web pages into rich-text editors as clean, safe HTML~~ shipped
|
|
1839
|
+
- ~~**1.20** `react-smart-copy/files` — take pasted and dropped files and folders safely: types from content, risky files rejected, safe names, limits~~ shipped
|
|
1840
|
+
- ~~**1.21** `react-smart-copy/secrets` — find and redact API keys, tokens, private keys, passwords, cards and IBANs in pastes and copies~~ shipped
|
|
1676
1841
|
|
|
1677
1842
|
Full history: [CHANGELOG.md](./CHANGELOG.md) or the [Releases page](https://suprabhat02.github.io/react-smart-copy/project/releases/).
|
|
1678
1843
|
|