Skip to content

A loaded state with currentIndex: -1 but non-empty items is accepted, and the next NavigateTo silently deletes the whole restored history #69

Closed

Description

@matt-edmondson

What's wrong

The NavigationState<T> constructor allows currentIndex == -1 for any number of items (Navigation/Models/NavigationState.cs ~L33). The stack itself only produces -1 when it is empty (FromNavigationStack).

Navigation<T>.LoadStateAsync passes the loaded state straight into RestoreState (Navigation/Services/NavigationStack.cs ~L160, ~L169-177), and RestoreState does no validation. After such a load, the stack has items but no current page:

  • Current == null
  • CanGoBack == false
  • CanGoForward == true

The next NavigateTo treats every restored item as forward history and removes it (RemoveRange(_currentIndex + 1, ...) with _currentIndex == -1).

LoadStateAsync is the single entry point for every IPersistenceProvider<T>, including custom ones (Architecture.md presents a DatabasePersistenceProvider). Yet the only checks on a loaded state, the null-item check added for #60 and the index range check, live in NavigationState's constructor. A state that bypasses them reaches RestoreState unchecked.

Reproduction

  1. Write this state file:
    {"items":[{"id":"a","displayName":"A"},{"id":"b","displayName":"B"}],"currentIndex":-1}
  2. Call LoadStateAsync(). It returns true, with count=2, Current=null, CanGoBack=false, CanGoForward=true.
  3. Call NavigateTo(new NavigationItem("x","X")). The history is now just X; A and B are gone.

Once the stack is in this state, SaveStateAsync persists currentIndex: -1 with items again, so the corrupt state round-trips.

Suggested fix / acceptance criteria

  • In the NavigationState constructor, reject currentIndex == -1 when items is non-empty. JsonFilePersistenceProvider already maps ArgumentException to a null load.
  • Also validate in LoadStateAsync, so every provider gets the same checks:
    • no null items
    • -1 <= CurrentIndex < Items.Count
    • -1 only when Items is empty
  • On an invalid state, return false and leave the stack unchanged.
  • Tests:
    • The JSON above loads as false and leaves the existing stack untouched.
    • A custom provider returning an out-of-range index or a null item makes LoadStateAsync return false without changing the stack.

Activity

  1. added
    bug
    on Sep 28, 2026
  2. matt-edmondson commented on Sep 28, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Suggested next step: move all state validation into LoadStateAsync, so custom providers go through the same checks. Keep the constructor check as a second guard.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions