-
-
Notifications
You must be signed in to change notification settings - Fork 233
fix: replace deprecated csurf and native-url packages #4505
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
Changes from all commits
62ffbd3
e24135a
c7a9a20
06a0954
058b87b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,6 @@ import { storeDir } from '../config/app.ts' | |
| import { ensureDir } from './utils.ts' | ||
| import { Manager } from 'mqtt-jsonl-store' | ||
| import { join } from 'node:path' | ||
| import url from 'native-url' | ||
|
|
||
| const logger = module('Mqtt') | ||
|
|
||
|
|
@@ -351,10 +350,21 @@ class MqttClient extends TypedEventEmitter<MqttClientEventCallbacks> { | |
| MqttClient.NAME_PREFIX + (process.env.MQTT_NAME || config.name), | ||
| ) | ||
|
|
||
| const parsed = url.parse(config.host || '') | ||
| let parsed: URL | null = null | ||
|
||
| let protocol = 'mqtt' | ||
| let hostname = config.host | ||
|
|
||
| if (parsed.protocol) protocol = parsed.protocol.replace(/:$/, '') | ||
| // Try to parse as URL if it contains a protocol | ||
| try { | ||
| parsed = new URL(config.host || '') | ||
| if (parsed.protocol) { | ||
| protocol = parsed.protocol.replace(/:$/, '') | ||
| hostname = parsed.hostname | ||
| } | ||
| } catch { | ||
| // If parsing fails, treat as hostname without protocol | ||
| hostname = config.host | ||
| } | ||
|
|
||
| const options: IClientOptions = { | ||
| clientId: this._clientID, | ||
|
|
@@ -397,9 +407,7 @@ class MqttClient extends TypedEventEmitter<MqttClientEventCallbacks> { | |
| } | ||
|
|
||
| try { | ||
| const serverUrl = `${protocol}://${ | ||
| parsed.hostname || config.host | ||
| }:${config.port}` | ||
| const serverUrl = `${protocol}://${hostname || config.host}:${config.port}` | ||
| logger.info(`Connecting to ${serverUrl}`) | ||
|
|
||
| const client = connect(serverUrl, options) | ||
|
|
||
Check failure
Code scanning / CodeQL
Missing CSRF middleware High
Copilot Autofix
AI about 2 months ago
In general, to fix this issue you should ensure that any state‑changing route that relies on cookie‑based authentication is protected by CSRF middleware. This typically means adding a CSRF protection middleware globally for sensitive routes or per‑route for each
POST,PUT, andDELETEendpoint that uses session cookies. In this codebase,doubleCsrffromcsrf-csrfis already imported, and some routes (/api/authenticate,/api/password) usedoubleCsrfProtection, so the best fix is to consistently apply this existing middleware to the remaining state‑changing, authenticated routes.The single best fix with minimal functional impact is:
cookieParserand body parsers as they are.doubleCsrfProtectionmiddleware instance (assumed to be defined earlier inapi/app.tssince it’s imported and already used).doubleCsrfProtectioninto the middleware chain for all authenticated, state‑changing routes that currently lack CSRF protection. That includes the following routes inapi/app.ts:POST /api/restartPOST /api/statisticsPOST /api/versionsPOST /api/importConfigPUT /api/storeDELETE /api/storePUT /api/store-multiPOST /api/store-multiPOST /api/store/uploadPOST /api/debug/startPOST /api/debug/stopPOST /api/debug/canceldoubleCsrfProtectionruns beforeisAuthenticated(or at least before the route handler). In the existing code,doubleCsrfProtectioncomes beforeisAuthenticatedon/api/password, so we should follow the same pattern for consistency.No new imports or helper methods are required; we just add
doubleCsrfProtectionas an extra middleware argument on the listed routes.