Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend): prevent localStorage errors on restricted WebViews (#14643)

sentry[bot]f11b45f6efa1
diff --git a/autogpt_platform/frontend/src/services/storage/__tests__/local-storage.test.ts b/autogpt_platform/frontend/src/services/storage/__tests__/local-storage.test.ts
index 0a4017bbcf..a79dcaefa6 100644
--- a/autogpt_platform/frontend/src/services/storage/__tests__/local-storage.test.ts
+++ b/autogpt_platform/frontend/src/services/storage/__tests__/local-storage.test.ts
@@ -10,12 +10,14 @@ vi.mock("@/services/environment", () => ({
},
}));
+import * as Sentry from "@sentry/nextjs";
import { Key, storage } from "../local-storage";
import { environment } from "@/services/environment";
describe("storage", () => {
beforeEach(() => {
window.localStorage.clear();
+ vi.mocked(Sentry.captureException).mockClear();
vi.mocked(environment.isServerSide).mockReturnValue(false);
});
@@ -38,6 +40,49 @@ describe("storage", () => {
});
});
+ describe("null localStorage", () => {
+ it("returns undefined for set and clean instead of throwing", () => {
+ const original = Object.getOwnPropertyDescriptor(window, "localStorage");
+ Object.defineProperty(window, "localStorage", {
+ value: null,
+ configurable: true,
+ });
+ try {
+ expect(storage.set(Key.COPILOT_MODE, "fast")).toBeUndefined();
+ expect(storage.clean(Key.COPILOT_MODE)).toBeUndefined();
+ } finally {
+ if (original) {
+ Object.defineProperty(window, "localStorage", original);
+ }
+ }
+ });
+ });
+
+ describe("unexpected write failures", () => {
+ it("reports them to Sentry when storage is available", () => {
+ const error = new Error("QuotaExceededError");
+ const setItem = vi
+ .spyOn(window.localStorage, "setItem")
+ .mockImplementation(() => {
+ throw error;
+ });
+ const removeItem = vi
+ .spyOn(window.localStorage, "removeItem")
+ .mockImplementation(() => {
+ throw error;
+ });
+ try {
+ storage.set(Key.COPILOT_MODE, "fast");
+ storage.clean(Key.COPILOT_MODE);
+ expect(Sentry.captureException).toHaveBeenCalledTimes(2);
+ expect(Sentry.captureException).toHaveBeenCalledWith(error);
+ } finally {
+ setItem.mockRestore();
+ removeItem.mockRestore();
+ }
+ });
+ });
+
describe("server-side guard", () => {
it("returns undefined for get when on server side", () => {
vi.mocked(environment.isServerSide).mockReturnValue(true);
diff --git a/autogpt_platform/frontend/src/services/storage/local-storage.ts b/autogpt_platform/frontend/src/services/storage/local-storage.ts
index 2281b75494..04fb72a0c3 100644
--- a/autogpt_platform/frontend/src/services/storage/local-storage.ts
+++ b/autogpt_platform/frontend/src/services/storage/local-storage.ts
@@ -33,6 +33,16 @@ export enum Key {
BUILDER_MOBILE_WARNING_SUPPRESSED = "builder-mobile-warning-suppressed",
}
+/** Returns true when localStorage is accessible — false when it is null or when
+ * accessing it throws (e.g. cookies/storage blocked by the browser). */
+function hasStorage(): boolean {
+ try {
+ return window.localStorage !== null;
+ } catch {
+ return false;
+ }
+}
+
function get(key: Key) {
if (environment.isServerSide()) {
Sentry.captureException(new Error("Local storage is not available"));
@@ -51,7 +61,16 @@ function set(key: Key, value: string) {
Sentry.captureException(new Error("Local storage is not available"));
return;
}
- return window.localStorage.setItem(key, value);
+ try {
+ return window.localStorage.setItem(key, value);
+ } catch (e) {
+ // localStorage is null/blocked on some WebViews — silently ignore that case.
+ // Any other error (e.g. QuotaExceededError) is unexpected and should be tracked.
+ if (hasStorage()) {
+ Sentry.captureException(e);
+ }
+ return;
+ }
}
function clean(key: Key) {
@@ -59,7 +78,16 @@ function clean(key: Key) {
Sentry.captureException(new Error("Local storage is not available"));
return;
}
- return window.localStorage.removeItem(key);
+ try {
+ return window.localStorage.removeItem(key);
+ } catch (e) {
+ // localStorage is null/blocked on some WebViews — silently ignore that case.
+ // Any other error is unexpected and should be tracked.
+ if (hasStorage()) {
+ Sentry.captureException(e);
+ }
+ return;
+ }
}
export const storage = {