Workspace Locked
This Space is protected. Enter the access password in the browser prompt to continue.
Nothing inside the workspace will load until the correct password is entered.
import http from 'node:http' import https from 'node:https' import { createHmac, timingSafeEqual } from 'node:crypto' const publicPort = Number(process.env.PORT || '7860') const upstreamUrl = new URL( process.env.WORKSPACE_UPSTREAM_URL || `http://127.0.0.1:${process.env.WORKSPACE_INTERNAL_PORT || '7861'}`, ) const accessPassword = process.env.SPACE_ACCESS_PASSWORD || '' const sessionSecret = process.env.SPACE_SESSION_SECRET || accessPassword const cookieName = 'hermes-space-access' const loginPath = '/__space_auth/login' const logoutPath = '/__space_auth/logout' const healthPath = '/__space_auth/health' const authorizedToken = signToken('authorized') if (!accessPassword) { console.error( '[space-access] Missing SPACE_ACCESS_PASSWORD. Configure it as a Hugging Face Space secret.', ) process.exit(1) } function signToken(scope) { return createHmac('sha256', sessionSecret) .update(`${scope}:${accessPassword}`) .digest('base64url') } function safeEqual(left, right) { const leftBuffer = Buffer.from(left) const rightBuffer = Buffer.from(right) if (leftBuffer.length !== rightBuffer.length) { return false } return timingSafeEqual(leftBuffer, rightBuffer) } function parseCookies(cookieHeader = '') { const cookies = {} for (const chunk of cookieHeader.split(';')) { const trimmed = chunk.trim() if (!trimmed) continue const separatorIndex = trimmed.indexOf('=') if (separatorIndex === -1) continue const key = trimmed.slice(0, separatorIndex) const value = trimmed.slice(separatorIndex + 1) cookies[key] = value } return cookies } function isAuthorized(request) { const cookies = parseCookies(request.headers.cookie) const token = cookies[cookieName] if (!token) return false return safeEqual(token, authorizedToken) } function wantsHtml(request) { const accept = request.headers.accept || '' return accept.includes('text/html') } function readJsonBody(request) { return new Promise((resolve, reject) => { const chunks = [] let size = 0 request.on('data', (chunk) => { size += chunk.length if (size > 16 * 1024) { reject(new Error('Body too large')) request.destroy() return } chunks.push(chunk) }) request.on('end', () => { try { const raw = Buffer.concat(chunks).toString('utf8') resolve(raw ? JSON.parse(raw) : {}) } catch (error) { reject(error) } }) request.on('error', reject) }) } function sendJson(response, statusCode, payload, extraHeaders = {}) { response.writeHead(statusCode, { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store', ...extraHeaders, }) response.end(JSON.stringify(payload)) } function sendPromptPage(response) { const html = `
This Space is protected. Enter the access password in the browser prompt to continue.
Nothing inside the workspace will load until the correct password is entered.