Skip to content
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

feat: add user agent allowlist option #492

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ npm run start
| HOMEPAGE | undefined | Swarm hash that loads as the homepage of gateway-proxy |
| REMAP | undefined | Semicolon separated `name=hash` values to rewrite Swarm hashes to human-friendly names |
| ALLOWLIST | undefined | Comma separated list of hashes, ENS domains or CIDs to allow |
| ALLOW_USER_AGENTS | undefined | Comma separated list of user-agent substrings to give unlimited access to |

### Curl

Expand Down
6 changes: 6 additions & 0 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
cidSubdomains?: boolean
ensSubdomains?: boolean
remap: Record<string, string>
userAgents?: string[]
}

export function createProxyEndpoints(app: Application, options: Options) {
Expand Down Expand Up @@ -68,7 +69,7 @@
path: string,
query: Record<string, unknown>,
headers: IncomingHttpHeaders,
body: any,

Check warning on line 72 in src/proxy.ts

View workflow job for this annotation

GitHub Actions / check (14.x)

Unexpected any. Specify a different type
res: Response,
options: Options,
) {
Expand All @@ -92,13 +93,18 @@
})

if (options.allowlist) {
const allowed = (options.userAgents || [])
.filter(x => x)
.some(x => headers['user-agent']?.toLowerCase().includes(x.toLowerCase()))

const currentCid = Strings.searchSubstring(path, x => x.length > 48 && x.startsWith('bah'))
const currentHash = Strings.searchHex(path, 64)

const isBlockedHash = currentHash && !options.allowlist.includes(currentHash)
const isBlockedCid = currentCid && !options.allowlist.includes(currentCid)

if (
!allowed &&
(isBlockedHash || isBlockedCid) &&
(response.headers['content-disposition'] || '').toLowerCase().includes('.htm')
) {
Expand Down
5 changes: 4 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export const createApp = (
ensSubdomains,
hostname,
remap: Object.fromEntries(
(Arrays.getArgument(process.argv, 'remap', process.env as any, 'REMAP') || '').split(';').map(x => x.split('=')),
(Arrays.getArgument(process.argv, 'remap', process.env, 'REMAP') || '').split(';').map(x => x.split('=')),
),
userAgents: (Arrays.getArgument(process.argv, 'allow-user-agents', process.env, 'ALLOW_USER_AGENTS') || '').split(
',',
),
})

Expand Down
Loading