Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/javascript/components/common/NewListItemForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ export const NewListItemForm = <T extends unknown>({
onCompressed?: () => void
defaultError: Error
}): JSX.Element => {
const [value, setValue] = useState(
localStorage.getItem(`smde_${contextId}`) || ''
)
const [value, setValue] = useState(() => {
try {
return localStorage.getItem(`smde_${contextId}`) || ''
} catch {
return ''
}
})
const handleSuccess = useCallback(
(item: T) => {
setValue('')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ export const StartDiscussionPanel = ({
links: Links
}): JSX.Element => {
const contextId = `start-discussion-request-${request.uuid}`
const [state, setState] = useState({
expanded: defaultExpanded,
value: localStorage.getItem(`smde_${contextId}`) || '',
const [state, setState] = useState(() => {
let value = ''
try {
value = localStorage.getItem(`smde_${contextId}`) || ''
} catch {
// localStorage may be inaccessible (e.g. Safari private browsing)
}
return { expanded: defaultExpanded, value }
})
const lastIteration = iterations[iterations.length - 1]

Expand Down
18 changes: 11 additions & 7 deletions app/javascript/packs/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { lazy } from '@/utils/lazy-with-retry'
import { camelizeKeys } from 'humps'
import { camelizeKeysAs } from '@/utils/camelize-keys-as'
import { initReact } from '@/utils/react-bootloader'
import { safeLocalStorage } from '@/utils/safe-local-storage'
import { RenderLoader } from '@/components/common/RenderLoader'
import 'focus-visible'
import 'tippy.js/animations/shift-away-subtle.css'
Expand Down Expand Up @@ -220,7 +221,7 @@ declare global {

if (typeof window !== 'undefined') {
const persister = createSyncStoragePersister({
storage: window.localStorage,
storage: safeLocalStorage(),
key: 'REACT_QUERY_OFFLINE_CACHE',
// Strip non-serializable `promise` fields from dehydrated query state.
// When a query is pending during dehydration, its state includes a real
Expand Down Expand Up @@ -878,13 +879,16 @@ document.addEventListener('submit', function (event: SubmitEvent) {
'frontend-training-page-size',
]

const allKeys = Object.keys(localStorage)
const storage = safeLocalStorage()
if (storage) {
const allKeys = Object.keys(storage)

allKeys.forEach((key) => {
if (!keysToKeep.includes(key)) {
localStorage.removeItem(key)
}
})
allKeys.forEach((key) => {
if (!keysToKeep.includes(key)) {
storage.removeItem(key)
}
})
}
}
})

Expand Down
11 changes: 11 additions & 0 deletions app/javascript/utils/react-bootloader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ if (process.env.SENTRY_DSN) {
)
if (isStudentCodeError) return null

// Drop non-actionable localStorage/sessionStorage SecurityErrors (Safari private
// browsing, restricted iframes, or strict cookie settings block storage access entirely)
const isStorageAccessError = event.exception?.values?.some(
(ex) =>
ex.value?.includes(
"read the 'localStorage' property from 'Window'"
) ||
ex.value?.includes("read the 'sessionStorage' property from 'Window'")
)
if (isStorageAccessError) return null

// Drop non-actionable "Maximum call stack size exceeded" errors.
// These come from browser extensions, third-party scripts, or users
// with cached bundles (the root cause in app code was fixed in #8408).
Expand Down
14 changes: 14 additions & 0 deletions app/javascript/utils/safe-local-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Safely access window.localStorage.
*
* In some browsers (Safari private browsing, restricted iframes, strict cookie
* settings) even reading window.localStorage throws a SecurityError. This
* helper returns null when access is denied so callers can degrade gracefully.
*/
export function safeLocalStorage(): Storage | null {
try {
return window.localStorage
} catch {
return null
}
}
Loading