document.cookie中expires 格式设置问题导致部分iphone safari上登录失效

一、问题描述

设备信息:iPhone 12, iOS 16.3

昨天有个小伙伴发现自己的iPhone safari打开网页登录时,登录页面显示登录成功,但实际进入首页后仍然显示未登录。多次测试,该问题在该设备上属于必现问题。

二、问题排查与解决

经过排查发现:

并非所有iphone上都会出现该问题,比如:iPhone 7, iOS 15.3上就能正常登录显示。

通过代码走查,系统在登录页面登录成功后会将Token信息存储在cookie中,并在跳转首页时通过cookie中携带的token获取用户信息,只要存在用户信息则页面显示已登录状态。

那么,问题可能就出在cookie的设置上。

接下来,使用MacBook与问题机型进行联调,联调方法大致如下:

(1)在MacBook中打开safari,在safari浏览器->设置...->高级中勾选在菜单栏中显示"开发"菜单

(2)iPhone中打开设置,点击Safari浏览器->高级,打开网页检查器开关

(3)用数据线链接MacBook与iPhone,在MacBook 的 Safari顶部开发菜单中选择xxx的MacBook, 找到对应的网页打开调试即可。

以下是联调问题机型,查看最终设置的cookie的结果:

通过上述两张图可以发现,实际doment.cookie的设置代码与最终效果存在差异,主要体现在Pathexpires 两个属性值上。正确的设置效果应该类似下面的样子:

那么问题出在哪呢?Path作为字符串,格式设置上应该不会有什么问题,而expires 就不同了,它是有一定的格式要求的:

The cookie expiration date defines the time when the browser will automatically delete it (according to the browser's time zone).The date must be exactly in this format, in the GMT timezone. We can use date.toUTCString to get it. For instance, we can set the cookie to expire in 1 day:

// +1 day from now

let date = new Date(Date.now() + 86400e3);

date = date.toUTCString();

document.cookie = "user=John; expires=" + date;

对比代码,发现源代码少了toUTCString的调用。为了验证这个猜测,在登录页面联调时在Safari控制台直接运行测试代码:

运行后,进入首页正常显示登录状态,而此时的cookie审查也已正常设置:

至此,我们发现在iphone上如果document.cookie中expires 格式设置不正确会导致登录失效(当然,在比较早的机型上可能是正常的)

相关推荐
2501_9151063217 小时前
iOS 抓包全流程指南,HTTPS 抓包、TCP 数据流分析与多工具协同的方法论
android·tcp/ip·ios·小程序·https·uni-app·iphone
2501_915918412 天前
移动端 HTTPS 抓包实战,多工具组合分析与高效排查指南
数据库·网络协议·ios·小程序·https·uni-app·iphone
2501_915106323 天前
iOS性能调优的系统化实践,从架构分层到多工具协同的全流程优化指南(开发者深度版)
android·ios·小程序·架构·uni-app·iphone·webview
Digitally3 天前
无需 iTunes 备份与恢复 iPhone 的 2 种方法
iphone
2501_916007474 天前
iOS性能调试工具终极指南,从系统底层到多端协同的全方位优化实践(2025版)
android·ios·小程序·https·uni-app·iphone·webview
2501_915921434 天前
iOS崩溃日志深度分析与工具组合实战,从符号化到自动化诊断的完整体系
android·ios·小程序·uni-app·自动化·cocoa·iphone
2501_916008894 天前
没有源码如何加密 IPA 实战流程与多工具组合落地指南
android·ios·小程序·https·uni-app·iphone·webview
2501_915909064 天前
Flutter 应用怎么加固,多工具组合的工程化实战(Flutter 加固/Dart 混淆/IPA 成品加固/Ipa Guard + CI)
android·flutter·ios·ci/cd·小程序·uni-app·iphone
denggun123455 天前
ios包体积管理方案
ios·iphone
Digitally5 天前
解决 iPhone 和 Mac 之间备忘录无法同步的9种方法
macos·ios·iphone