@@ -38,6 +38,7 @@ const {
3838 ErrnoException,
3939 ExceptionWithHostPort,
4040 codes : {
41+ ERR_ACCESS_DENIED ,
4142 ERR_BUFFER_OUT_OF_BOUNDS ,
4243 ERR_INVALID_ARG_TYPE ,
4344 ERR_INVALID_FD_TYPE ,
@@ -81,6 +82,7 @@ const {
8182
8283const dc = require ( 'diagnostics_channel' ) ;
8384const udpSocketChannel = dc . channel ( 'udp.socket' ) ;
85+ const permission = require ( 'internal/process/permission' ) ;
8486
8587const BIND_STATE_UNBOUND = 0 ;
8688const BIND_STATE_BINDING = 1 ;
@@ -327,12 +329,9 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
327329 else
328330 address = '::' ;
329331 }
330-
331- // Resolve address first
332- state . handle . lookup ( address , ( err , ip ) => {
332+ const afterDns = ( err , ip ) => {
333333 if ( ! state . handle )
334334 return ; // Handle has been closed in the mean time
335-
336335 if ( err ) {
337336 state . bindState = BIND_STATE_UNBOUND ;
338337 this . emit ( 'error' , err ) ;
@@ -372,7 +371,22 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) {
372371
373372 startListening ( this ) ;
374373 }
375- } ) ;
374+ } ;
375+ if ( permission . isEnabled ( ) ) {
376+ const resource = `${ address } /${ port || '*' } ` ;
377+ if ( ! permission . has ( 'net.udp' , resource ) ) {
378+ process . nextTick ( ( ) => {
379+ afterDns ( new ERR_ACCESS_DENIED (
380+ `bind to ${ resource } ` ,
381+ resource ,
382+ 'NetUDP'
383+ ) ) ;
384+ } ) ;
385+ return this ;
386+ }
387+ }
388+ // Resolve address first
389+ state . handle . lookup ( address , afterDns ) ;
376390
377391 return this ;
378392} ;
@@ -413,13 +427,35 @@ function _connect(port, address, callback) {
413427 this . once ( 'connect' , callback ) ;
414428
415429 const afterDns = ( ex , ip ) => {
430+ if ( ! ex && ! address && permission . isEnabled ( ) ) {
431+ const resource = `${ ip } /${ port } ` ;
432+ if ( ! permission . has ( 'net.udp' , resource ) ) {
433+ ex = new ERR_ACCESS_DENIED (
434+ `connect to ${ resource } ` ,
435+ resource ,
436+ 'NetUDP'
437+ ) ;
438+ }
439+ }
416440 defaultTriggerAsyncIdScope (
417441 this [ async_id_symbol ] ,
418442 doConnect ,
419443 ex , this , ip , address , port , callback ,
420444 ) ;
421445 } ;
422-
446+ if ( address && permission . isEnabled ( ) ) {
447+ const resource = `${ address } /${ port } ` ;
448+ if ( ! permission . has ( 'net.udp' , resource ) ) {
449+ process . nextTick ( ( ) => {
450+ afterDns ( new ERR_ACCESS_DENIED (
451+ `connect to ${ resource } ` ,
452+ resource ,
453+ 'NetUDP'
454+ ) ) ;
455+ } ) ;
456+ return ;
457+ }
458+ }
423459 state . handle . lookup ( address , afterDns ) ;
424460}
425461
@@ -430,9 +466,13 @@ function doConnect(ex, self, ip, address, port, callback) {
430466 return ;
431467
432468 if ( ! ex ) {
433- const err = state . handle . connect ( ip , port ) ;
434- if ( err ) {
435- ex = new ExceptionWithHostPort ( err , 'connect' , address , port ) ;
469+ try {
470+ const err = state . handle . connect ( ip , port ) ;
471+ if ( err ) {
472+ ex = new ExceptionWithHostPort ( err , 'connect' , address , port ) ;
473+ }
474+ } catch ( e ) {
475+ ex = e ;
436476 }
437477 }
438478
@@ -663,6 +703,17 @@ Socket.prototype.send = function(buffer,
663703 }
664704
665705 const afterDns = ( ex , ip ) => {
706+ // If we have not checked before dns, check it now
707+ if ( ! ex && ! connected && ! address && permission . isEnabled ( ) ) {
708+ const resource = `${ ip } /${ port } ` ;
709+ if ( ! permission . has ( 'net.udp' , resource ) ) {
710+ ex = new ERR_ACCESS_DENIED (
711+ `send to ${ resource } ` ,
712+ resource ,
713+ 'NetUDP'
714+ ) ;
715+ }
716+ }
666717 defaultTriggerAsyncIdScope (
667718 this [ async_id_symbol ] ,
668719 doSend ,
@@ -671,6 +722,20 @@ Socket.prototype.send = function(buffer,
671722 } ;
672723
673724 if ( ! connected ) {
725+ // If address is not empty, check it
726+ if ( address && permission . isEnabled ( ) ) {
727+ const resource = `${ address } /${ port } ` ;
728+ if ( ! permission . has ( 'net.udp' , resource ) ) {
729+ process . nextTick ( ( ) => {
730+ afterDns ( new ERR_ACCESS_DENIED (
731+ `send to ${ resource } ` ,
732+ resource ,
733+ 'NetUDP'
734+ ) ) ;
735+ } ) ;
736+ return ;
737+ }
738+ }
674739 state . handle . lookup ( address , afterDns ) ;
675740 } else {
676741 afterDns ( null , null ) ;
0 commit comments