Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
jenkins
提交
ab8d3304
J
jenkins
项目概览
xxadev
/
jenkins
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
ab8d3304
编写于
7月 05, 2019
作者:
O
Oleg Nenashev
提交者:
GitHub
7月 05, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3939 from jsoref/logout-kill-sessions
[JENKINS-25046] Drop cookie sessions when logging out
上级
740f9eb3
1684de42
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
105 addition
and
9 deletion
+105
-9
core/src/main/java/hudson/security/SecurityRealm.java
core/src/main/java/hudson/security/SecurityRealm.java
+40
-4
test/src/test/java/hudson/security/SecurityRealmTest.java
test/src/test/java/hudson/security/SecurityRealmTest.java
+65
-5
未找到文件。
core/src/main/java/hudson/security/SecurityRealm.java
浏览文件 @
ab8d3304
...
...
@@ -68,6 +68,7 @@ import java.io.IOException;
import
java.io.UnsupportedEncodingException
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
/**
...
...
@@ -290,15 +291,50 @@ public abstract class SecurityRealm extends AbstractDescribableImpl<SecurityReal
Authentication
auth
=
SecurityContextHolder
.
getContext
().
getAuthentication
();
SecurityContextHolder
.
clearContext
();
// reset remember-me cookie
Cookie
cookie
=
new
Cookie
(
ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE_KEY
,
""
);
String
contextPath
=
req
.
getContextPath
().
length
()
>
0
?
req
.
getContextPath
()
:
"/"
;
resetRememberMeCookie
(
req
,
rsp
,
contextPath
);
clearStaleSessionCookies
(
req
,
rsp
,
contextPath
);
rsp
.
sendRedirect2
(
getPostLogOutUrl
(
req
,
auth
));
}
private
void
resetRememberMeCookie
(
StaplerRequest
req
,
StaplerResponse
rsp
,
String
contextPath
)
{
Cookie
cookie
=
new
Cookie
(
ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE_KEY
,
""
);
cookie
.
setMaxAge
(
0
);
cookie
.
setSecure
(
req
.
isSecure
());
cookie
.
setHttpOnly
(
true
);
cookie
.
setPath
(
req
.
getContextPath
().
length
()>
0
?
req
.
getContextPath
()
:
"/"
);
cookie
.
setPath
(
contextPath
);
rsp
.
addCookie
(
cookie
);
}
rsp
.
sendRedirect2
(
getPostLogOutUrl
(
req
,
auth
));
private
void
clearStaleSessionCookies
(
StaplerRequest
req
,
StaplerResponse
rsp
,
String
contextPath
)
{
/* While "executableWar.jetty.sessionIdCookieName" and
* "executableWar.jetty.disableCustomSessionIdCookieName"
* <https://github.com/jenkinsci/extras-executable-war/blob/6558df699d1366b18d045d2ffda3e970df377873/src/main/java/Main.java#L79-L97>
* can influence the current running behavior of the generated session cookie, we aren't interested
* in either of them at all.
*
* What matters to us are any stale cookies.
* Those cookies would have been created by this jenkins in a different incarnation, when it
* could, perhaps, have had different configuration flags, including for those configurables.
*
* Thus, we unconditionally zap all JSESSIONID. cookies.
* a new cookie will be generated by sendRedirect2(...)
*
* We don't care about JSESSIONID cookies outside our path because it's the browser's
* responsibility not to send them to us in the first place.
*/
final
String
cookieName
=
"JSESSIONID."
;
for
(
Cookie
cookie
:
req
.
getCookies
())
{
if
(
cookie
.
getName
().
startsWith
(
cookieName
))
{
LOGGER
.
log
(
Level
.
FINE
,
"Removing cookie {0} during logout"
,
cookie
.
getName
());
// one reason users log out is to clear their session(s)
// so tell the browser to drop all old sessions
cookie
.
setMaxAge
(
0
);
cookie
.
setValue
(
""
);
rsp
.
addCookie
(
cookie
);
}
}
}
/**
...
...
test/src/test/java/hudson/security/SecurityRealmTest.java
浏览文件 @
ab8d3304
...
...
@@ -23,10 +23,11 @@
*/
package
hudson.security
;
import
com.gargoylesoftware.htmlunit.CookieManager
;
import
com.gargoylesoftware.htmlunit.WebResponse
;
import
com.gargoylesoftware.htmlunit.util.NameValuePair
;
import
com.gargoylesoftware.htmlunit.html.HtmlPage
;
import
com.gargoylesoftware.htmlunit.util.Cookie
;
import
hudson.security.captcha.CaptchaSupport
;
import
org.hamcrest.CoreMatchers
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.jvnet.hudson.test.Issue
;
...
...
@@ -34,9 +35,11 @@ import org.jvnet.hudson.test.JenkinsRule;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Calendar
;
import
java.util.Collections
;
import
java.util.Date
;
import
java.util.Random
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
...
...
@@ -79,4 +82,61 @@ public class SecurityRealmTest {
public
void
generateImage
(
String
id
,
OutputStream
ios
)
throws
IOException
{
}
}
static
void
addSessionCookie
(
CookieManager
manager
,
String
domain
,
String
path
,
Date
date
)
{
manager
.
addCookie
(
new
Cookie
(
domain
,
"JSESSIONID."
+
Integer
.
toHexString
(
new
Random
().
nextInt
()),
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
,
path
,
date
,
false
));
}
@Test
public
void
many_sessions_logout
()
throws
Exception
{
final
String
WILL_NOT_BE_SENT
=
"/will-not-be-sent"
;
final
String
LOCALHOST
=
"localhost"
;
final
String
JSESSIONID
=
"JSESSIONID"
;
JenkinsRule
.
WebClient
wc
=
j
.
createWebClient
();
CookieManager
manager
=
wc
.
getCookieManager
();
manager
.
setCookiesEnabled
(
true
);
wc
.
goTo
(
"login"
);
Calendar
calendar
=
Calendar
.
getInstance
();
calendar
.
add
(
Calendar
.
DAY_OF_YEAR
,
1
);
Date
tomorrow
=
calendar
.
getTime
();
Collections
.
nCopies
(
8
,
1
)
.
stream
()
.
forEach
(
i
->
addSessionCookie
(
manager
,
LOCALHOST
,
"/jenkins"
,
tomorrow
));
addSessionCookie
(
manager
,
LOCALHOST
,
WILL_NOT_BE_SENT
,
tomorrow
);
HtmlPage
page
=
wc
.
goTo
(
"logout"
);
int
unexpectedSessionCookies
=
2
;
StringBuilder
builder
=
new
StringBuilder
();
builder
.
append
(
"Session cookies: "
);
for
(
Cookie
cookie
:
manager
.
getCookies
())
{
if
(
cookie
.
getName
().
startsWith
(
JSESSIONID
))
{
String
path
=
cookie
.
getPath
();
builder
.
append
(
cookie
.
getName
());
if
(
path
!=
null
)
builder
.
append
(
"; Path="
).
append
(
path
);
builder
.
append
(
"\n"
);
if
(
WILL_NOT_BE_SENT
.
equals
(
path
))
{
// Because it wasn't sent and thus wasn't deleted.
--
unexpectedSessionCookies
;
}
else
if
(
JSESSIONID
.
equals
(
cookie
.
getName
()))
{
// Because this test harness isn't winstone and the cleaning
// code is only responsible for deleting "JSESSIONID." cookies.
--
unexpectedSessionCookies
;
}
}
}
System
.
err
.
println
(
builder
.
toString
());
assertThat
(
unexpectedSessionCookies
,
is
(
0
));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录