@@ -82,8 +82,11 @@ type CameraClipPayload = {
8282 hasAudio : boolean ;
8383} ;
8484
85- /** Validate and normalize an unknown camera still-image payload. */
86- export function parseCameraSnapPayload ( value : unknown ) : CameraSnapPayload {
85+ /** Validate a complete still-image payload before any capture can be published. */
86+ export function parseCameraSnapPayload (
87+ value : unknown ,
88+ opts : { expectedHost ?: string } = { } ,
89+ ) : CameraSnapPayload {
8790 const obj = asRecord ( value ) ;
8891 const format = readStringValue ( obj . format ) ;
8992 const base64 = readStringValue ( obj . base64 ) ;
@@ -93,6 +96,12 @@ export function parseCameraSnapPayload(value: unknown): CameraSnapPayload {
9396 if ( ! format || ( ! base64 && ! url ) || width === undefined || height === undefined ) {
9497 throw new Error ( "invalid camera.snap payload" ) ;
9598 }
99+ if ( url ) {
100+ validateCameraPayloadUrl ( url , requireNodeRemoteIp ( opts . expectedHost ) ) ;
101+ }
102+ if ( base64 ) {
103+ validateCameraPayloadBase64 ( base64 , MAX_CAMERA_BASE64_BYTES ) ;
104+ }
96105 return { format, ...( base64 ? { base64 } : { } ) , ...( url ? { url } : { } ) , width, height } ;
97106}
98107
@@ -128,21 +137,26 @@ export function cameraTempPath(opts: {
128137 return path . join ( tmpDir , `${ cliName } -camera-${ opts . kind } ${ facingPart } -${ id } ${ ext } ` ) ;
129138}
130139
131- /** Download a node-hosted media URL to disk after HTTPS, host, redirect, and size checks. */
132- async function writeUrlToFile ( filePath : string , url : string , opts : { expectedHost : string } ) {
140+ function validateCameraPayloadUrl ( url : string , expectedNodeHost : string ) : string {
133141 const parsed = new URL ( url ) ;
134142 if ( parsed . protocol !== "https:" ) {
135143 throw new Error ( `writeUrlToFile: only https URLs are allowed, got ${ parsed . protocol } ` ) ;
136144 }
137- const expectedHost = normalizeHostname ( opts . expectedHost ) ;
145+ const expectedHost = normalizeHostname ( expectedNodeHost ) ;
138146 if ( ! expectedHost ) {
139147 throw new Error ( "writeUrlToFile: expectedHost is required" ) ;
140148 }
141149 if ( normalizeHostname ( parsed . hostname ) !== expectedHost ) {
142150 throw new Error (
143- `writeUrlToFile: url host ${ parsed . hostname } must match node host ${ opts . expectedHost } ` ,
151+ `writeUrlToFile: url host ${ parsed . hostname } must match node host ${ expectedNodeHost } ` ,
144152 ) ;
145153 }
154+ return expectedHost ;
155+ }
156+
157+ /** Download a node-hosted media URL to disk after HTTPS, host, redirect, and size checks. */
158+ async function writeUrlToFile ( filePath : string , url : string , opts : { expectedHost : string } ) {
159+ const expectedHost = validateCameraPayloadUrl ( url , opts . expectedHost ) ;
146160
147161 // The node host is allowed even when private because the RPC response supplied its remote IP.
148162 const policy = {
@@ -234,20 +248,25 @@ async function writeUrlToFile(filePath: string, url: string, opts: { expectedHos
234248 return { path : filePath , bytes } ;
235249}
236250
237- /** Decode a base64 media payload to disk with preflight and post-decode size checks. */
238- export async function writeBase64ToFile (
239- filePath : string ,
240- base64 : string ,
241- opts : { maxBytes ?: number } = { } ,
242- ) {
243- const maxBytes = opts . maxBytes ?? MAX_CAMERA_BASE64_BYTES ;
251+ function validateCameraPayloadBase64 ( base64 : string , maxBytes : number ) : string {
244252 if ( estimateBase64DecodedBytes ( base64 ) > maxBytes ) {
245253 throw new Error ( `writeBase64ToFile: decoded payload exceeds max ${ maxBytes } ` ) ;
246254 }
247255 const canonicalBase64 = canonicalizeBase64 ( base64 ) ;
248256 if ( ! canonicalBase64 ) {
249257 throw new Error ( "writeBase64ToFile: invalid base64 payload" ) ;
250258 }
259+ return canonicalBase64 ;
260+ }
261+
262+ /** Decode a base64 media payload to disk with preflight and post-decode size checks. */
263+ export async function writeBase64ToFile (
264+ filePath : string ,
265+ base64 : string ,
266+ opts : { maxBytes ?: number } = { } ,
267+ ) {
268+ const maxBytes = opts . maxBytes ?? MAX_CAMERA_BASE64_BYTES ;
269+ const canonicalBase64 = validateCameraPayloadBase64 ( base64 , maxBytes ) ;
251270 const buf = Buffer . from ( canonicalBase64 , "base64" ) ;
252271 if ( buf . length > maxBytes ) {
253272 throw new Error ( `writeBase64ToFile: decoded ${ buf . length } bytes, exceeds max ${ maxBytes } ` ) ;
0 commit comments