Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Support creating password pastes on web

Manav Rathi924d82fd1dcd
diff --git a/web/apps/paste/src/features/paste/components/PasteCreatePanel.tsx b/web/apps/paste/src/features/paste/components/PasteCreatePanel.tsx
index 853e9e7d06..d22f7064d0 100644
--- a/web/apps/paste/src/features/paste/components/PasteCreatePanel.tsx
+++ b/web/apps/paste/src/features/paste/components/PasteCreatePanel.tsx
@@ -1,15 +1,25 @@
import { Navigation06Icon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
+import LockOpenRoundedIcon from "@mui/icons-material/LockOpenRounded";
+import LockRoundedIcon from "@mui/icons-material/LockRounded";
import {
Box,
+ Button,
CircularProgress,
+ Dialog,
+ DialogActions,
+ DialogContent,
+ DialogTitle,
IconButton,
+ Stack,
TextField,
Typography,
} from "@mui/material";
import useMediaQuery from "@mui/material/useMediaQuery";
import { usePasteColorMode } from "features/paste/hooks/usePasteColorMode";
import { getPasteThemeTokens } from "features/paste/theme/pasteThemeTokens";
+import type { FormEvent } from "react";
+import { useState } from "react";
import { MAX_PASTE_CHARS } from "../constants";
import { PasteLinkCard } from "./PasteLinkCard";
import { pasteTextFieldSx } from "./textFieldSx";
@@ -20,7 +30,7 @@ interface PasteCreatePanelProps {
createError: string | null;
createdLink: string | null;
onInputChange: (value: string) => void;
- onCreate: () => Promise<void>;
+ onCreate: (password?: string) => Promise<void>;
onCopyLink: (value: string) => Promise<void>;
onShareLink: (url: string) => Promise<void>;
}
@@ -38,6 +48,11 @@ export const PasteCreatePanel = ({
const isMobile = useMediaQuery("(max-width:599.95px)", { noSsr: true });
const { resolvedMode } = usePasteColorMode();
const tokens = getPasteThemeTokens(resolvedMode);
+ const [passwordProtected, setPasswordProtected] = useState(false);
+ const [passwordDialogOpen, setPasswordDialogOpen] = useState(false);
+ const [password, setPassword] = useState("");
+ const [confirmPassword, setConfirmPassword] = useState("");
+ const [passwordError, setPasswordError] = useState<string | null>(null);
const isInputEmpty = inputText.trim().length === 0;
const nearLimitThreshold = Math.floor(MAX_PASTE_CHARS * 0.9);
const isNearCharLimit = inputText.length >= nearLimitThreshold;
@@ -49,6 +64,29 @@ export const PasteCreatePanel = ({
"Auto-deletes after 24 hours",
];
+ const resetPasswordDialog = () => {
+ setPassword("");
+ setConfirmPassword("");
+ setPasswordError(null);
+ };
+
+ const handlePasswordSubmit = (event: FormEvent<HTMLFormElement>) => {
+ event.preventDefault();
+ if (!password) {
+ setPasswordError("Enter a password");
+ return;
+ }
+ if (password !== confirmPassword) {
+ setPasswordError("Passwords do not match");
+ return;
+ }
+
+ const submittedPassword = password;
+ setPasswordDialogOpen(false);
+ resetPasswordDialog();
+ void onCreate(submittedPassword);
+ };
+
return (
<Box sx={{ width: "100%", maxWidth: "100%", minWidth: 0 }}>
<Box
@@ -126,41 +164,83 @@ export const PasteCreatePanel = ({
pointerEvents: "none",
}}
>
- <Typography
- variant="mini"
+ <Box
sx={{
display: "flex",
alignItems: "center",
+ gap: 0.7,
height: { xs: 32, sm: 36 },
- color: tokens.text.counter,
- fontWeight: 600,
- lineHeight: 1,
- letterSpacing: "0.01em",
+ pointerEvents: "auto",
}}
>
- <Box
- component="span"
+ <Typography
+ variant="mini"
sx={{
- color: isNearCharLimit
- ? tokens.text.counterHighlight
- : tokens.text.counter,
+ display: "flex",
+ alignItems: "center",
+ color: tokens.text.counter,
+ fontWeight: 600,
+ lineHeight: 1,
+ letterSpacing: "0.01em",
}}
>
- {inputText.length}
- </Box>
- <Box
- component="span"
- sx={{ color: tokens.text.counter }}
+ <Box
+ component="span"
+ sx={{
+ color: isNearCharLimit
+ ? tokens.text.counterHighlight
+ : tokens.text.counter,
+ }}
+ >
+ {inputText.length}
+ </Box>
+ <Box
+ component="span"
+ sx={{ color: tokens.text.counter }}
+ >
+ /{MAX_PASTE_CHARS}
+ </Box>
+ </Typography>
+ <IconButton
+ aria-label={
+ passwordProtected
+ ? "Disable password protection"
+ : "Enable password protection"
+ }
+ aria-pressed={passwordProtected}
+ onClick={() => {
+ if (creating) return;
+ setPasswordProtected((enabled) => !enabled);
+ }}
+ sx={{
+ width: { xs: 30, sm: 32 },
+ height: { xs: 30, sm: 32 },
+ color: passwordProtected
+ ? tokens.button.primaryBg
+ : tokens.text.counter,
+ bgcolor: passwordProtected
+ ? tokens.surface.chipBg
+ : "transparent",
+ "&:hover": { bgcolor: tokens.surface.chipBg },
+ }}
>
- /{MAX_PASTE_CHARS}
- </Box>
- </Typography>
+ {passwordProtected ? (
+ <LockRoundedIcon sx={{ fontSize: 17 }} />
+ ) : (
+ <LockOpenRoundedIcon sx={{ fontSize: 17 }} />
+ )}
+ </IconButton>
+ </Box>
<IconButton
aria-label="Create secure link"
aria-busy={creating}
onClick={() => {
if (creating || isCreateDisabled) return;
- void onCreate();
+ if (passwordProtected) {
+ setPasswordDialogOpen(true);
+ } else {
+ void onCreate();
+ }
}}
disabled={isCreateDisabled}
sx={{
@@ -295,6 +375,65 @@ export const PasteCreatePanel = ({
/>
</Box>
)}
+
+ <Dialog
+ open={passwordDialogOpen}
+ onClose={() => {
+ setPasswordDialogOpen(false);
+ resetPasswordDialog();
+ }}
+ fullWidth
+ maxWidth="xs"
+ slotProps={{
+ paper: {
+ component: "form",
+ onSubmit: handlePasswordSubmit,
+ sx: { borderRadius: "14px" },
+ },
+ }}
+ >
+ <DialogTitle sx={{ pb: 1 }}>Paste password</DialogTitle>
+ <DialogContent>
+ <Stack spacing={1.5} sx={{ pt: 1 }}>
+ <TextField
+ autoFocus
+ type="password"
+ label="Password"
+ value={password}
+ autoComplete="off"
+ onChange={(event) => {
+ setPassword(event.target.value);
+ setPasswordError(null);
+ }}
+ />
+ <TextField
+ type="password"
+ label="Confirm password"
+ value={confirmPassword}
+ autoComplete="off"
+ error={!!passwordError}
+ helperText={passwordError ?? " "}
+ onChange={(event) => {
+ setConfirmPassword(event.target.value);
+ setPasswordError(null);
+ }}
+ />
+ </Stack>
+ </DialogContent>
+ <DialogActions sx={{ px: 3, pb: 2.5 }}>
+ <Button
+ onClick={() => {
+ setPasswordDialogOpen(false);
+ resetPasswordDialog();
+ }}
+ >
+ Cancel
+ </Button>
+ <Button type="submit" variant="contained" disableElevation>
+ Create
+ </Button>
+ </DialogActions>
+ </Dialog>
</Box>
);
};
diff --git a/web/apps/paste/src/features/paste/hooks/useCreatePaste.ts b/web/apps/paste/src/features/paste/hooks/useCreatePaste.ts
index 62ea221730..8a1dda290a 100644
--- a/web/apps/paste/src/features/paste/hooks/useCreatePaste.ts
+++ b/web/apps/paste/src/features/paste/hooks/useCreatePaste.ts
@@ -12,7 +12,7 @@ export const useCreatePaste = () => {
const [createError, setCreateError] = useState<string | null>(null);
const [createdLink, setCreatedLink] = useState<string | null>(null);
- const createSecureLink = async () => {
+ const createSecureLink = async (password?: string) => {
setCreateError(null);
if (!inputText.trim()) {
@@ -27,12 +27,12 @@ export const useCreatePaste = () => {
setCreating(true);
try {
- const { fragmentSecret, payload } =
- await encryptPasteForCreate(inputText);
- const response = await createPaste(payload);
- setCreatedLink(
- buildPasteLink(response.accessToken, fragmentSecret),
+ const { linkFragment, payload } = await encryptPasteForCreate(
+ inputText,
+ password,
);
+ const response = await createPaste(payload);
+ setCreatedLink(buildPasteLink(response.accessToken, linkFragment));
} catch (error) {
const message =
error instanceof Error
diff --git a/web/apps/paste/src/features/paste/utils/pasteCrypto.ts b/web/apps/paste/src/features/paste/utils/pasteCrypto.ts
index cb5213f254..fe037577e3 100644
--- a/web/apps/paste/src/features/paste/utils/pasteCrypto.ts
+++ b/web/apps/paste/src/features/paste/utils/pasteCrypto.ts
@@ -3,6 +3,7 @@ import {
decryptMetadataJSON,
deriveInteractiveKey,
deriveKey,
+ deriveModerateKey,
encryptBox,
encryptMetadataJSON,
generateKey,
@@ -24,16 +25,27 @@ export interface PasteKey {
export const createFragmentSecret = () =>
newID("").slice(0, FRAGMENT_SECRET_LENGTH);
-export const encryptPasteForCreate = async (text: string) => {
+const pasteKeyLinkFragment = (pasteKey: PasteKey) =>
+ pasteKey.passwordRequired
+ ? `${PASSWORD_FRAGMENT_PREFIX}${pasteKey.fragmentSecret}`
+ : pasteKey.fragmentSecret;
+
+export const encryptPasteForCreate = async (
+ text: string,
+ password?: string,
+) => {
const key = await generateKey();
const fragmentSecret = createFragmentSecret();
+ const pasteKey = { fragmentSecret, passwordRequired: !!password };
const encrypted = await encryptMetadataJSON({ text }, key);
- const keyEncryptionKey = await deriveInteractiveKey(fragmentSecret);
+ const keyEncryptionKey = password
+ ? await deriveModerateKey(pasteKeyKdfSecret(pasteKey, password))
+ : await deriveInteractiveKey(fragmentSecret);
const encryptedPasteKey = await encryptBox(key, keyEncryptionKey.key);
return {
- fragmentSecret,
+ linkFragment: pasteKeyLinkFragment(pasteKey),
payload: {
encryptedData: encrypted.encryptedData,
decryptionHeader: encrypted.decryptionHeader,
diff --git a/web/packages/base/crypto/index.ts b/web/packages/base/crypto/index.ts
index cf837db59c..f66272a864 100644
--- a/web/packages/base/crypto/index.ts
+++ b/web/packages/base/crypto/index.ts
@@ -513,6 +513,15 @@ export const deriveInteractiveKey = (
? libsodium.deriveInteractiveKey(passphrase)
: sharedWorker().then((w) => w.deriveInteractiveKey(passphrase));
+/**
+ * Derive a key suitable for moderate-cost password gates from the given
+ * {@link passphrase}.
+ */
+export const deriveModerateKey = (passphrase: string): Promise<DerivedKey> =>
+ inWorker()
+ ? libsodium.deriveModerateKey(passphrase)
+ : sharedWorker().then((w) => w.deriveModerateKey(passphrase));
+
/**
* Derive a subkey of the given {@link key} using the specified parameters.
*
diff --git a/web/packages/base/crypto/libsodium.ts b/web/packages/base/crypto/libsodium.ts
index bb4c17091f..e6cd7218cb 100644
--- a/web/packages/base/crypto/libsodium.ts
+++ b/web/packages/base/crypto/libsodium.ts
@@ -858,6 +858,21 @@ export const deriveInteractiveKey = async (
return { key, salt, opsLimit, memLimit };
};
+/**
+ * A variant of {@link deriveSensitiveKey} for deriving an alternative key with
+ * parameters suitable for moderate-cost password gates.
+ */
+export const deriveModerateKey = async (
+ passphrase: string,
+): Promise<DerivedKey> => {
+ const salt = await generateDeriveKeySalt();
+ const opsLimit = sodium.crypto_pwhash_OPSLIMIT_MODERATE;
+ const memLimit = sodium.crypto_pwhash_MEMLIMIT_MODERATE;
+
+ const key = await deriveKey(passphrase, salt, opsLimit, memLimit);
+ return { key, salt, opsLimit, memLimit };
+};
+
/**
* Derive a {@link subKeyID}-th subkey of length {@link subKeyLength} bytes by
* applying a KDF (Key Derivation Function) for the given {@link key} and the
diff --git a/web/packages/base/crypto/worker.ts b/web/packages/base/crypto/worker.ts
index bc25a7c0ad..29e4e30991 100644
--- a/web/packages/base/crypto/worker.ts
+++ b/web/packages/base/crypto/worker.ts
@@ -45,6 +45,7 @@ export class CryptoWorker {
deriveKey = libsodium.deriveKey;
deriveSensitiveKey = libsodium.deriveSensitiveKey;
deriveInteractiveKey = libsodium.deriveInteractiveKey;
+ deriveModerateKey = libsodium.deriveModerateKey;
deriveSubKeyBytes = libsodium.deriveSubKeyBytes;
}