@@ -4,6 +4,13 @@ import { apiClient } from "../../../lib/apiClient.js";
44import { customFuzzySearch } from "../../../lib/fuzzy.js" ;
55import { getBrowserStackAuth } from "../../../lib/get-auth.js" ;
66import { BrowserStackConfig } from "../../../lib/types.js" ;
7+ import {
8+ validateUploadPath ,
9+ UploadValidationOptions ,
10+ APP_BINARY_EXTENSIONS ,
11+ MAX_APP_UPLOAD_BYTES ,
12+ } from "../../../lib/upload-validator.js" ;
13+ import appConfig from "../../../config.js" ;
714
815interface Device {
916 device : string ;
@@ -139,14 +146,14 @@ export async function uploadApp(
139146 username : string ,
140147 password : string ,
141148) : Promise < string > {
142- const filePath = appPath ;
143-
144- if ( ! fs . existsSync ( filePath ) ) {
145- throw new Error ( `File not found at path: ${ filePath } ` ) ;
146- }
149+ const safePath = validateUploadPath ( appPath , {
150+ allowedExtensions : APP_BINARY_EXTENSIONS ,
151+ maxSizeBytes : MAX_APP_UPLOAD_BYTES ,
152+ allowedBaseDir : appConfig . UPLOAD_BASE_DIR ,
153+ } ) ;
147154
148155 const formData = new FormData ( ) ;
149- formData . append ( "file" , fs . createReadStream ( filePath ) ) ;
156+ formData . append ( "file" , fs . createReadStream ( safePath ) ) ;
150157
151158 const response = await apiClient . post < UploadResponse > ( {
152159 url : "https://api-cloud.browserstack.com/app-automate/upload" ,
@@ -171,13 +178,16 @@ async function uploadFileToBrowserStack(
171178 endpoint : string ,
172179 responseKey : string ,
173180 config : BrowserStackConfig ,
181+ validation : Pick < UploadValidationOptions , "allowedExtensions" > ,
174182) : Promise < string > {
175- if ( ! fs . existsSync ( filePath ) ) {
176- throw new Error ( `File not found at path: ${ filePath } ` ) ;
177- }
183+ const safePath = validateUploadPath ( filePath , {
184+ allowedExtensions : validation . allowedExtensions ,
185+ maxSizeBytes : MAX_APP_UPLOAD_BYTES ,
186+ allowedBaseDir : appConfig . UPLOAD_BASE_DIR ,
187+ } ) ;
178188
179189 const formData = new FormData ( ) ;
180- formData . append ( "file" , fs . createReadStream ( filePath ) ) ;
190+ formData . append ( "file" , fs . createReadStream ( safePath ) ) ;
181191
182192 const authHeader =
183193 "Basic " +
@@ -211,6 +221,7 @@ export async function uploadEspressoApp(
211221 "https://api-cloud.browserstack.com/app-automate/espresso/v2/app" ,
212222 "app_url" ,
213223 config ,
224+ { allowedExtensions : [ ".apk" , ".aab" ] } ,
214225 ) ;
215226}
216227
@@ -224,6 +235,7 @@ export async function uploadEspressoTestSuite(
224235 "https://api-cloud.browserstack.com/app-automate/espresso/v2/test-suite" ,
225236 "test_suite_url" ,
226237 config ,
238+ { allowedExtensions : [ ".apk" ] } ,
227239 ) ;
228240}
229241
@@ -237,6 +249,7 @@ export async function uploadXcuiApp(
237249 "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/app" ,
238250 "app_url" ,
239251 config ,
252+ { allowedExtensions : [ ".ipa" ] } ,
240253 ) ;
241254}
242255
@@ -250,6 +263,7 @@ export async function uploadXcuiTestSuite(
250263 "https://api-cloud.browserstack.com/app-automate/xcuitest/v2/test-suite" ,
251264 "test_suite_url" ,
252265 config ,
266+ { allowedExtensions : [ ".zip" ] } ,
253267 ) ;
254268}
255269
0 commit comments