Cookie中允许的字符

2023-10-31

本文翻译自:Allowed characters in cookies

this one's a quickie: 这是一个快捷方式:

What are the allowed characters in both cookie name and value? Cookie名称和值中允许使用哪些字符? Are they same as URL or some common subset? 它们是否与URL或某些公共子集相同?

Reason I'm asking is that I've recently hit some strange behavior with cookies that have - in their name and I'm just wondering if it's something browser specific or if my code is faulty. 原因我问的是,我已经创出近期与曲奇饼有一些奇怪的行为-他们的名字,我只是想知道,如果它是一些具体的事情浏览器,或者如果我的代码是错误的。


#1楼

参考:https://stackoom.com/question/8ghO/Cookie中允许的字符


#2楼

you can not put ";" 您不能输入“;” in the value field of a cookie, the name that will be set is the string until the ";" 在Cookie的value字段中,要设置的名称是字符串,直到“;”为止 in most browsers... 在大多数浏览器中...


#3楼

I think it's generally browser specific. 我认为这通常是特定于浏览器的。 To be on the safe side, base64 encode a JSON object, and store everything in that. 为了安全起见,base64对JSON对象进行编码,然后将所有内容存储在其中。 That way you just have to decode it and parse the JSON. 这样,您只需要解码并解析JSON。 All the characters used in base64 should play fine with most, if not all browsers. 如果不是所有浏览器,base64中使用的所有字符都应该可以正常运行。


#4楼

There are 2 versions of cookies specifications Cookie规范有2个版本
1. Version 0 cookies aka Netscape cookies, 1.版本0 Cookie或Netscape Cookie,
2. Version 1 aka RFC 2965 cookies 2.版本1 aka RFC 2965 cookie
In version 0 The name and value part of cookies are sequences of characters, excluding the semicolon, comma, equals sign, and whitespace, if not used with double quotes 在版本0中,cookie的名称和值部分是字符序列,如果不使用双引号,则不包括分号,逗号,等号和空格
version 1 is a lot more complicated you can check it here 版本1复杂得多,您可以在此处进行检查
In this version specs for name value part is almost same except name can not start with $ sign 在此版本中,名称值部分的规格几乎相同,只是名称不能以$符号开头


#5楼

this one's a quickie: 这是一个快捷方式:

You might think it should be, but really it's not at all! 您可能会认为应该这样做,但实际上根本不行!

What are the allowed characters in both cookie name and value? Cookie名称和值中允许使用哪些字符?

According to the ancient Netscape cookie_spec the entire NAME=VALUE string is: 根据古老的Netscape cookie_spec ,整个NAME=VALUE字符串为:

a sequence of characters excluding semi-colon, comma and white space. 一系列字符,不包括分号,逗号和空格。

So - should work, and it does seem to be OK in browsers I've got here; 所以-应该可以工作,而且在我来到这里的浏览器中似乎还可以; where are you having trouble with it? 您在哪里遇到麻烦?

By implication of the above: 通过以上暗示:

  • = is legal to include, but potentially ambiguous. =是合法的包括,但可能具有不同含义。 Browsers always split the name and value on the first = symbol in the string, so in practice you can put an = symbol in the VALUE but not the NAME. 浏览器始终在字符串的第一个=符号上拆分名称和值,因此在实践中,您可以在VALUE中放置=符号,但不能在NAME中放置=符号。

What isn't mentioned, because Netscape were terrible at writing specs, but seems to be consistently supported by browsers: 没有提到什么,因为Netscape在编写规范时很糟糕,但是似乎始终受到浏览器的支持:

  • either the NAME or the VALUE may be empty strings NAME或VALUE可能为空字符串

  • if there is no = symbol in the string at all, browsers treat it as the cookie with the empty-string name, ie Set-Cookie: foo is the same as Set-Cookie: =foo . 如果字符串中根本没有=符号,浏览器会将其视为具有空字符串名称的cookie,即Set-Cookie: fooSet-Cookie: =foo

  • when browsers output a cookie with an empty name, they omit the equals sign. 当浏览器输出名称为空的cookie时,它们会省略等号。 So Set-Cookie: =bar begets Cookie: bar . 所以Set-Cookie: =bar Cookie: bar

  • commas and spaces in names and values do actually seem to work, though spaces around the equals sign are trimmed 名称和值中的逗号和空格似乎确实有效,尽管等号周围的空格已修剪

  • control characters ( \\x00 to \\x1F plus \\x7F ) aren't allowed 不允许使用控制字符( \\x00\\x1F加上\\x7F

