Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Экранируем HTTP-заголовки в ответе #126

Merged
merged 1 commit into from
Nov 12, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/de.response.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,29 @@ de.Response.prototype.setRedirect = function(location) {

// --------------------------------------------------------------------------------------------------------------- //

function escapeHeader(header) {
return header
.replace(/([\uD800-\uDBFF][\uDC00-\uDFFF])+/g, encodeURI) // валидные суррогатные пары
.replace(/[\uD800-\uDFFF]/g, '') // невалидные половинки суррогатных пар
.replace(/[\u0000-\u001F\u007F-\uFFFF]+/g, encodeURI); // всё остальное непечатное
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А ты уверен, что тут надо не encodeURIComponent?
И может проще/дешевле весь хедер целиком энкодить, чем такими регэкспами фигачить?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для не-ascii разницы между encodeURI и encodeURIComponent нет.

Не проще. Например, заголовок Location: /ф?n=m%26m&t=ф сломается, т.к. % заэнкодится ещё раз и получится /%D1%84?n=m%2526m&t=%D1%84 вместо '/%D1%84?n=m%26m&t=%D1%84

de.Response.prototype.end = function(response, result) {
var headers = this.headers;
for (var header in headers) {
response.setHeader( header, headers[header] );
response.setHeader( header, escapeHeader(headers[header]) );
}

var cookies = this.cookies;
var cookie = [];
for (var name in cookies) {
cookie.push(name + '=' + cookies[name]);
cookie.push(escapeHeader(name + '=' + cookies[name]));
}
response.setHeader('Set-Cookie', cookie); // FIXME: Выставлять expire и т.д.

if (this.location) {
response.statusCode = 302;
response.setHeader('Location', this.location);
response.setHeader('Location', escapeHeader(this.location));
response.end();
return;
}
Expand Down