1
0
Fork 0

move fixpath back to string.nas. It *is* about strings, after all, and also

usable for property paths, so io.nas doesn't seem the right place.
This commit is contained in:
mfranz 2008-06-13 13:01:38 +00:00
parent 496a76b9fa
commit 4d17687f12
2 changed files with 35 additions and 34 deletions

View file

@ -113,44 +113,14 @@ var writexml = func(path, node, indent = "\t", prefix = "___") {
die("writexml(): tree has more than one root node"); die("writexml(): tree has more than one root node");
} }
# Removes superfluous slashes, empty and "." elements, expands
# all ".." elements, and turns all backslashes into slashes.
# The result will start with a slash if it started with a slash
# or backslash, it will end without slash. Should be applied on
# absolute property or file paths, otherwise ".." elements might
# be resolved wrongly.
#
var fixpath = func(path) {
var d = "";
for(var i = 0; i < size(path); i += 1)
d ~= path[i] == `\\` ? "/" : chr(path[i]);
var prefix = d[0] == `/` ? "/" : "";
var stack = [];
foreach(var e; split("/", d)) {
if(e == "." or e == "")
continue;
elsif(e == "..")
pop(stack);
else
append(stack, e);
}
if(!size(stack))
return "/";
path = stack[0];
foreach(var s; subvec(stack, 1))
path ~= "/" ~ s;
return prefix ~ path;
}
# Redefine io.open() such that files can only be written under authorized directories. # Redefine io.open() such that files can only be written under authorized directories.
# #
setlistener("/sim/signals/nasal-dir-initialized", func { setlistener("/sim/signals/nasal-dir-initialized", func {
var _open = open; var _open = open;
var writable_dirs = [ var writable_dirs = [
# "*", # any # "", # any
# fixpath(getprop("/sim/fg-root")) ~ "/Scenery/", # string.fixpath(getprop("/sim/fg-root")) ~ "/Scenery/",
fixpath(getprop("/sim/fg-home")) ~ "/", string.fixpath(getprop("/sim/fg-home")) ~ "/",
"/tmp/", "/var/tmp/", "/tmp/", "/var/tmp/",
"[A-Za-z]:TMP/", "[A-Za-z]:TEMP/", "[A-Za-z]:TMP/", "[A-Za-z]:TEMP/",
"[A-Za-z]:/TMP/", "[A-Za-z]:/TEMP/", "[A-Za-z]:/TMP/", "[A-Za-z]:/TEMP/",
@ -166,7 +136,7 @@ setlistener("/sim/signals/nasal-dir-initialized", func {
return _open(path, mode); return _open(path, mode);
} }
var fpath = fixpath(path); var fpath = string.fixpath(path);
if(fpath != path) print(debug._error("SECURITY: fix path '" ~ path ~ "' -> '" ~ fpath ~ "'")); if(fpath != path) print(debug._error("SECURITY: fix path '" ~ path ~ "' -> '" ~ fpath ~ "'"));
foreach(var p; writable_dirs) { foreach(var p; writable_dirs) {
print("SECURITY: check for path '" ~ p ~ "'"); print("SECURITY: check for path '" ~ p ~ "'");

View file

@ -155,3 +155,34 @@ var match = func(str, patt) {
} }
##
# Removes superfluous slashes, empty and "." elements, expands
# all ".." elements, and turns all backslashes into slashes.
# The result will start with a slash if it started with a slash
# or backslash, it will end without slash. Should be applied on
# absolute property or file paths, otherwise ".." elements might
# be resolved wrongly.
#
var fixpath = func(path) {
var d = "";
for (var i = 0; i < size(path); i += 1)
d ~= path[i] == `\\` ? "/" : chr(path[i]);
var prefix = d[0] == `/` ? "/" : "";
var stack = [];
foreach (var e; split("/", d)) {
if (e == "." or e == "")
continue;
elsif (e == "..")
pop(stack);
else
append(stack, e);
}
if (!size(stack))
return "/";
path = stack[0];
foreach (var s; subvec(stack, 1))
path ~= "/" ~ s;
return prefix ~ path;
}