Skip to content
Open
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
3 changes: 3 additions & 0 deletions apps/oauth-redirect-proxy/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
20 changes: 11 additions & 9 deletions apps/oauth-redirect-proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# Use the official Node.js 22 image as the base image
FROM node:22.23.4-alpine

# Set the working directory inside the container
# Set the working directory inside the container and hand it to the
# unprivileged "node" user that ships with the image
WORKDIR /app
RUN chown node:node /app
USER node

# Copy package.json and package-lock.json (if available)
COPY package*.json ./

# Install dependencies
RUN npm install --omit=dev
# Install the exact dependency tree from the lockfile (dev dependencies
# included: the build needs TypeScript)
COPY --chown=node:node package.json package-lock.json ./
RUN npm ci

# Copy the rest of the application code
COPY . .
COPY --chown=node:node . .

# Build the Next.js application
RUN npm run build
# Build the Next.js application, then drop dev dependencies
RUN npm run build && npm prune --omit=dev

# Expose the port the app runs on
EXPOSE 3000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,36 @@ import { NextRequest, NextResponse } from 'next/server';
// Force dynamic rendering for this route
export const dynamic = 'force-dynamic';

type RouteContext = { params: Promise<{ provider: string }> };

// An OAuth form_post callback is a handful of short fields; anything larger is refused
// before it is buffered.
const MAX_BODY_BYTES = 64 * 1024;

async function readBodyWithinLimit(request: NextRequest): Promise<string | null> {
if (!request.body) return '';
const reader = request.body.getReader();
const chunks: Uint8Array[] = [];
let received = 0;
for (;;) {
const { done, value } = await reader.read();
if (done) break;
received += value.byteLength;
if (received > MAX_BODY_BYTES) {
await reader.cancel();
return null;
}
chunks.push(value);
}
return Buffer.concat(chunks).toString('utf8');
}

async function handleOAuthCallback(
request: NextRequest,
{ params }: { params: { provider: string } }
{ params }: RouteContext
) {
try {
const { provider } = params;
const { provider } = await params;
const method = request.method;

// Security: Encode provider to prevent path injection
Expand All @@ -20,29 +44,22 @@ async function handleOAuthCallback(
// Get request body if it exists (for POST requests)
let body: string | null = null;
if (method === 'POST') {
try {
body = await request.text();
} catch (error) {
const received = await readBodyWithinLimit(request).catch((error) => {
console.warn('Could not read request body:', error);
return '';
});
if (received === null) {
return new NextResponse('Payload too large', { status: 413 });
}
body = received;
}

// Security: Create sanitized log data (redact sensitive parameters)
const sanitizedParams: Record<string, string> = {};
searchParams.forEach((value, key) => {
// Redact sensitive OAuth parameters
if (key === 'code' || key === 'state' || key === 'access_token' || key === 'refresh_token') {
sanitizedParams[key] = '[REDACTED]';
} else {
sanitizedParams[key] = value;
}
});

// Log the incoming request for debugging (sanitized)
// Log parameter names only: providers return codes and tokens under many names
// (code, id_token, JARM "response"), so no value is safe to log.
console.log('OAuth redirect received:', {
provider: encodedProvider,
method,
searchParams: sanitizedParams,
searchParamKeys: Array.from(searchParams.keys()),
bodyLength: body ? body.length : 0,
userAgent: request.headers.get('user-agent')?.substring(0, 100) || 'unknown',
timestamp: new Date().toISOString()
Expand Down Expand Up @@ -71,13 +88,14 @@ async function handleOAuthCallback(
request.headers.forEach((value, key) => {
const lowerKey = key.toLowerCase();

// Skip hop-by-hop headers that should not be forwarded
const hopByHopHeaders = [
// Hop-by-hop headers, and this domain's own credentials, which StackOne must not get
const skippedHeaders = [
'host', 'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization',
'te', 'trailers', 'transfer-encoding', 'upgrade', 'content-length', 'content-encoding'
'te', 'trailers', 'transfer-encoding', 'upgrade', 'content-length', 'content-encoding',
'cookie', 'authorization'
];

if (!hopByHopHeaders.includes(lowerKey)) {
if (!skippedHeaders.includes(lowerKey)) {
forwardHeaders[key] = value;
}
});
Expand Down Expand Up @@ -143,14 +161,14 @@ async function handleOAuthCallback(

export async function GET(
request: NextRequest,
context: { params: { provider: string } }
context: RouteContext
) {
return handleOAuthCallback(request, context);
}

export async function POST(
request: NextRequest,
context: { params: { provider: string } }
context: RouteContext
) {
return handleOAuthCallback(request, context);
}
3 changes: 2 additions & 1 deletion apps/oauth-redirect-proxy/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference path="./.next/types/routes.d.ts" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
Loading