|
| 1 | +package lucee.runtime.config; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.concurrent.ConcurrentHashMap; |
| 7 | + |
| 8 | +import lucee.commons.io.SystemUtil; |
| 9 | +import lucee.commons.io.log.Log; |
| 10 | +import lucee.commons.io.res.Resource; |
| 11 | +import lucee.commons.io.res.util.ResourceUtil; |
| 12 | +import lucee.commons.lang.StringUtil; |
| 13 | +import lucee.runtime.Mapping; |
| 14 | +import lucee.runtime.MappingImpl; |
| 15 | +import lucee.runtime.PageContext; |
| 16 | + |
| 17 | +/** |
| 18 | + * Manages virtual directory mappings from x-vdirs header (mod_cfml). |
| 19 | + * |
| 20 | + * Virtual directories are parsed from the x-vdirs header format: |
| 21 | + * /virtual1,/physical/path1;/virtual2,/physical/path2; |
| 22 | + * |
| 23 | + * Security: Requires x-vdirs-sharedkey header to match lucee.vdirs.sharedkey configuration. |
| 24 | + * Feature is disabled if lucee.vdirs.sharedkey is not configured. |
| 25 | + */ |
| 26 | +public class VirtualDirectoryManager { |
| 27 | + |
| 28 | + private static final String CONFIGURED_SHARED_KEY = SystemUtil.getSystemPropOrEnvVar( "lucee.vdirs.sharedkey", |
| 29 | + "" ); |
| 30 | + |
| 31 | + private final ConfigWeb config; |
| 32 | + |
| 33 | + // Cache: x-vdirs header value hash -> array of Mapping objects |
| 34 | + private final Map<Integer, Mapping[]> cache = new ConcurrentHashMap<>( 4 ); |
| 35 | + |
| 36 | + private volatile boolean invalidKeyWarned = false; |
| 37 | + |
| 38 | + public VirtualDirectoryManager( ConfigWeb config ) { |
| 39 | + this.config = config; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Gets virtual directory mappings for the current request. |
| 44 | + * Returns null if feature is disabled or validation fails. |
| 45 | + * |
| 46 | + * @param pc PageContext for current request |
| 47 | + * @return Array of Mapping objects, or null |
| 48 | + */ |
| 49 | + public Mapping[] getVirtualDirectoryMappings( PageContext pc ) { |
| 50 | + // Feature disabled if no shared key configured |
| 51 | + if ( StringUtil.isEmpty( CONFIGURED_SHARED_KEY ) ) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + // Read x-vdirs header directly from servlet request (avoids lazy-loading CGI scope) |
| 56 | + String xVdirs = pc.getHttpServletRequest().getHeader( "x-vdirs" ); |
| 57 | + |
| 58 | + // No virtual directories in this request |
| 59 | + if ( StringUtil.isEmpty( xVdirs ) ) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + // Validate shared key |
| 64 | + String xVdirsSharedKey = pc.getHttpServletRequest().getHeader( "x-vdirs-sharedkey" ); |
| 65 | + if ( StringUtil.isEmpty( xVdirsSharedKey ) || !CONFIGURED_SHARED_KEY.equals( xVdirsSharedKey ) ) { |
| 66 | + if ( !invalidKeyWarned ) { |
| 67 | + invalidKeyWarned = true; |
| 68 | + Log log = config.getLog( "application" ); |
| 69 | + if ( log != null ) { |
| 70 | + log.error( "VirtualDirectoryManager", |
| 71 | + "x-vdirs header received but x-vdirs-sharedkey is missing or doesn't match configured shared key. Ignoring virtual directories." ); |
| 72 | + } |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + // Check cache |
| 78 | + int cacheKey = xVdirs.hashCode(); |
| 79 | + Mapping[] cached = cache.get( cacheKey ); |
| 80 | + if ( cached != null ) { |
| 81 | + return cached; |
| 82 | + } |
| 83 | + |
| 84 | + // Parse and cache |
| 85 | + Mapping[] mappings = parseVirtualDirectories( xVdirs ); |
| 86 | + if ( mappings != null && mappings.length > 0 ) { |
| 87 | + cache.put( cacheKey, mappings ); |
| 88 | + } |
| 89 | + |
| 90 | + return mappings; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Parses x-vdirs header format: /virtual1,/physical1;/virtual2,/physical2; |
| 95 | + * |
| 96 | + * @param xVdirs Header value |
| 97 | + * @return Array of Mapping objects |
| 98 | + */ |
| 99 | + private Mapping[] parseVirtualDirectories( String xVdirs ) { |
| 100 | + if ( StringUtil.isEmpty( xVdirs ) ) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + // Split by semicolon |
| 105 | + String[] entries = xVdirs.split( ";" ); |
| 106 | + if ( entries.length == 0 ) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + // Parse each entry |
| 111 | + List<Mapping> mappings = new ArrayList<>(); |
| 112 | + for ( String entry : entries ) { |
| 113 | + entry = entry.trim(); |
| 114 | + if ( entry.isEmpty() ) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + // Split by comma |
| 119 | + String[] parts = entry.split( ",", 2 ); |
| 120 | + if ( parts.length != 2 ) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + String virtual = parts[0].trim(); |
| 125 | + String physical = parts[1].trim(); |
| 126 | + |
| 127 | + // Skip invalid entries |
| 128 | + if ( virtual.length() <= 1 || physical.length() <= 1 ) { |
| 129 | + continue; |
| 130 | + } |
| 131 | + |
| 132 | + // Convert Windows backslashes to forward slashes |
| 133 | + physical = physical.replace( '\\', '/' ); |
| 134 | + |
| 135 | + // Create mapping |
| 136 | + try { |
| 137 | + Resource physicalResource = ResourceUtil.toResourceExisting( config, physical ); |
| 138 | + if ( physicalResource != null && physicalResource.exists() ) { |
| 139 | + Mapping mapping = new MappingImpl( config, virtual, physical, null, Config.INSPECT_UNDEFINED, ConfigPro.INSPECT_INTERVAL_UNDEFINED, |
| 140 | + ConfigPro.INSPECT_INTERVAL_UNDEFINED, true, false, true, true, false, false, null, -1, -1 ); |
| 141 | + mappings.add( mapping ); |
| 142 | + } |
| 143 | + } |
| 144 | + catch ( Exception e ) { |
| 145 | + // Log and skip invalid mappings |
| 146 | + Log log = config.getLog( "application" ); |
| 147 | + if ( log != null ) { |
| 148 | + log.error( "VirtualDirectoryManager", "Failed to create virtual directory mapping: " + virtual + " -> " + physical, e ); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return mappings.isEmpty() ? null : mappings.toArray( new Mapping[0] ); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Clears the cache. Called when configuration changes or for testing. |
| 158 | + */ |
| 159 | + public void clearCache() { |
| 160 | + cache.clear(); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Returns true if virtual directory feature is enabled (shared key is configured). |
| 165 | + */ |
| 166 | + public boolean isEnabled() { |
| 167 | + return !StringUtil.isEmpty( CONFIGURED_SHARED_KEY ); |
| 168 | + } |
| 169 | +} |
0 commit comments