Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: ui tweeks for saving secrets (#4558)

Myles Scolnickcb11551ba733
diff --git a/frontend/src/components/editor/chrome/panels/__tests__/write-secret-modal.test.ts b/frontend/src/components/editor/chrome/panels/__tests__/write-secret-modal.test.ts
new file mode 100644
index 0000000000..2022f9968e
--- /dev/null
+++ b/frontend/src/components/editor/chrome/panels/__tests__/write-secret-modal.test.ts
@@ -0,0 +1,43 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { describe, it, expect } from "vitest";
+import { replaceInvalid } from "../write-secret-modal";
+
+describe("replaceInvalid", () => {
+ it("should replace spaces with underscores", () => {
+ expect(replaceInvalid("secret name")).toBe("secret_name");
+ });
+
+ it("should replace hyphens with underscores", () => {
+ expect(replaceInvalid("secret-name")).toBe("secret_name");
+ });
+
+ it("should replace multiple invalid characters with underscores", () => {
+ expect(replaceInvalid("secret name-with-hyphens")).toBe(
+ "secret_name_with_hyphens",
+ );
+ });
+
+ it("should handle leading and trailing invalid characters", () => {
+ expect(replaceInvalid("-secret name-")).toBe("_secret_name_");
+ });
+
+ it("should handle consecutive invalid characters", () => {
+ expect(replaceInvalid("secret--name with spaces")).toBe(
+ "secret__name__with_spaces",
+ );
+ });
+
+ it("should not replace valid characters (alphanumeric and underscore)", () => {
+ expect(replaceInvalid("valid_secret_name_123")).toBe(
+ "valid_secret_name_123",
+ );
+ });
+
+ it("should return an empty string if the input is empty", () => {
+ expect(replaceInvalid("")).toBe("");
+ });
+
+ it("should handle strings with only invalid characters", () => {
+ expect(replaceInvalid(" - ")).toBe("___");
+ });
+});
diff --git a/frontend/src/components/editor/chrome/panels/write-secret-modal.tsx b/frontend/src/components/editor/chrome/panels/write-secret-modal.tsx
index 39deddb684..eef0d5f908 100644
--- a/frontend/src/components/editor/chrome/panels/write-secret-modal.tsx
+++ b/frontend/src/components/editor/chrome/panels/write-secret-modal.tsx
@@ -109,8 +109,8 @@ export const WriteSecretModal: React.FC<{
id="key"
value={key}
onChange={(e) => {
- // Remove any whitespace from the input
- setKey(e.target.value.replaceAll(/\s+/g, "_"));
+ // Remove any non-word characters from the input
+ setKey(replaceInvalid(e.target.value));
}}
placeholder="MY_SECRET_KEY"
required={true}
@@ -126,6 +126,12 @@ export const WriteSecretModal: React.FC<{
required={true}
autoComplete="off"
/>
+ {/* http is prone to man-in-the-middle */}
+ {isHttpUrl() && (
+ <FormDescription>
+ Note: You are sending this key over http.
+ </FormDescription>
+ )}
</div>
<div className="grid gap-2">
<Label htmlFor="location">Location</Label>
@@ -172,3 +178,12 @@ export const WriteSecretModal: React.FC<{
</DialogContent>
);
};
+
+export function replaceInvalid(input: string): string {
+ return input.replaceAll(/\W/g, "_");
+}
+
+function isHttpUrl(): boolean {
+ const url = window.location.href;
+ return url.startsWith("http://");
+}
diff --git a/frontend/src/components/editor/chrome/types.ts b/frontend/src/components/editor/chrome/types.ts
index bb7a5bfbf3..b3d854292e 100644
--- a/frontend/src/components/editor/chrome/types.ts
+++ b/frontend/src/components/editor/chrome/types.ts
@@ -15,7 +15,7 @@ import {
BoxIcon,
BotMessageSquareIcon,
ActivityIcon,
- KeyIcon,
+ KeyRoundIcon,
} from "lucide-react";
export type PanelType =
@@ -112,7 +112,7 @@ export const PANELS: PanelDescriptor[] = [
{
// Not supported in WebAssembly yet
type: "secrets",
- Icon: KeyIcon,
+ Icon: KeyRoundIcon,
tooltip: "Secrets",
hidden: isWasm(),
position: "sidebar",