-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add regenerate method for session security (#3499)
- Loading branch information
1 parent
024a5c1
commit 74613d1
Showing
5 changed files
with
123 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/session/test/fixtures/change-session-key/src/configuration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Configuration, Controller, Get } from '@midwayjs/core'; | ||
import * as koa from '../../../../../web-koa'; | ||
import * as assert from 'assert'; | ||
|
||
@Configuration({ | ||
imports: [ | ||
koa, | ||
], | ||
importConfigs: [ | ||
{ | ||
default: { | ||
keys: '12345', | ||
session: { | ||
sameSite: 'none', | ||
} | ||
} | ||
} | ||
] | ||
}) | ||
export class AutoConfiguration { | ||
async onReady(container, app) { | ||
const message = 'hi'; | ||
app.useMiddleware(async (ctx, next) => { | ||
ctx.session = { message: 'hi' }; | ||
await next(); | ||
}); | ||
|
||
app.useMiddleware(async function(ctx, next) { | ||
const sessionKey = ctx.cookies.get('MW_SESS', { signed: false }); | ||
if (sessionKey) { | ||
await ctx.session.regenerate(); | ||
} | ||
await next(); | ||
}); | ||
|
||
app.useMiddleware(async function(ctx) { | ||
assert(ctx.session.message === message); | ||
ctx.body = ''; | ||
}); | ||
} | ||
} | ||
|
||
@Controller('/') | ||
export class HomeController { | ||
|
||
@Get('/get') | ||
async get(ctx) { | ||
return ctx.session; | ||
} | ||
@Get('/set') | ||
async set(ctx) { | ||
ctx.session = ctx.query; | ||
ctx.body = ctx.session; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters