Skip to content
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
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

strategy:
matrix:
node-version: [18.x, 20.x]
node-version: [24.x, 26.x]
fail-fast: false

steps:
Expand Down Expand Up @@ -86,7 +86,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v7
with:
node-version: '20.x'
node-version: '24.x'

- name: Check JavaScript syntax
run: |
Expand Down Expand Up @@ -222,7 +222,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v7
with:
node-version: '20.x'
node-version: '24.x'

- name: Install test dependencies
run: |
Expand Down Expand Up @@ -259,7 +259,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v7
with:
node-version: '20.x'
node-version: '24.x'

- name: Install schema validation tools
run: |
Expand Down Expand Up @@ -288,7 +288,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v7
with:
node-version: '20.x'
node-version: '24.x'

- name: Install dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion schema/API_SCHEMA.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ No authentication is required for public endpoints.
### Rate Limiting

The API includes caching headers to optimize performance:
- Application data is cached for 5 minutes (`max-age=300`)
- Application data is cached for 12 hours (`max-age=43200`)
- Use conditional requests with caching headers when possible

### Example Usage
Expand Down
10 changes: 5 additions & 5 deletions schema/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ paths:
Examples: MicrosoftEdge, MozillaFirefox, GoogleChrome, etc.
schema:
type: string
pattern: '^[A-Za-z0-9-_]{1,64}$'
pattern: '^[A-Za-z0-9-_.]{1,64}$'
example: MicrosoftEdge
responses:
'200':
Expand All @@ -81,7 +81,7 @@ paths:
description: Caching policy for the response
schema:
type: string
example: "public, max-age=300"
example: "public, max-age=43200"
content:
application/json:
schema:
Expand Down Expand Up @@ -111,7 +111,7 @@ paths:
description: Caching policy for the response
schema:
type: string
example: "public, max-age=300"
example: "public, max-age=43200"
content:
application/json:
schema:
Expand Down Expand Up @@ -178,7 +178,7 @@ paths:
description: Caching policy for the response
schema:
type: string
example: "public, max-age=300"
example: "public, max-age=43200"
content:
application/json:
schema:
Expand Down Expand Up @@ -210,7 +210,7 @@ paths:
description: Caching policy for the response
schema:
type: string
example: "public, max-age=300"
example: "public, max-age=43200"
content:
application/json:
schema:
Expand Down
110 changes: 98 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,67 @@ function validateAppId(appId) {
return /^[A-Za-z0-9-_.]{1,64}$/.test(appId)
}

function validateUserAgent(userAgent) {
if (!userAgent || typeof userAgent !== 'string') return false

const trimmedUserAgent = userAgent.trim()
if (trimmedUserAgent.length < 3 || trimmedUserAgent.length > 100) return false

if (!/^[A-Za-z0-9][A-Za-z0-9\-._/\s]*[A-Za-z0-9]$/.test(trimmedUserAgent)) {
return false
}

if (!trimmedUserAgent.includes('/')) {
return false
}

const normalized = trimmedUserAgent.toLowerCase()
const blockedPrefixes = [
'curl/', 'wget/', 'superagent/', 'node-superagent/', 'axios/', 'fetch/',
'mozilla/', 'chrome/', 'firefox/', 'safari/', 'edge/', 'msie/',
'postman/', 'insomnia/', 'python-requests/', 'httpie/', 'okhttp/',
'go-http-client/', 'java/', 'apache-httpclient/'
]

if (blockedPrefixes.some(prefix => normalized.startsWith(prefix))) {
return false
}

return true
}

function getRequestUserAgent(req) {
const headers = req && (req.headers || (req.request && req.request.headers))

if (!headers) {
return ''
}

if (typeof headers.get === 'function') {
return headers.get('User-Agent') || headers.get('user-agent') || ''
}

if (typeof headers === 'object') {
if (Array.isArray(headers['User-Agent'])) {
return headers['User-Agent'][0] || ''
}
if (Array.isArray(headers['user-agent'])) {
return headers['user-agent'][0] || ''
}

return headers['User-Agent'] || headers['user-agent'] || ''
}

return ''
}

function userAgentRequiredResponse() {
return jsonResponse({
message: 'A valid User-Agent header is required. Please include a custom User-Agent in the format "company/location" or similar descriptive pattern.',
documentation: 'https://eucpilots.com/evergreen/api/'
}, 400)
}

