Skip to content

Instantly share code, notes, and snippets.

@danmayer
Created August 14, 2019 18:56
Show Gist options
  • Save danmayer/7e317e93ae10990a7b0393c4e8df8cba to your computer and use it in GitHub Desktop.
Save danmayer/7e317e93ae10990a7b0393c4e8df8cba to your computer and use it in GitHub Desktop.
Debugging Growing Cookies and Cookie Overflows
####
# This can be inserted in a before filter, or an endpoint causing cookie overflows
# this will log data for users who are approaching the max_cookie_size
# it will also for the most common cookie to overflow session, break down the size by key value pairs.
# This logs cookie and key names, but none of the data which may include PII, it just logs the byte sizes
#
# How to read:
# Cookie Debug (size: 3280) # overall cookies total size in bytes
# [COOKIE_NAME] size=IN_BYTES # these are sorted by smallest to largest byte size
# session: [KEY] size=IN_BYTES # for the session cookie, the key name with values in byte size sorted
#
# example output:
# Cookie Debug (size: 3280): [_dmxyz_c] size=0, [_xyz_ses.zzd] size=1, [_dzzz_ses] size=1, [_gat_UA-12345] size=1, [_gat_xyz] size=1, [_sp_xyz] size=1, [_xyz_toffset] size=2, [_dxyzcnst] size=2, [show_xyz] size=5, [stuff] size=6, [morestuff] size=10, [_xyzcst] size=10, [xyxxgbd] size=13, [_xdfdffs] size=13, [_dfsdfdload_seq] size=19, [_sdfdsf] size=19, [_sdfsdid] size=20, [_sdfdso] size=24, [_sdfsdct] size=24, [_gsdfsdau] size=24, [_sdfds] size=26, [_sdfd] size=26, [fbm_sdfsdf] size=26, [__sdfa] size=27, [_sdfsdff_geo] size=29, [_sdfsdfp] size=29, [_sdfsdfsion] size=32, [__sdfsdfid] size=36, [_wcsdfdsfd] size=36, [_wchtbl_uid] size=36, [adfsdfds_uuid] size=36, [Isdfsdf9] size=37, [IsdfsdfI] size=50, [pisdfsdfkie] size=100, [snsdsdfga] size=100, [_gac_sdfsdf9] size=105, [_gsdfsdfw] size=107, [_gsdfsdfc] size=107, [_dsdfsdf5d] size=108, [_spsdfsdf5d] size=108, [_app_one_session] size=112, [_sdfsdpik] size=135, [_APP_session] size=394, [fbsr_asdfsdf] size=620 session: [handoff_to_ios_after_style_profile] size=5, [session_id] size=32, [_csrf_token] size=44
####
if request.headers['Cookie'] && request.headers['Cookie'].bytesize > (MAX_COOKIE_SIZE * 0.80)
cookie_data = {}
cookies.each do |key, val|
cookie_data[val.to_s.bytesize.to_i] = [cookie_data[val.to_s.bytesize.to_i], "[#{key}] size=#{val.to_s.bytesize}"].compact.join(', ')
end
cookie_data = cookie_data.sort.map{ |_k, val| val }.join(', ')
session_data = {}
session.each do |key, val|
session_data[val.to_s.bytesize.to_i] = [session_data[val.to_s.bytesize.to_i], "[#{key}] size=#{val.to_s.bytesize}"].compact.join(', ')
end
session_data = session_data.sort.map{ |_k, val| val }.join(', ')
Rails.logger.info "Cookie Debug (size: #{request.headers['Cookie'].bytesize}): #{cookie_data} session: #{session_data}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment