@@ -10,7 +10,6 @@ import {
1010import type {
1111 OtelHttpAgentFactory ,
1212 OtelHttpAgentOptions ,
13- OtelLogger ,
1413 OtelSignalIdentifier ,
1514} from "./service-types.js" ;
1615
@@ -75,8 +74,7 @@ function readOtelEnvFile(params: {
7574 signalIdentifier : OtelSignalIdentifier ;
7675 signalSuffix : "CERTIFICATE" | "CLIENT_CERTIFICATE" | "CLIENT_KEY" ;
7776 sharedEnvName : string ;
78- logger : OtelLogger ;
79- warning : string ;
77+ label : string ;
8078} ) : Buffer | undefined {
8179 const signalEnvName = `OTEL_EXPORTER_OTLP_${ params . signalIdentifier } _${ params . signalSuffix } ` ;
8280 const filePath =
@@ -86,48 +84,53 @@ function readOtelEnvFile(params: {
8684 return undefined ;
8785 }
8886 try {
89- return readFileSync ( nodePath . resolve ( process . cwd ( ) , filePath ) ) ;
87+ const material = readFileSync ( nodePath . resolve ( process . cwd ( ) , filePath ) ) ;
88+ if ( material . length > 0 ) {
89+ return material ;
90+ }
9091 } catch {
91- params . logger . warn ( `diagnostics-otel: ${ params . warning } ` ) ;
92- return undefined ;
92+ // Never expose certificate paths or silently downgrade to system trust.
9393 }
94+ throw new Error (
95+ `Configured OpenTelemetry ${ params . label } file is missing, empty, or unreadable; refusing insecure export` ,
96+ ) ;
9497}
9598
9699function normalizeOtelEnvValue ( value : string | undefined ) : string | undefined {
97- const trimmed = value ?. trim ( ) ;
98- return trimmed ? trimmed : undefined ;
100+ return value ?. trim ( ) ? value : undefined ;
99101}
100102
101103export function resolveOtelHttpAgentOptions ( params : {
102104 url : string | undefined ;
103105 signalIdentifier : OtelSignalIdentifier ;
104- logger : OtelLogger ;
105- } ) : OtelHttpAgentFactory | undefined {
106- const { url, signalIdentifier, logger } = params ;
107- if ( ! url ) {
108- return undefined ;
109- }
106+ } ) : OtelHttpAgentFactory | OtelHttpAgentOptions | undefined {
107+ const { url, signalIdentifier } = params ;
110108 const ca = readOtelEnvFile ( {
111109 signalIdentifier,
112110 signalSuffix : "CERTIFICATE" ,
113111 sharedEnvName : OTEL_EXPORTER_OTLP_CERTIFICATE_ENV ,
114- logger,
115- warning : "failed to read root certificate file" ,
112+ label : "TLS root certificate" ,
116113 } ) ;
117114 const cert = readOtelEnvFile ( {
118115 signalIdentifier,
119116 signalSuffix : "CLIENT_CERTIFICATE" ,
120117 sharedEnvName : OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE_ENV ,
121- logger,
122- warning : "failed to read client certificate chain file" ,
118+ label : "mTLS client certificate" ,
123119 } ) ;
124120 const key = readOtelEnvFile ( {
125121 signalIdentifier,
126122 signalSuffix : "CLIENT_KEY" ,
127123 sharedEnvName : OTEL_EXPORTER_OTLP_CLIENT_KEY_ENV ,
128- logger,
129- warning : "failed to read client certificate private key file" ,
124+ label : "mTLS client private key" ,
130125 } ) ;
126+ if ( ( cert === undefined ) !== ( key === undefined ) ) {
127+ throw new Error (
128+ "Configured OpenTelemetry mTLS requires both a client certificate and private key; refusing insecure export" ,
129+ ) ;
130+ }
131+ if ( ! url ) {
132+ return undefined ;
133+ }
131134 const agentOptions : OtelHttpAgentOptions = {
132135 keepAlive : true ,
133136 ...( ca !== undefined ? { ca } : { } ) ,
@@ -136,10 +139,13 @@ export function resolveOtelHttpAgentOptions(params: {
136139 } ;
137140 try {
138141 const agent = createNodeProxyAgent ( { mode : "env" , targetUrl : url , agentOptions } ) ;
139- return agent ? ( ) => agent : undefined ;
142+ if ( agent ) {
143+ return ( ) => agent ;
144+ }
140145 } catch {
141146 throw new Error ( "Configured telemetry proxy is invalid or unsupported; refusing direct export" ) ;
142147 }
148+ return ( ca || cert || key ) && new URL ( url ) . protocol === "https:" ? agentOptions : undefined ;
143149}
144150
145151export function resolveSampleRate ( value : number | undefined ) : number | undefined {
0 commit comments