1+ // Extracted utility functions for better testability
2+
3+ /**
4+ * Disable signatures in boot configuration
5+ */
6+ function disableSignatures ( configContent ) {
7+ return configContent . replace ( / s e t s i g s _ e n a b l e d t r u e / g, 'set sigs_enabled false' ) ;
8+ }
9+
10+ /**
11+ * Validate port number
12+ */
13+ function validatePort ( port , defaultPort = 3000 ) {
14+ const portNum = Number ( port ) ;
15+ if ( ! Number . isInteger ( portNum ) || portNum < 1 || portNum > 65535 ) {
16+ return defaultPort ;
17+ }
18+ return portNum ;
19+ }
20+
21+ /**
22+ * Check if version is a commit SHA
23+ */
24+ function isCommitSha ( version ) {
25+ return version . length === 40 && / ^ [ a - f 0 - 9 ] + $ / i. test ( version ) ;
26+ }
27+
28+ /**
29+ * Generate download URL based on version type
30+ */
31+ function getDownloadUrl ( version , file = '' ) {
32+ const baseUrl = isCommitSha ( version )
33+ ? `https://s3.amazonaws.com/dev.boot.netboot.xyz/${ version } /ipxe/`
34+ : `https://github.com/netbootxyz/netboot.xyz/releases/download/${ version } /` ;
35+ return baseUrl + file ;
36+ }
37+
38+ /**
39+ * Validate file path for security
40+ */
41+ function validateFilePath ( userPath , rootDir ) {
42+ try {
43+ const path = require ( 'path' ) ;
44+ const resolved = path . resolve ( rootDir , userPath ) ;
45+ const rootWithSeparator = path . resolve ( rootDir ) + path . sep ;
46+ return {
47+ path : resolved ,
48+ isSecure : resolved . startsWith ( rootWithSeparator )
49+ } ;
50+ } catch {
51+ return { path : null , isSecure : false } ;
52+ }
53+ }
54+
55+ /**
56+ * Check if host is allowed for downloads
57+ */
58+ function isAllowedHost ( url , allowedHosts = [ 's3.amazonaws.com' ] ) {
59+ try {
60+ const urlLib = require ( 'url' ) ;
61+ const parsedUrl = urlLib . parse ( url ) ;
62+ return allowedHosts . includes ( parsedUrl . host ) ;
63+ } catch {
64+ return false ;
65+ }
66+ }
67+
68+ module . exports = {
69+ disableSignatures,
70+ validatePort,
71+ isCommitSha,
72+ getDownloadUrl,
73+ validateFilePath,
74+ isAllowedHost
75+ } ;
0 commit comments