Skip to content

Commit f286387

Browse files
committed
vweb: secure HttpOnly cookies
1 parent cdfbb29 commit f286387

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

vlib/builtin/string_test.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ fn test_replace_each() {
233233
'[code]', '<code>',
234234
'[/code]', '</code>'
235235
]) == '<b>bold</b> <code>code</code>'
236+
bb2 := '[b]cool[/b]'
237+
assert bb2.replace_each([
238+
'[b]', '<b>',
239+
'[/b]', '</b>',
240+
]) == '<b>cool</b>'
236241
}
237242

238243
fn test_itoa() {

vlib/vweb/tmpl/tmpl.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ _ = header
7676
}
7777
// HTML, may include `@var`
7878
else {
79-
s.writeln(line.replace('@', '\x24').replace('\'', '"') )
79+
s.writeln(line.replace('@', '\x24').replace("'", '"') )
8080
}
8181
}
8282
s.writeln(STR_END)

vlib/vweb/vweb.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ pub fn (ctx Context) not_found(s string) {
7474

7575
pub fn (ctx mut Context) set_cookie(key, val string) { // TODO support directives, escape cookie value (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie)
7676
//println('Set-Cookie $key=$val')
77-
ctx.add_header('Set-Cookie', '$key=$val')
77+
ctx.add_header('Set-Cookie', '$key=$val; Secure; HttpOnly')
7878
}
7979

8080
pub fn (ctx &Context) get_cookie(key string) ?string { // TODO refactor
81-
cookie_header := ' ' + ctx.get_header('Cookie')
81+
cookie_header := ' ' + ctx.get_header('cookie')
8282
cookie := if cookie_header.contains(';') {
8383
cookie_header.find_between(' $key=', ';')
8484
} else {

0 commit comments

Comments
 (0)