-
Notifications
You must be signed in to change notification settings - Fork 8
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
fix(server): 🩹 Using sessionID as a fallback to requests where referer is missing. #20
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,23 +59,36 @@ export class HeliaServer { | |
] | ||
} | ||
|
||
/** | ||
* Computes referer path for the request. | ||
*/ | ||
private fetchRefererPath ({ request }: IRouteHandler): string { | ||
let refererPath = new URL(request.headers.referer ?? 'http://ipfs.io').pathname | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defaulting to 'ipfs.io' here seems wrong. why not default to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not currentUrl (because pathname) but |
||
if (refererPath === '/') { | ||
refererPath = request.sessionID | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we use sessionId in scenarios other than when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. referrer should be more reliable, in most cases, this is just a fallback. |
||
} | ||
|
||
if (refererPath !== undefined) { | ||
return refererPath | ||
} | ||
|
||
throw new Error('Error calculating referer') | ||
} | ||
|
||
/** | ||
* Handles redirecting to the relative path | ||
*/ | ||
private async redirectRelative ({ request, response }: IRouteHandler): Promise<void> { | ||
try { | ||
const referrerPath = new URL(request.headers.referer ?? '').pathname | ||
if (referrerPath !== undefined) { | ||
this.log('Referer found:', referrerPath) | ||
let relativeRedirectPath = `${referrerPath}${request.path}` | ||
const { namespace, address } = this.heliaFetch.parsePath(referrerPath) | ||
if (namespace === 'ipns') { | ||
relativeRedirectPath = `/${namespace}/${address}${request.path}` | ||
} | ||
// absolute redirect | ||
this.log('Redirecting to relative to referer:', referrerPath) | ||
response.redirect(relativeRedirectPath) | ||
const refererPath = this.fetchRefererPath({ request, response }) | ||
let relativeRedirectPath = `${refererPath}${request.path}` | ||
const { namespace, address } = this.heliaFetch.parsePath(refererPath) | ||
if (namespace === 'ipns') { | ||
relativeRedirectPath = `/${namespace}/${address}${request.path}` | ||
} | ||
// absolute redirect | ||
this.log('Redirecting to relative to referer:', refererPath) | ||
response.redirect(relativeRedirectPath) | ||
} catch (error) { | ||
this.log('Error redirecting to relative path:', error) | ||
response.status(500).end() | ||
|
@@ -113,9 +126,8 @@ export class HeliaServer { | |
address: reqDomain | ||
} = this.heliaFetch.parsePath(request.path) | ||
|
||
if (request.headers.referer !== undefined) { | ||
this.log('Referer found:', request.headers.referer) | ||
const refererPath = new URL(request.headers.referer).pathname | ||
try { | ||
const refererPath = this.fetchRefererPath({ request, response }) | ||
const { | ||
namespace: refNamespace, | ||
address: refDomain | ||
|
@@ -131,6 +143,8 @@ export class HeliaServer { | |
return true | ||
} | ||
} | ||
} catch (error) { | ||
this.log('Error checking for additional redirection:', error) | ||
} | ||
return false | ||
} | ||
|
@@ -190,4 +204,14 @@ export class HeliaServer { | |
await this.heliaFetch.node?.gc() | ||
response.status(200).end() | ||
} | ||
|
||
/** | ||
* Generates a session ID for the request. | ||
* This is a very ghetto way of identifying what the root ipns path for a request is. | ||
* Overloading the sessionID the first request allows us to use the same session for all subsequent requests. Mostly! | ||
* In many cases it won't work, but the browser won't care for those either. | ||
*/ | ||
public sessionId (request: Request): string { | ||
return request.sessionID ?? request.path | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
import debug from 'debug' | ||
import express from 'express' | ||
import promBundle from 'express-prom-bundle' | ||
import session from 'express-session' | ||
import { HeliaServer, type IRouteEntry } from './heliaServer.js' | ||
|
||
const logger = debug('helia-server') | ||
const app = express() | ||
const promMetricsMiddleware = promBundle({ includeMethod: true }) | ||
|
||
const heliaServer = new HeliaServer(logger) | ||
await heliaServer.isReady | ||
|
||
// Constants | ||
const PORT = (process?.env?.PORT ?? 8080) as number | ||
const HOST = process?.env?.HOST ?? '0.0.0.0' | ||
|
||
// Add the prometheus middleware | ||
const app = express() | ||
app.use(promMetricsMiddleware) | ||
|
||
const heliaServer = new HeliaServer(logger) | ||
await heliaServer.isReady | ||
app.use(session({ | ||
genid: heliaServer.sessionId, | ||
secret: 'very secret value' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: too secret of a value. please remove from commit history. |
||
})) | ||
|
||
// Add the routes | ||
app.get('/', (req, res) => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should use the term
fetch
here.Also, (joke) just because HTTP spec spells referrer wrong doesn't mean we need to? =P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was driving me crazy, sorry tired of seeing two different spellings.