mirror of
				https://github.com/LibreTranslate/LibreTranslate.git
				synced 2025-11-03 19:17:17 -05:00 
			
		
		
		
	
		
			
				
	
	
		
			16 lines
		
	
	
		
			428 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			16 lines
		
	
	
		
			428 B
		
	
	
	
		
			Python
		
	
	
	
	
	
import os
 | 
						|
 | 
						|
 | 
						|
class SuspiciousFileOperation(Exception):
 | 
						|
    pass
 | 
						|
 | 
						|
 | 
						|
def path_traversal_check(unsafe_path, known_safe_path):
 | 
						|
    known_safe_path = os.path.abspath(known_safe_path)
 | 
						|
    unsafe_path = os.path.abspath(unsafe_path)
 | 
						|
 | 
						|
    if (os.path.commonprefix([known_safe_path, unsafe_path]) != known_safe_path):
 | 
						|
        raise SuspiciousFileOperation("{} is not safe".format(unsafe_path))
 | 
						|
 | 
						|
    # Passes the check
 | 
						|
    return unsafe_path |