-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sandbox): implement TX sandboxing PE-3974
Implements the arweave.net feature that prepends a base32 encoded ID to the hostname when serving TXs. The feature is enabled if ARNS_ROOT_HOST is set. The protocol (https or http) in the redirect URLs can be overriden using SANDBOX_PROTOCOL.
- Loading branch information
Showing
6 changed files
with
90 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* AR.IO Gateway | ||
* Copyright (C) 2022 - 2023 Permanent Data Solutions, Inc | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
import { Handler, Request } from 'express'; | ||
import url from 'node:url'; | ||
import { base32 } from 'rfc4648'; | ||
|
||
import { fromB64Url } from '../lib/encoding.js'; | ||
|
||
function getRequestSandbox(req: Request): string | undefined { | ||
if (req.subdomains.length === 1) { | ||
return req.subdomains[0]; | ||
} | ||
return undefined; | ||
} | ||
|
||
function getRequestId(req: Request): string | undefined { | ||
return (req.path.match(/^\/([a-zA-Z0-9-_]{43})/) ?? [])[1]; | ||
} | ||
|
||
function sandboxFromId(id: string): string { | ||
return base32.stringify(fromB64Url(id), { pad: false }).toLowerCase(); | ||
} | ||
|
||
export function createSandboxMiddleware({ | ||
rootHost, | ||
sandboxProtocol, | ||
}: { | ||
rootHost?: string; | ||
sandboxProtocol?: string; | ||
}): Handler { | ||
return (req, res, next) => { | ||
if (rootHost === undefined) { | ||
next(); | ||
return; | ||
} | ||
|
||
const id = getRequestId(req); | ||
if (id === undefined) { | ||
next(); | ||
return; | ||
} | ||
|
||
const reqSandbox = getRequestSandbox(req); | ||
const idSandbox = sandboxFromId(id); | ||
if (reqSandbox !== idSandbox) { | ||
const queryString = url.parse(req.originalUrl).query ?? ''; | ||
const path = req.path.replace(/\/\//, '/'); | ||
const protocol = sandboxProtocol ?? (req.secure ? 'https' : 'http'); | ||
return res.redirect( | ||
302, | ||
`${protocol}://${idSandbox}.${rootHost}${path}?${queryString}`, | ||
); | ||
} | ||
|
||
next(); | ||
}; | ||
} |
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