Skip to content

Commit 136306c

Browse files
committed
fix: use base uri from the referrer header and not query string
1 parent f093b03 commit 136306c

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

npm-audit.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ <h5 class="card-title">
5555
<div class="card">
5656
<div class="card-body">
5757
<h5 class="card-title">
58-
July 23rd 2020, 5:46:38 am
58+
July 23rd 2020, 7:09:43 am
5959
</h5>
6060
<p class="card-text">Last updated</p>
6161
</div>

src/Redirect/index.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,23 @@ import { IncomingMessage } from 'http'
1717
import { RedirectContract, ResponseContract } from '@ioc:Adonis/Core/Response'
1818
import { RouterContract, MakeUrlOptions } from '@ioc:Adonis/Core/Route'
1919

20+
/**
21+
* Exposes the API to construct redirect routes
22+
*/
2023
export class Redirect implements RedirectContract {
24+
/**
25+
* A boolean to forward the existing query string
26+
*/
2127
private forwardQueryString = false
28+
29+
/**
30+
* The status code for the redirect
31+
*/
2232
private statusCode = 302
33+
34+
/**
35+
* A custom query string to forward
36+
*/
2337
private queryString: { [key: string]: any } = {}
2438

2539
constructor(
@@ -61,32 +75,34 @@ export class Redirect implements RedirectContract {
6175
* Redirect to the previous path.
6276
*/
6377
public back() {
64-
const url = (this.request.headers['referer'] ||
65-
this.request.headers['referrer'] ||
66-
'/') as string
78+
let url = this.request.headers['referer'] || this.request.headers['referrer'] || '/'
79+
url = Array.isArray(url) ? url[0] : url
6780

68-
return this.toPath(url)
81+
/**
82+
* Remove query string from the referrer
83+
*/
84+
return this.toPath(url.split('?')[0])
6985
}
7086

7187
/**
7288
* Redirect the request using a route identifier.
7389
*/
7490
public toRoute(routeIdentifier: string, urlOptions?: MakeUrlOptions, domain?: string) {
75-
const route = this.router.lookup(routeIdentifier, domain)
91+
// const route = this.router.lookup(routeIdentifier, domain)
7692

77-
if (!route) {
93+
const url = this.router.makeUrl(routeIdentifier, urlOptions, domain)
94+
if (!url) {
7895
throw new Error(`Unable to lookup route for "${routeIdentifier}" identifier`)
7996
}
8097

81-
const url = this.router.makeUrl(routeIdentifier, urlOptions, domain) as string
8298
return this.toPath(url)
8399
}
84100

85101
/**
86102
* Redirect the request using a path.
87103
*/
88104
public toPath(url: string) {
89-
let query
105+
let query: any
90106

91107
// Extract the current QueryString if we want to forward it.
92108
if (this.forwardQueryString) {

test/redirect.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,19 @@ test.group('Redirect', () => {
132132
assert.equal(header.location, '/foo')
133133
})
134134

135-
test('redirect back to referer', async (assert) => {
135+
test('redirect back to referrer when query string', async (assert) => {
136136
const server = createServer((req, res) => {
137137
const response = new Response(req, res, encryption, responseConfig, router)
138-
response.redirect('back')
138+
response.redirect().withQs({ name: 'virk' }).back()
139139
response.finish()
140140
})
141141

142-
const { header } = await supertest(server).get('/').set('referer', '/foo').redirects(1)
143-
assert.equal(header.location, '/foo')
142+
const { header } = await supertest(server)
143+
.get('/')
144+
.set('referer', '/foo?name=virk')
145+
.redirects(1)
146+
147+
assert.equal(header.location, '/foo?name=virk')
144148
})
145149

146150
test('redirect back to root (/) when referrer header is not set', async (assert) => {

0 commit comments

Comments
 (0)