Programing

Rails 애플리케이션의 쿠키 오버플로?

crosscheck 2020. 8. 16. 19:13
반응형

Rails 애플리케이션의 쿠키 오버플로?


ActionDispatch :: Cookies :: CookieOverflow in UsersController # create

페이지를 열려고 할 때이 오류가 발생합니다. 이 오류를 디버깅하는 방법을 모르겠습니다. 이 문제에 대한 제안이 있습니까?

def create
  @user = User.new(params[:user])
  sign_in @user

  if @user.save
    @user.folders.create(:folder_name=>"Default Folder", :user_id=>@user.id)
    flash[:success] = "Welcome to Bunch<it>! "
    redirect_to @user
  else
    @title = "Sign up"
    render 'new'
  end
end


def sign_in(user)
  cookies.permanent.signed[:remember_token] = [user.id, user.salt]
  session[:current_user] = user
  current_user = user
end

쿠키에 저장할 수있는 항목에는 4kb 제한이 있으며, Rails가 쿠키에 쓰기 위해 개체를 텍스트로 변환하면 해당 제한보다 클 수 있습니다.

Ruby on Rails ActionDispatch::Cookies::CookieOverflow오류

이렇게하면이 CookieOverflow오류가 발생합니다.

이 문제를 해결하는 가장 쉬운 방법은 session_store를 변경하고 cookie_store. active_record_store예를 들어 사용할 수 있습니다 .

단계는 다음과 같습니다.

  1. 세션 테이블을 생성하는 마이그레이션 생성

    rake db:sessions:create
    
  2. 마이그레이션 실행

    rake db:migrate
    
  3. 수정 config/initializers/session_store.rb에서

    (App)::Application.config.session_store :cookie_store, :key => 'xxx'
    

    (App)::Application.config.session_store :active_record_store
    

세 단계를 완료했으면 애플리케이션을 다시 시작하십시오. Rails는 이제 세션 테이블을 사용하여 세션 데이터를 저장하며 4kb 제한이 없습니다.


:active_record_store기능이 Rails 4/5에서 작동 하도록하려면 activerecord-session_store gem을 다음에 추가해야 합니다 Gemfile.

gem 'activerecord-session_store'

그런 다음 마이그레이션 생성기를 실행하십시오.

rails generate active_record:session_migration
rake db:migrate

마지막으로 세션 저장소를 config/initializers/session_store.rb다음 위치에 설정하십시오 .

Rails.application.config.session_store :active_record_store, :key => '_my_app_session'

최신 정보:

null value in column "session_id" violates not-null constraint레일 4에서 메시지를 받는 사람이 있다면 github에 해결 방법이 있습니다 (테스트되지 않음). 이니셜 라이저를 생성해야합니다.ActiveRecord::SessionStore::Session.attr_accessible :data, :session_id


오류 메시지는 오버플로 인 쿠키 저장소 크기 문제를 명확하게 나타냅니다.

이 문제를 해결하려면 세션 (기본적으로 쿠키)을 활성 레코드 저장소 또는 Memcache 저장소로 이동해야합니다.

데이터베이스 세션의 경우 :

config.action_controller.session_store = :active_record_store

You need to create the session table as below

rake db:sessions:create
rake db:migrate

OR

For Memcache sessions:

config.action_controller.session_store = :mem_cache_store

Also you need to setup a mem cache server and configure it as below:

config.cache_store = :mem_cache_store, 'localhost', '127.0.0.1:11211',
{:namespace => 'myapp123'}

It's not a good idea to store a model object in the session.

Check out this railscast on this topic: http://railscasts.com/episodes/13-dangers-of-model-in-session?autoplay=true

It's a better practice to store the id (user's id in this case) inside the session. Then you won't have this problem.

(See Frederick Cheung comment above also).


If you're seeing this, check that you're not blowing up some session data. In my case, it was thousands of the same message pumped into the flash message. Just saying.

I'll add that if you think the solution is to make your cookie store bigger (as most of the other answers address), you're probably better off rethinking what you're actually putting in cookies. If you need more than a couple of auth tokens, session ID's, and maybe a few layout/tracking cookies, you're living in the 90's.


That error is because you are trying to serialize the user model When storing an object in a cookie, rails will use Marshal.dump which can produce a large amount of content since it's everything on the user record

Instead of storing the actual user record with session[:current_user] = user try just storing the users' ID then have a method a method to look up the user from that e.g.

def sign_in(user)
  ...
  session[:current_user_id] = user.id
end

def current_user
  @current_user ||= User.find(session[:current_user_id])
end

This error appeared for me when I was running a specs. After updating Capybara from 1.x to 2.x. Just rake tmp:clear solved it.

참고URL : https://stackoverflow.com/questions/9473808/cookie-overflow-in-rails-application

반응형