sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] storage: the generic namespace() view reads and overwrites its parent key for an empty key (py + ts)

Open arronbailiss opened this issue 1 week ago • 0 comments

Human Overview

To be filled in by a human (AI-drafted issue, see team/AI_USAGE_POLICY.md).

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

Python, TypeScript

Strands Version

main @ 794d37c6e (strands-agents and @strands-agents/sdk from source); TypeScript also at 7296caba7

Language Runtime Version

Python 3.12.11, Node.js 24.15.0

Operating System

macOS 26.7.3

Installation Method

git clone

Steps to Reproduce

Both SDKs have a generic namespaced view for any Storage without a namespace() method of its own (TypeScript namespace() in strands-ts/src/storage/storage.ts, reached through resolveNamespace, the session manager and the stash; Python _NamespacedStorage in strands-py/src/strands/storage/storage.py:199, reached through _resolve_namespace). Neither checks the key. For '' or '/' the view asks the backend for prefix/, which the backend normalizes to the key prefix: an entry in the parent, outside the namespace. The shipped backends' own namespace() reject the same call.

TypeScript, run from strands-ts/ with npx tsx (the helper is internal, so the repro imports it from source):

import { InMemoryStorage } from './src/storage/index.js'
import { namespace, type Storage } from './src/storage/storage.js'

const base = new InMemoryStorage()
// A backend without its own namespace(), as a custom Storage would be.
const custom: Storage = {
  write: (k, d) => base.write(k, d),
  read: (k) => base.read(k),
  delete: (k) => base.delete(k),
  list: (q) => base.list(q),
}
await base.write('prefix', new Uint8Array([1]))
const view = namespace(custom, 'prefix')
console.log("view.read(''):", await view.read(''))
console.log("view.read('/'):", await view.read('/'))
await view.write('', new Uint8Array([9]))
console.log("parent 'prefix' after view.write(''):", await base.read('prefix'))
try { await base.namespace('prefix').read('') } catch (e) { console.log("InMemoryStorage.namespace('prefix').read(''):", (e as Error).message) }

Python, with strands-py on the path:

import asyncio
from strands.storage import InMemoryStorage
from strands.storage.storage import _NamespacedStorage

class Custom:  # a backend without its own namespace()
    def __init__(self, b): self.b = b
    async def write(self, k, d): await self.b.write(k, d)
    async def read(self, k): return await self.b.read(k)
    async def delete(self, k): await self.b.delete(k)
    async def list(self, q=""): return await self.b.list(q)

async def main():
    base = InMemoryStorage()
    await base.write("prefix", b"\x01")
    view = _NamespacedStorage(Custom(base), "prefix")
    print("view.read(''):", await view.read(""))
    print("view.read('/'):", await view.read("/"))
    await view.write("", b"\x09")
    print("parent 'prefix' after view.write(''):", await base.read("prefix"))
    try:
        await base.namespace("prefix").read("")
    except Exception as e:
        print("InMemoryStorage.namespace('prefix').read(''):", e)

asyncio.run(main())

Expected Behavior

The view rejects a key with no segments, as each SDK's InMemoryStorage.namespace(), LocalFileStorage.namespace() and S3Storage.namespace() do (Storage key must not be empty), so a namespaced view can never read, overwrite or delete its parent's key.

Actual Behavior

TypeScript:

view.read(''): Uint8Array(1) [ 1 ]
view.read('/'): Uint8Array(1) [ 1 ]
parent 'prefix' after view.write(''): Uint8Array(1) [ 9 ]
InMemoryStorage.namespace('prefix').read(''): Storage key must not be empty

Python:

view.read(''): b'\x01'
view.read('/'): b'\x01'
parent 'prefix' after view.write(''): b'\t'
InMemoryStorage.namespace('prefix').read(''): Storage key must not be empty

Additional Context

Same helper as #5685 (trailing-slash prefixes), but a separate problem: the open fix for that, #5686, changes only the TypeScript prefix normalization and does not cover empty keys.

arronbailiss avatar Nov 20 '26 17:11 arronbailiss