{"action":"created","issue":{"url":"https://api.github.com/repos/rack/rack/issues/848","labels_url":"https://api.github.com/repos/rack/rack/issues/848/labels{/name}","comments_url":"https://api.github.com/repos/rack/rack/issues/848/comments","events_url":"https://api.github.com/repos/rack/rack/issues/848/events","html_url":"https://github.com/rack/rack/issues/848","id":72101693,"number":848,"title":"\"Can't modify frozen Hash\" on request.cookies","user":{"login":"chengguangnan","id":861069,"avatar_url":"https://avatars.githubusercontent.com/u/861069?v=3","gravatar_id":"","url":"https://api.github.com/users/chengguangnan","html_url":"https://github.com/chengguangnan","followers_url":"https://api.github.com/users/chengguangnan/followers","following_url":"https://api.github.com/users/chengguangnan/following{/other_user}","gists_url":"https://api.github.com/users/chengguangnan/gists{/gist_id}","starred_url":"https://api.github.com/users/chengguangnan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chengguangnan/subscriptions","organizations_url":"https://api.github.com/users/chengguangnan/orgs","repos_url":"https://api.github.com/users/chengguangnan/repos","events_url":"https://api.github.com/users/chengguangnan/events{/privacy}","received_events_url":"https://api.github.com/users/chengguangnan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-04-30T08:43:20Z","updated_at":"2015-05-20T08:43:04Z","closed_at":null,"body":"I was try to access cookie from a rack middleware.  So I used `request.cookies`, but it gives \"cache error: can't modify frozen Hash\".\r\n\r\nThe problem came from `/rack-1.6.0/lib/rack/request.rb:296:in `cookies'`. \r\n\r\nOr on master branch, its [line 302](https://github.com/rack/rack/blob/master/lib/rack/request.rb#L302)\r\n\r\nIt seems to me that the code is try to modify the @env hash while it's frozen. \r\n\r\nIn my case, I have to make a dup to bypass this problem.\r\n\r\n`request = Rack::Request.new(request.env.dup)`\r\n\r\nIs the `@env[\"rack.request.cookie_hash\"] ` at line 302 a cache? Maybe its frozen state should be checked before modifying.\r\n\r\nBTW. I encountered this problem while using rack-cache. \r\n\r\n```\r\n    config.middleware.use Rack::Cache, {\r\n      metastore: \"file:tmp/cache/rack/meta\",\r\n      entitystore: \"file:tmp/cache/rack/body\",\r\n      verbose: true,\r\n      allow_reload: true,\r\n      allow_revalidate: true,\r\n      cache_key: lambda { |request|\r\n        request = Rack::Request.new(request.env.dup)\r\n        Rack::Cache::Key.new(request).generate + request.cookies[\"uid\"]\r\n      }\r\n```"},"comment":{"url":"https://api.github.com/repos/rack/rack/issues/comments/103811165","html_url":"https://github.com/rack/rack/issues/848#issuecomment-103811165","issue_url":"https://api.github.com/repos/rack/rack/issues/848","id":103811165,"user":{"login":"jodosha","id":5089,"avatar_url":"https://avatars.githubusercontent.com/u/5089?v=3","gravatar_id":"","url":"https://api.github.com/users/jodosha","html_url":"https://github.com/jodosha","followers_url":"https://api.github.com/users/jodosha/followers","following_url":"https://api.github.com/users/jodosha/following{/other_user}","gists_url":"https://api.github.com/users/jodosha/gists{/gist_id}","starred_url":"https://api.github.com/users/jodosha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jodosha/subscriptions","organizations_url":"https://api.github.com/users/jodosha/orgs","repos_url":"https://api.github.com/users/jodosha/repos","events_url":"https://api.github.com/users/jodosha/events{/privacy}","received_events_url":"https://api.github.com/users/jodosha/received_events","type":"User","site_admin":false},"created_at":"2015-05-20T08:43:04Z","updated_at":"2015-05-20T08:43:04Z","body":"@chengguangnan By grepping the source code of Rack, the only fronzen objects are string constants like `ETAG_STRING = 'ETag'.freeze`.\r\n\r\nOn the other hand, `Rack::Cache` seems to [freeze](https://github.com/rtomayko/rack-cache/blob/d00e6e491fcc7bdca9c27d735abde5c4fdb48cd9/lib/rack/cache/context.rb#L61) the environment before to pass it as argument for `Rack::Request`.\r\n\r\nThis last object works under the assumption of \"mutable env\" and cookies are exposed as a mutable Hash too. Eg `request.cookies['foo'] = 'bar'`.\r\n\r\nOne possible solution is to extract cookies parsing logic into `Rack::Utils` and make `Request` use it. This new function `.parse_cookies` should work without mutating the env. I'm preparing a PR with this approach."}}