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
6 changes: 2 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ROOT_DIR } from '@config/path.config';
import { gateMiddleware, initializeRuntime, shutdown, startHeartbeat } from '@licensing/runtime';
import { setDB } from '@licensing/store';
import * as Sentry from '@sentry/node';
import { isCorsOriginAllowed } from '@utils/corsOrigin';
import { ServerUP } from '@utils/server-up';
import axios from 'axios';
import compression from 'compression';
Expand Down Expand Up @@ -62,10 +63,7 @@ async function bootstrap() {
cors({
origin(requestOrigin, callback) {
const { ORIGIN } = configService.get<Cors>('CORS');
if (ORIGIN.includes('*')) {
return callback(null, true);
}
if (ORIGIN.indexOf(requestOrigin) !== -1) {
if (isCorsOriginAllowed(ORIGIN, requestOrigin)) {
return callback(null, true);
}
return callback(new Error('Not allowed by CORS'));
Expand Down
9 changes: 9 additions & 0 deletions src/utils/corsOrigin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Whether a CORS request may proceed. CORS governs browsers only: a request without an Origin header comes from a
* server, a bot or a health check, never from a cross-site page, so it is always allowed.
*/
export const isCorsOriginAllowed = (allowed: string[], requestOrigin: string | undefined): boolean => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Low · Utility misses required export

isCorsOriginAllowed is exported only by name and has no default export, so the new utility does not follow the repository’s required utility pattern. The file also omits that pattern’s prescribed logger and failure handling.

Add the required default export and follow the utility pattern’s logger and failure-handling requirements.

From .cursor/rules/specialized-rules/util-rules.mdc L12-29:

### Standard Utility Pattern
```typescript
import { Logger } from '@config/logger.config';

const logger = new Logger('UtilityName');

export function utilityFunction(param: ParamType): ReturnType {
  try {
    // Utility logic
    return result;
  } catch (error) {
    logger.error(`Utility function failed: ${error.message}`);
    throw error;
  }
}…

if (!requestOrigin) return true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Cross-site requests alter license state

When a cross-site browser request omits Origin on /license/activate or /license/register, isCorsOriginAllowed returns true, so the CORS middleware passes the request to the unauthenticated licensing routes. /license/register lets initRegistration replace pending registration state using an attacker-chosen redirect_uri; /license/activate lets completeActivation save a supplied code’s API key and activate the instance, replacing pending license state when the instance is inactive and the code is valid.

Do not treat a missing Origin as trusted for these licensing routes; reject it or require an explicitly allowed origin.

if (allowed.includes('*')) return true;
return allowed.includes(requestOrigin);
};