function ensureEvergreenBinding() {
console.log('Checking EVERGREEN binding...', typeof EVERGREEN)
if (typeof EVERGREEN === 'undefined') {
Expand Down Expand Up @@ -169,6 +230,10 @@ async function storeLogToR2(request, startTime) {

// Handle /app endpoint without application name
app.get("/app", async (req, res) => {
if (!validateUserAgent(getRequestUserAgent(req))) {
return userAgentRequiredResponse()
}

return jsonResponse({
message: 'Application name is required. Please specify a valid application name in the URL (e.g., /app/MicrosoftEdge). Call /apps for a list of available applications.',
documentation: 'https://eucpilots.com/evergreen/api/'
Expand All @@ -177,6 +242,10 @@ app.get("/app", async (req, res) => {

// Returns data for a specific app
app.get("/app/:appId", async (req, res) => {
if (!validateUserAgent(getRequestUserAgent(req))) {
return userAgentRequiredResponse()
}

if (!ensureEvergreenBinding()) {
return jsonResponse({ message: 'Server configuration error' }, 500)
}
Expand Down Expand Up @@ -212,14 +281,17 @@ app.get("/app/:appId", async (req, res) => {
console.error('Error fetching app data:', err)
console.error('Error stack:', err.stack)
return jsonResponse({
message: 'Internal server error',
error: err.message
message: 'Internal server error'
}, 500, { 'X-Cache-Status': 'ERROR' })
}
});

// Returns data for all supported apps
app.get("/apps", async (req, res) => {
if (!validateUserAgent(getRequestUserAgent(req))) {
return userAgentRequiredResponse()
}

if (!ensureEvergreenBinding()) {
return jsonResponse({ message: 'Server configuration error' }, 500)
}
Expand All @@ -242,14 +314,15 @@ app.get("/apps", async (req, res) => {
console.error('Error fetching apps list:', err)
console.error('Error stack:', err.stack)
return jsonResponse({
message: 'Internal server error',
error: err.message
message: 'Internal server error'
}, 500, { 'X-Cache-Status': 'ERROR' })
}
});

// Return a message if someone calls /endpoints
app.get('/endpoints', async (req, res) => {
app.get('/endpoints', async (req, res) => { if (!validateUserAgent(getRequestUserAgent(req))) {
return userAgentRequiredResponse()
}
console.log("GET /endpoints called");
return jsonResponse({
message: 'Method not found. Supported endpoint calls are /endpoints/versions and /endpoints/downloads.',
Expand All @@ -259,6 +332,10 @@ app.get('/endpoints', async (req, res) => {

// Returns endpoints data for URLs used by Evergreen when finding application versions
app.get("/endpoints/versions", async (req, res) => {
if (!validateUserAgent(getRequestUserAgent(req))) {
return userAgentRequiredResponse()
}

if (!ensureEvergreenBinding()) {
return jsonResponse({ message: 'Server configuration error' }, 500)
}
Expand All @@ -285,6 +362,10 @@ app.get("/endpoints/versions", async (req, res) => {

// Returns endpoints data for URLs used by Evergreen to download application installers with Save-EvergreenApp
app.get("/endpoints/downloads", async (req, res) => {
if (!validateUserAgent(getRequestUserAgent(req))) {
return userAgentRequiredResponse()
}

if (!ensureEvergreenBinding()) {
return jsonResponse({ message: 'Server configuration error' }, 500)
}
Expand All @@ -311,6 +392,10 @@ app.get("/endpoints/downloads", async (req, res) => {

// Health check endpoint with cache diagnostics
app.get("/health", async (req, res) => {
if (!validateUserAgent(getRequestUserAgent(req))) {
return userAgentRequiredResponse()
}

try {
const health = {
status: 'ok',
Expand Down Expand Up @@ -386,21 +471,22 @@ app.get("/health", async (req, res) => {
console.error('Health check error:', error)
return jsonResponse({
status: 'error',
message: 'Health check failed',
error: error.message
message: 'Health check failed'
}, 500)
}
})

// Root endpoint
app.get('/', async (req, res) => {
if (!validateUserAgent(getRequestUserAgent(req))) {
return userAgentRequiredResponse()
}

console.log(`Root endpoint called!`);
return jsonResponse({
message: 'Evergreen API with hybrid caching',
documentation: 'https://eucpilots.com/evergreen/api/',
endpoints: ['/apps', '/app/{appId}', '/endpoints/versions', '/endpoints/downloads', '/health'],
caching: '2-tier: Memory + KV (12h TTL)'
})
message: 'Endpoint not found. Use /apps, /app/{appId}, /endpoints/versions, /endpoints/downloads, or /health.',
documentation: 'https://eucpilots.com/evergreen/api/'
}, 404)
});

// Event listener with R2 logging
Expand Down
Loading
Loading