What isn't mentioned and browsers are totally inconsistent about, is non-ASCII (Unicode) characters: 未提及且浏览器完全不一致的是非ASCII(Unicode)字符:

  • in Opera and Google Chrome, they are encoded to Cookie headers with UTF-8; 在Opera和Google Chrome中,它们使用UTF-8编码为Cookie标头;
  • in IE, the machine's default code page is used (locale-specific and never UTF-8); 在IE中,使用计算机的默认代码页(特定于语言环境,从不使用UTF-8);
  • Firefox (and other Mozilla-based browsers) use the low byte of each UTF-16 code point on its own (so ISO-8859-1 is OK but anything else is mangled); Firefox(和其他基于Mozilla的浏览器)自行使用每个UTF-16代码点的低字节(因此,ISO-8859-1可以,但其他任何东西都被破坏了);
  • Safari simply refuses to send any cookie containing non-ASCII characters. Safari只是拒绝发送任何包含非ASCII字符的cookie。

so in practice you cannot use non-ASCII characters in cookies at all. 因此实际上,您根本不能在Cookie中使用非ASCII字符。 If you want to use Unicode, control codes or other arbitrary byte sequences, the cookie_spec demands you use an ad-hoc encoding scheme of your own choosing and suggest URL-encoding (as produced by JavaScript's encodeURIComponent ) as a reasonable choice. 如果要使用Unicode,控制代码或其他任意字节序列,则cookie_spec要求您使用自己选择的即席编码方案,并建议将URL编码(由JavaScript的encodeURIComponent产生)作为合理选择。

In terms of actual standards, there have been a few attempts to codify cookie behaviour but none thus far actually reflect the real world. 实际标准方面,已经进行了一些尝试来整理Cookie行为,但是到目前为止,还没有任何方法能够真正反映出真实世界。

  • RFC 2109 was an attempt to codify and fix the original Netscape cookie_spec. RFC 2109试图对原始Netscape cookie_spec进行编码和修复。 In this standard many more special characters are disallowed, as it uses RFC 2616 tokens (a - is still allowed there), and only the value may be specified in a quoted-string with other characters. 在该标准中,不允许使用更多特殊字符,因为它使用RFC 2616令牌(此处仍然允许使用- ),并且只能在带引号的字符串中指定该值以及其他字符。 No browser ever implemented the limitations, the special handling of quoted strings and escaping, or the new features in this spec. 没有浏览器实现限制,对引号引起的字符串的特殊处理和转义,或者此规范中的新功能。

  • RFC 2965 was another go at it, tidying up 2109 and adding more features under a 'version 2 cookies' scheme. RFC 2965是另一种解决方法,它整理了2109,并在“版本2 Cookie”方案下添加了更多功能。 Nobody ever implemented any of that either. 也没有人实施过任何一个。 This spec has the same token-and-quoted-string limitations as the earlier version and it's just as much a load of nonsense. 该规范与早期版本具有相同的标记和引号字符串限制,并且是一堆废话。

  • RFC 6265 is an HTML5-era attempt to clear up the historical mess. RFC 6265是HTML5时代试图清除历史混乱的尝试。 It still doesn't match reality exactly but it's much better then the earlier attempts—it is at least a proper subset of what browsers support, not introducing any syntax that is supposed to work but doesn't (like the previous quoted-string). 它仍然不完全符合现实,但是比早期的尝试要好得多-它至少是浏览器支持的一个适当子集,没有引入任何应该起作用但不起作用的语法(例如之前的带引号的字符串) 。

In 6265 the cookie name is still specified as an RFC 2616 token , which means you can pick from the alphanums plus: 在6265中,cookie名称仍然指定为RFC 2616 token ,这意味着您可以从字母数字加:

!#$%&'*+-.^_`|~

In the cookie value it formally bans the (filtered by browsers) control characters and (inconsistently-implemented) non-ASCII characters. 在cookie值中,它正式禁止(由浏览器过滤)控制字符和(不一致执行的)非ASCII字符。 It retains cookie_spec's prohibition on space, comma and semicolon, plus for compatibility with any poor idiots who actually implemented the earlier RFCs it also banned backslash and quotes, other than quotes wrapping the whole value (but in that case the quotes are still considered part of the value, not an encoding scheme). 它保留了cookie_spec对空格,逗号和分号的禁止,并且为了与实际上实施较早RFC的任何可怜的白痴兼容,它还禁止反斜杠和引号,但引号包装了整个值(但在这种情况下,引号仍被认为是值,而不是编码方案)。 So that leaves you with the alphanums plus: 这样就剩下字母数字加号了:

!#$%&'()*+-./:<=>?@[]^_`{|}~

In the real world we are still using the original-and-worst Netscape cookie_spec, so code that consumes cookies should be prepared to encounter pretty much anything, but for code that produces cookies it is advisable to stick with the subset in RFC 6265. 在现实世界中,我们仍在使用原始和最差的Netscape cookie_spec,因此应该准备使用cookie的代码来处理几乎所有内容,但是对于生成cookie的代码,建议坚持使用RFC 6265中的子集。


#6楼

Newer rfc6265 published in April 2011: 2011年4月发布的较新的rfc6265

cookie-header = "Cookie:" OWS cookie-string OWS
cookie-string = cookie-pair *( ";" SP cookie-pair )
cookie-pair  = cookie-name "=" cookie-value
cookie-value = *cookie-octet / ( DQUOTE *cookie-octet DQUOTE )

cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E
                   ; US-ASCII characters excluding CTLs,
                   ; whitespace DQUOTE, comma, semicolon,
                   ; and backslash

If you look to @bobince answer you see that newer restriction more strict. 如果您使用@bobince答案,则会看到更新的限制更加严格。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Cookie中允许的字符 的相关文章

  • NodeJS 快速会话 req.session 未定义

    我正在开发一个简单的登录系统 但会话似乎没有保存 我编写了一个简单的代码进行测试 谁能告诉我它有什么问题吗 我正在运行登录 之后我正在运行 is logged 但我从未登录并且两个会话都未定义 var port process env PO
  • Rails 3 session_store 域 :all 的作用是什么?

    更新了问题以使其更清楚 据我所知 您可以设置 session store 的域以在子域之间共享会话 如下所示 Rails application config session store cookie store key gt my key
  • 为什么我们在 Javascript 中将秒乘以 1000 来添加时间

    我正在研究 cookie 我在 Stack Overflow 上发现了一些问题 这些问题已经得到解答 我在问题中看到答案将秒乘以 1000 我想知道 getTime 返回什么格式 这需要乘以 1000 让cookie在30秒后过期 http
  • 在 Rails 5 控制器集成测试中设置 cookie.signed

    想象一下这样的场景 有一个控制器集成测试调用一个控制器方法 其中cookie signed用于一些完整性检查 控制器 app controllers foo controller rb def index entity FooEntity
  • 在Android中存储和恢复cookie(持久cookie存储)

    搜索了很多 我有一个应用程序 应用程序登录服务器并接收一些cookie 然后它可以使用它们执行一些POST请求 例如获取用户个人资料 我想在会话之间存储它们 这意味着我可以重新启动设备 运行应用程序并获取配置文件 而无需额外登录 或者 换句
  • Cookie 不会重置

    好吧 我被难住了 我已经盯着这个看了好几个小时了 我使用以下代码在 access login php 设置 cookie setcookie username username time 604800 当我尝试注销 位于 access lo
  • 通过cas进行ajax调用

    我需要编写一个谷歌小工具来读取谷歌群组的提要 问题是我正在进行 ajax 调用来检索提要 而我们的 google apps 域受 CAS 中央身份验证服务 保护 因此 我在拨打电话时收到 400 错误请求 我怀疑浏览器在进行 ajax 调用
  • 使用 Rails 自动登录?

    我正在尝试使用 Rails 的 Restful Authentication 插件建立一个简单的身份验证系统 我只是想知道它是如何工作的 b c 我似乎无法弄清楚 cookie 的要求是什么 以及如何做到这一点浏览器始终会记住您 6 个多月
  • 删除 servlet 中的 cookie 时出现问题

    我尝试使用以下代码删除 servlet 中的 cookie Cookie minIdCookie null for Cookie c req getCookies if c getName equals iPlanetDirectoryPr
  • Cordova 上的 ClearCookiesAsync()

    我正在尝试使用 wp8 cordova 中的插件来清除 WebBrowser cookie 我已经让它与 JavaScript 进行通信 并且我的 c 文件中有类似这样的内容 using WPCordovaClassLib Cordova
  • .NET 中的 Cookie 和会话过期

    我有一个 MVC4 单一应用程序页面 登录页面有 3 个字段 用户 密码和 记住我 复选框 C 登录代码是这样的 if WebSecurity Login model UserName model Password persistCooki
  • 如何在 Rails rspec 中测试 cookie 过期时间

    在 rspec 中设置 cookie 有很多困惑http relishapp com rspec rspec rails v 2 6 dir controller specs file cookies http relishapp com
  • nginx代理认证拦截

    我有几个服务 它们支持 nginx 实例 为了处理身份验证 在 nginx 中 我拦截每个请求并将其发送到身份验证服务 在那里 如果凭据正确 我将设置一个包含用户相关信息的 cookie 现在 请求应该被路由到适当的服务 并设置 cooki
  • IdentityServer4 客户端 - 刷新 CookieAuthenticationEvents 上的访问令牌

    我试图在访问令牌过期时使用刷新令牌 类似的问题已回答here https stackoverflow com a 41557598 3501052 And 更新令牌的示例代码 https stackoverflow com question
  • 如何用javascript正确读取php cookies

    考虑这个 php 和 javascript 代码 然后我在控制台中看到的是 utma 111872281 291759993 1444771465 1445374822 1445436904 4 utmz 111872281 1444771
  • 我可以更改 FormsAuthentication cookie 名称吗?

    我可以更改 FormsAuthentication cookie 名称吗 如果是 如何 我遇到的问题是 当我在同一个域中部署两个 Web 应用程序时 当任何人登录时 第二个应用程序将自动注销 因为它们使用相同的身份验证 cookie 名称
  • PHP 读取使用 setcookie() 创建的 cookie

    来自manual https www php net setcookie 直到下一次加载 Cookie 应该可见的页面之前 Cookie 才会变得可见 这意味着创建的 cookiesetcookie将无法访问 COOKIE直到下一页加载 有
  • 如何设置cookie值?

    我正在执行以下操作来设置 cookie 值 HttpCookie mycookie new HttpCookie mycookie mycookie Value value1 Case sensitivity mycookie Expire
  • 每个网络请求都会发送浏览器cookie吗?

    每个网络请求都会发送浏览器的cookie吗 我不是在谈论页面浏览量 而是对图像的请求 js文件等 Update如果一个网页有 50 个元素 那就是 50 个请求 为什么它会为每个请求发送相同的 cookie 它不会缓存或知道它已经拥有它吗
  • 如何保护Web应用程序免受cookie窃取攻击?

    我的网络应用程序的身份验证机制目前非常简单 当用户登录时 网站会发回一个存储的会话 cookie 使用localStorage 在用户的浏览器上 但是 此 cookie 很容易被窃取并用于从另一台计算机重播会话 我注意到其他网站 例如 Gm

随机推荐

  • C语言

    目录 一 实验环境 二 黑白圣诞树 三 windows h简介 四 windows h实现彩色圣诞树 1 设置用户窗口 2 移动光标 3 修改字体颜色 4 绘制圣诞树 5 绘制雪景 6 完整代码 7 运行 一 实验环境 编译环境 vc 6
  • 【因果推断与机器学习】带入坑——之辛普森悖论

    因果推断与机器学习 Why you might Care Simpson s Paradox 考虑一个纯粹假设的未来 那里有一种被称为COVID 27的新疾病在人类中普遍存在 在这个纯粹假设的未来中 已经开发了两种治疗方法 治疗A和治疗B
  • 西门子dcs系统组态手册下载_PLC/DCS/HMI 知识普及

    什么是PLC 可编程控制器 简称PLC Programmable logic Controller 是指以计算机技术为基础的新型工业控制装置 在1987年国际电工委员会颁布的PLC标准草案中对PLC做了如下定义 PLC是一种专门为在工业环境
  • 原版安装Win10 1909专业版 64位MSDN镜像2020 05

    原版安装Win10 1909专业版 64位MSDN镜像2020 05 一 更新内容 1 去除预装kms激活 未激活的可以通过桌面自行激活 2 提升商店购买应用的速度性能 3 解决在IE浏览器中阻碍下载和安装 NET组件的问题 4 更新修复补
  • Qt5.14.2 MInGW静态编译配置教程

    Qt5 14 2 MinGW静态编译教程 1 安装Qt 1 1 下载安装包 1 2 安装 2 工具的下载安装 3 检查上述配置是否成功 4 静态编译qmake 4 1 静态编译配置 4 2 编译 4 3 安装静态库 4 4新增静态编译 1
  • SQL Server(五)-视图

    与表一样 视图也是由字段和记录组成的 只是这些字段和记录来源于其他被引用的表或视图 所以视图并不是真实存在的 而是一张虚拟的表 视图中的数据并不是存在于视图中的 而是存在于被引用的数据表当中的 当被引用的数据表中的记录内容改变时 视图中的记
  • 临沂地区的OLED拼接屏有哪些独特优点?

    临沂oled拼接屏是一种高清晰度的显示屏 由多个oled屏幕拼接而成 它可以用于商业广告 展览 会议 演出等场合 具有高亮度 高对比度 高色彩饱和度 高刷新率等优点 能够吸引人们的眼球 提高信息传递效果 临沂oled拼接屏的优点之一是高亮度
  • 虚拟偶像是未来趋势吗?

    Hello 我的朋友 这里是古希伯 今天聊聊 虚拟偶像行业是不是未来的趋势 虚拟偶像零都知道吧 日本初音未来 洛天依诸多的 日本虚拟偶像行业市场是最为发达的 日漫这一块本身具有先天优势存在 乃至于国内诸多公司都没有能力完全复刻 虚拟偶像目前
  • slot-插槽的基本使用-具名插槽的使用(重要)

    slot 插槽的基本使用 具名插槽的使用 为什么使用slot slot翻译为插槽 1 在生活中很多地方都有插槽 电脑的USB插槽 插板当中的电源插槽 2 插槽的目的是让我们原来的设备具有更多的扩展性 3 比如电脑的USB我们可以插入U盘 硬
  • 手撸,自定义application.yml配置项

    文章目录 前言 教程 1 加入配置依赖 及maven插件 2 元注解 分析 3 配置文件 4 测试能不能用 5 怎么从配置里面取值 并加载呢 第一 我们在配置中加入值 第二 编写一个自动配置类 前言 我们现在在springboot中 极其简
  • Spring Dynamic Modules - DMserver

    spring dm server 官网 http static springsource com projects dm server 1 0 x programmer guide htmlsingle programmer guide h
  • 英语发音规则---gh

    英语发音规则 gh 一 总结 一句话总结 gh字母组合的读音在中学英语课本中归纳起来主要有 发音 和 不发音 两种情况 gh字词首是发 g 因为需要开头啊 例如 ghost g st n 鬼 幽灵 gh在词尾读作 f 因为需要尾巴 例如 l
  • 什么是相关性

    我们曾经讲过 默认情况下 返回结果是按相关性倒序排列的 但是什么是相关性 相关性如何计算 每个文档都有相关性评分 用一个正浮点数字段 score 来表示 score 的评分越高 相关性越高 查询语句会为每个文档生成一个 score 字段 评
  • OPENCV+QT环境配置

    qt opencv开发入门 4步搞定opencv环境配置2 https www bilibili com video BV1f34y1v7t8 vd source 0aeb782d0b9c2e6b0e0cdea3e2121eba 第一步 安
  • U20.4 升级 pytorch 1.11

    一 系统环境 1 系统已安装 pytorch 1 7 1 8 a pip install torch 1 8 2 cu111 torchvision 0 9 2 cu111 torchaudio 0 8 2 f https download
  • 小编必看,教你如何使用微信公众号编辑器快速排版精美文章

    对于小编而言 写作就是每天的日常 图文排版是公众号的形象 也是新媒体运营小编必须掌握的技能 一篇文章除了内容优质之外 排版也是非常重要的 好的排版才能让读者有更好的阅读体验 俗话说 工欲善其事 必先利其器 众多微信公众号编辑器中 推荐使用来
  • CMAKE语法:add_definitions、add_compile_options

    前言 CMAKE命令通用理解 command
  • SQL_开发技巧(附阿里巴巴开发规范)

    目录 一 数据库命令规范 1 1 explain 1 2 单表delete或者update语句 加个limit 1 3 where后字段 隐式转换 1 4 减少不必要的查询字段 1 5 SQL命令行修改数据 养成begin commit 事
  • 【ProVerif学习笔记】6:握手协议(handshake protocol)建模

    这节先不继续往下看 巩固一下对ProVerif建模握手协议的理解 握手协议的流程如下 ex handshake pv 只验证保密性 手册里在引入security性质之前 握手协议的模型如下 对称加密相关 对称密钥 type key 对称加密
  • Cookie中允许的字符

    本文翻译自 Allowed characters in cookies this one s a quickie 这是一个快捷方式 What are the allowed characters in both cookie name an