Skip to content

Commit

Permalink
Respect UDP packet maximum size (#551)
Browse files Browse the repository at this point in the history
and do not process bigger messages
  • Loading branch information
Apollon77 authored Dec 1, 2023
1 parent 8b0b777 commit 8054e34
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/matter-node.js/src/net/UdpChannelNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

import { Logger } from "@project-chip/matter.js/log";
import { NetworkError, UdpChannel, UdpChannelOptions } from "@project-chip/matter.js/net";
import { MatterCoreSpecificationV1_2 } from "@project-chip/matter.js/spec";
import { ByteArray } from "@project-chip/matter.js/util";

import * as dgram from "dgram";
import { NetworkNode } from "./NetworkNode.js";

/** @see {@link MatterCoreSpecificationV1_2} § 4.4.4 */
const MAX_UDP_PAYLOAD_SIZE = 1280;

const logger = Logger.get("UdpChannelNode");

function createDgramSocket(host: string | undefined, port: number | undefined, options: dgram.SocketOptions) {
Expand Down Expand Up @@ -102,6 +105,12 @@ export class UdpChannelNode implements UdpChannel {

onData(listener: (netInterface: string, peerAddress: string, peerPort: number, data: ByteArray) => void) {
const messageListener = (data: ByteArray, { address, port }: dgram.RemoteInfo) => {
if (data.length > MAX_UDP_PAYLOAD_SIZE) {
logger.warn(
`Ignoring UDP message with size ${data.length} from ${address}:${port}, which is larger than the maximum allowed size of ${MAX_UDP_PAYLOAD_SIZE}.`,
);
return;
}
const netInterface = this.netInterface ?? NetworkNode.getNetInterfaceForIp(address);
if (netInterface === undefined) return;
listener(netInterface, address, port, data);
Expand All @@ -117,6 +126,14 @@ export class UdpChannelNode implements UdpChannel {

async send(host: string, port: number, data: ByteArray) {
return new Promise<void>((resolve, reject) => {
if (data.length > MAX_UDP_PAYLOAD_SIZE) {
reject(
new NetworkError(
`Cannot send UDP message with size ${data.length}, which is larger than the maximum allowed size of ${MAX_UDP_PAYLOAD_SIZE}.`,
),
);
return;
}
this.socket.send(data, port, host, error => {
if (error !== null) {
reject(error);
Expand Down
9 changes: 9 additions & 0 deletions packages/matter.js/src/spec/Specifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ export interface MatterApplicationClusterSpecificationV1_1 {}

/** {@link https://csa-iot.org/developer-resource/specifications-download-request/ Matter Device Library Specification 1.1} */
export interface MatterDeviceLibrarySpecificationV1_1 {}

/** {@link https://csa-iot.org/developer-resource/specifications-download-request/ Matter Core Specification 1.2} */
export interface MatterCoreSpecificationV1_2 {}

/** {@link https://csa-iot.org/developer-resource/specifications-download-request/ Matter Application Cluster Specification 1.2} */
export interface MatterApplicationClusterSpecificationV1_2 {}

/** {@link https://csa-iot.org/developer-resource/specifications-download-request/ Matter Device Library Specification 1.2} */
export interface MatterDeviceLibrarySpecificationV1_2 {}

0 comments on commit 8054e34

Please sign in to comment.