开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
Java12Source
/
src
/
java.base
/
java
/
io
/
FilePermission.java
Java12Source
/
src
/
java.base
/
java
/
io
/
FilePermission.java
FilePermission.java 46.87 KB
一键复制 编辑 原始数据 按行查看 历史
Stefan 提交于 2021年03月18日 23:35 +08:00 . init
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.io;
import java.nio.file.*;
import java.security.*;
import java.util.Enumeration;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import jdk.internal.access.JavaIOFilePermissionAccess;
import jdk.internal.access.SharedSecrets;
import sun.nio.fs.DefaultFileSystemProvider;
import sun.security.action.GetPropertyAction;
import sun.security.util.FilePermCompat;
import sun.security.util.SecurityConstants;
/**
* This class represents access to a file or directory. A FilePermission consists
* of a pathname and a set of actions valid for that pathname.
* <P>
* Pathname is the pathname of the file or directory granted the specified
* actions. A pathname that ends in "/*" (where "/" is
* the file separator character, <code>File.separatorChar</code>) indicates
* all the files and directories contained in that directory. A pathname
* that ends with "/-" indicates (recursively) all files
* and subdirectories contained in that directory. Such a pathname is called
* a wildcard pathname. Otherwise, it's a simple pathname.
* <P>
* A pathname consisting of the special token {@literal "<<ALL FILES>>"}
* matches <b>any</b> file.
* <P>
* Note: A pathname consisting of a single "*" indicates all the files
* in the current directory, while a pathname consisting of a single "-"
* indicates all the files in the current directory and
* (recursively) all files and subdirectories contained in the current
* directory.
* <P>
* The actions to be granted are passed to the constructor in a string containing
* a list of one or more comma-separated keywords. The possible keywords are
* "read", "write", "execute", "delete", and "readlink". Their meaning is
* defined as follows:
*
* <DL>
* <DT> read <DD> read permission
* <DT> write <DD> write permission
* <DT> execute
* <DD> execute permission. Allows <code>Runtime.exec</code> to
* be called. Corresponds to <code>SecurityManager.checkExec</code>.
* <DT> delete
* <DD> delete permission. Allows <code>File.delete</code> to
* be called. Corresponds to <code>SecurityManager.checkDelete</code>.
* <DT> readlink
* <DD> read link permission. Allows the target of a
* <a href="../nio/file/package-summary.html#links">symbolic link</a>
* to be read by invoking the {@link java.nio.file.Files#readSymbolicLink
* readSymbolicLink } method.
* </DL>
* <P>
* The actions string is converted to lowercase before processing.
* <P>
* Be careful when granting FilePermissions. Think about the implications
* of granting read and especially write access to various files and
* directories. The {@literal "<<ALL FILES>>"} permission with write action is
* especially dangerous. This grants permission to write to the entire
* file system. One thing this effectively allows is replacement of the
* system binary, including the JVM runtime environment.
* <P>
* Please note: Code can always read a file from the same
* directory it's in (or a subdirectory of that directory); it does not
* need explicit permission to do so.
*
* @see java.security.Permission
* @see java.security.Permissions
* @see java.security.PermissionCollection
*
*
* @author Marianne Mueller
* @author Roland Schemers
* @since 1.2
*
* @serial exclude
*/
public final class FilePermission extends Permission implements Serializable {
/**
* Execute action.
*/
private static final int EXECUTE = 0x1;
/**
* Write action.
*/
private static final int WRITE = 0x2;
/**
* Read action.
*/
private static final int READ = 0x4;
/**
* Delete action.
*/
private static final int DELETE = 0x8;
/**
* Read link action.
*/
private static final int READLINK = 0x10;
/**
* All actions (read,write,execute,delete,readlink)
*/
private static final int ALL = READ|WRITE|EXECUTE|DELETE|READLINK;
/**
* No actions.
*/
private static final int NONE = 0x0;
// the actions mask
private transient int mask;
// does path indicate a directory? (wildcard or recursive)
private transient boolean directory;
// is it a recursive directory specification?
private transient boolean recursive;
/**
* the actions string.
*
* @serial
*/
private String actions; // Left null as long as possible, then
// created and re-used in the getAction function.
// canonicalized dir path. used by the "old" behavior (nb == false).
// In the case of directories, it is the name "/blah/*" or "/blah/-"
// without the last character (the "*" or "-").
private transient String cpath;
// Following fields used by the "new" behavior (nb == true), in which
// input path is not canonicalized. For compatibility (so that granting
// FilePermission on "x" allows reading "`pwd`/x", an alternative path
// can be added so that both can be used in an implies() check. Please note
// the alternative path only deals with absolute/relative path, and does
// not deal with symlink/target.
private transient Path npath; // normalized dir path.
private transient Path npath2; // alternative normalized dir path.
private transient boolean allFiles; // whether this is <<ALL FILES>>
private transient boolean invalid; // whether input path is invalid
// static Strings used by init(int mask)
private static final char RECURSIVE_CHAR = '-';
private static final char WILD_CHAR = '*';
// public String toString() {
// StringBuffer sb = new StringBuffer();
// sb.append("*** FilePermission on " + getName() + " ***");
// for (Field f : FilePermission.class.getDeclaredFields()) {
// if (!Modifier.isStatic(f.getModifiers())) {
// try {
// sb.append(f.getName() + " = " + f.get(this));
// } catch (Exception e) {
// sb.append(f.getName() + " = " + e.toString());
// }
// sb.append('\n');
// }
// }
// sb.append("***\n");
// return sb.toString();
// }
private static final long serialVersionUID = 7930732926638008763L;
/**
* Use the platform's default file system to avoid recursive initialization
* issues when the VM is configured to use a custom file system provider.
*/
private static final java.nio.file.FileSystem builtInFS =
DefaultFileSystemProvider.theFileSystem();
private static final Path here = builtInFS.getPath(
GetPropertyAction.privilegedGetProperty("user.dir"));
private static final Path EMPTY_PATH = builtInFS.getPath("");
private static final Path DASH_PATH = builtInFS.getPath("-");
private static final Path DOTDOT_PATH = builtInFS.getPath("..");
/**
* A private constructor that clones some and updates some,
* always with a different name.
* @param input
*/
private FilePermission(String name,
FilePermission input,
Path npath,
Path npath2,
int mask,
String actions) {
super(name);
// Customizables
this.npath = npath;
this.npath2 = npath2;
this.actions = actions;
this.mask = mask;
// Cloneds
this.allFiles = input.allFiles;
this.invalid = input.invalid;
this.recursive = input.recursive;
this.directory = input.directory;
this.cpath = input.cpath;
}
/**
* Returns the alternative path as a Path object, i.e. absolute path
* for a relative one, or vice versa.
*
* @param in a real path w/o "-" or "*" at the end, and not <<ALL FILES>>.
* @return the alternative path, or null if cannot find one.
*/
private static Path altPath(Path in) {
try {
if (!in.isAbsolute()) {
return here.resolve(in).normalize();
} else {
return here.relativize(in).normalize();
}
} catch (IllegalArgumentException e) {
return null;
}
}
static {
SharedSecrets.setJavaIOFilePermissionAccess(
/**
* Creates FilePermission objects with special internals.
* See {@link FilePermCompat#newPermPlusAltPath(Permission)} and
* {@link FilePermCompat#newPermUsingAltPath(Permission)}.
*/
new JavaIOFilePermissionAccess() {
public FilePermission newPermPlusAltPath(FilePermission input) {
if (!input.invalid && input.npath2 == null && !input.allFiles) {
Path npath2 = altPath(input.npath);
if (npath2 != null) {
// Please note the name of the new permission is
// different than the original so that when one is
// added to a FilePermissionCollection it will not
// be merged with the original one.
return new FilePermission(input.getName() + "#plus",
input,
input.npath,
npath2,
input.mask,
input.actions);
}
}
return input;
}
public FilePermission newPermUsingAltPath(FilePermission input) {
if (!input.invalid && !input.allFiles) {
Path npath2 = altPath(input.npath);
if (npath2 != null) {
// New name, see above.
return new FilePermission(input.getName() + "#using",
input,
npath2,
null,
input.mask,
input.actions);
}
}
return null;
}
}
);
}
/**
* initialize a FilePermission object. Common to all constructors.
* Also called during de-serialization.
*
* @param mask the actions mask to use.
*
*/
private void init(int mask) {
if ((mask & ALL) != mask)
throw new IllegalArgumentException("invalid actions mask");
if (mask == NONE)
throw new IllegalArgumentException("invalid actions mask");
if (FilePermCompat.nb) {
String name = getName();
if (name == null)
throw new NullPointerException("name can't be null");
this.mask = mask;
if (name.equals("<<ALL FILES>>")) {
allFiles = true;
npath = EMPTY_PATH;
// other fields remain default
return;
}
boolean rememberStar = false;
if (name.endsWith("*")) {
rememberStar = true;
recursive = false;
name = name.substring(0, name.length()-1) + "-";
}
try {
// new File() can "normalize" some name, for example, "/C:/X" on
// Windows. Some JDK codes generate such illegal names.
npath = builtInFS.getPath(new File(name).getPath())
.normalize();
// lastName should always be non-null now
Path lastName = npath.getFileName();
if (lastName != null && lastName.equals(DASH_PATH)) {
directory = true;
recursive = !rememberStar;
npath = npath.getParent();
}
if (npath == null) {
npath = EMPTY_PATH;
}
invalid = false;
} catch (InvalidPathException ipe) {
// Still invalid. For compatibility reason, accept it
// but make this permission useless.
npath = builtInFS.getPath("-u-s-e-l-e-s-s-");
invalid = true;
}
} else {
if ((cpath = getName()) == null)
throw new NullPointerException("name can't be null");
this.mask = mask;
if (cpath.equals("<<ALL FILES>>")) {
directory = true;
recursive = true;
cpath = "";
return;
}
// store only the canonical cpath if possible
cpath = AccessController.doPrivileged(new PrivilegedAction<>() {
public String run() {
try {
String path = cpath;
if (cpath.endsWith("*")) {
// call getCanonicalPath with a path with wildcard character
// replaced to avoid calling it with paths that are
// intended to match all entries in a directory
path = path.substring(0, path.length() - 1) + "-";
path = new File(path).getCanonicalPath();
return path.substring(0, path.length() - 1) + "*";
} else {
return new File(path).getCanonicalPath();
}
} catch (IOException ioe) {
return cpath;
}
}
});
int len = cpath.length();
char last = ((len > 0) ? cpath.charAt(len - 1) : 0);
if (last == RECURSIVE_CHAR &&
cpath.charAt(len - 2) == File.separatorChar) {
directory = true;
recursive = true;
cpath = cpath.substring(0, --len);
} else if (last == WILD_CHAR &&
cpath.charAt(len - 2) == File.separatorChar) {
directory = true;
//recursive = false;
cpath = cpath.substring(0, --len);
} else {
// overkill since they are initialized to false, but
// commented out here to remind us...
//directory = false;
//recursive = false;
}
// XXX: at this point the path should be absolute. die if it isn't?
}
}
/**
* Creates a new FilePermission object with the specified actions.
* <i>path</i> is the pathname of a file or directory, and <i>actions</i>
* contains a comma-separated list of the desired actions granted on the
* file or directory. Possible actions are
* "read", "write", "execute", "delete", and "readlink".
*
* <p>A pathname that ends in "/*" (where "/" is
* the file separator character, <code>File.separatorChar</code>)
* indicates all the files and directories contained in that directory.
* A pathname that ends with "/-" indicates (recursively) all files and
* subdirectories contained in that directory. The special pathname
* {@literal "<<ALL FILES>>"} matches any file.
*
* <p>A pathname consisting of a single "*" indicates all the files
* in the current directory, while a pathname consisting of a single "-"
* indicates all the files in the current directory and
* (recursively) all files and subdirectories contained in the current
* directory.
*
* <p>A pathname containing an empty string represents an empty path.
*
* @implNote In this implementation, the
* {@code jdk.io.permissionsUseCanonicalPath} system property dictates how
* the {@code path} argument is processed and stored.
* <P>
* If the value of the system property is set to {@code true}, {@code path}
* is canonicalized and stored as a String object named {@code cpath}.
* This means a relative path is converted to an absolute path, a Windows
* DOS-style 8.3 path is expanded to a long path, and a symbolic link is
* resolved to its target, etc.
* <P>
* If the value of the system property is set to {@code false}, {@code path}
* is converted to a {@link java.nio.file.Path} object named {@code npath}
* after {@link Path#normalize() normalization}. No canonicalization is
* performed which means the underlying file system is not accessed.
* If an {@link InvalidPathException} is thrown during the conversion,
* this {@code FilePermission} will be labeled as invalid.
* <P>
* In either case, the "*" or "-" character at the end of a wildcard
* {@code path} is removed before canonicalization or normalization.
* It is stored in a separate wildcard flag field.
* <P>
* The default value of the {@code jdk.io.permissionsUseCanonicalPath}
* system property is {@code false} in this implementation.
*
* @param path the pathname of the file/directory.
* @param actions the action string.
*
* @throws IllegalArgumentException
* If actions is <code>null</code>, empty or contains an action
* other than the specified possible actions.
*/
public FilePermission(String path, String actions) {
super(path);
init(getMask(actions));
}
/**
* Creates a new FilePermission object using an action mask.
* More efficient than the FilePermission(String, String) constructor.
* Can be used from within
* code that needs to create a FilePermission object to pass into the
* <code>implies</code> method.
*
* @param path the pathname of the file/directory.
* @param mask the action mask to use.
*/
// package private for use by the FilePermissionCollection add method
FilePermission(String path, int mask) {
super(path);
init(mask);
}
/**
* Checks if this FilePermission object "implies" the specified permission.
* <P>
* More specifically, this method returns true if:
* <ul>
* <li> <i>p</i> is an instanceof FilePermission,
* <li> <i>p</i>'s actions are a proper subset of this
* object's actions, and
* <li> <i>p</i>'s pathname is implied by this object's
* pathname. For example, "/tmp/*" implies "/tmp/foo", since
* "/tmp/*" encompasses all files in the "/tmp" directory,
* including the one named "foo".
* </ul>
* <P>
* Precisely, a simple pathname implies another simple pathname
* if and only if they are equal. A simple pathname never implies
* a wildcard pathname. A wildcard pathname implies another wildcard
* pathname if and only if all simple pathnames implied by the latter
* are implied by the former. A wildcard pathname implies a simple
* pathname if and only if
* <ul>
* <li>if the wildcard flag is "*", the simple pathname's path
* must be right inside the wildcard pathname's path.
* <li>if the wildcard flag is "-", the simple pathname's path
* must be recursively inside the wildcard pathname's path.
* </ul>
* <P>
* {@literal "<<ALL FILES>>"} implies every other pathname. No pathname,
* except for {@literal "<<ALL FILES>>"} itself, implies
* {@literal "<<ALL FILES>>"}.
*
* @implNote
* If {@code jdk.io.permissionsUseCanonicalPath} is {@code true}, a
* simple {@code cpath} is inside a wildcard {@code cpath} if and only if
* after removing the base name (the last name in the pathname's name
* sequence) from the former the remaining part equals to the latter,
* a simple {@code cpath} is recursively inside a wildcard {@code cpath}
* if and only if the former starts with the latter.
* <p>
* If {@code jdk.io.permissionsUseCanonicalPath} is {@code false}, a
* simple {@code npath} is inside a wildcard {@code npath} if and only if
* {@code simple_npath.relativize(wildcard_npath)} is exactly "..",
* a simple {@code npath} is recursively inside a wildcard {@code npath}
* if and only if {@code simple_npath.relativize(wildcard_npath)} is a
* series of one or more "..". This means "/-" implies "/foo" but not "foo".
* <p>
* An invalid {@code FilePermission} does not imply any object except for
* itself. An invalid {@code FilePermission} is not implied by any object
* except for itself or a {@code FilePermission} on
* {@literal "<<ALL FILES>>"} whose actions is a superset of this
* invalid {@code FilePermission}. Even if two {@code FilePermission}
* are created with the same invalid path, one does not imply the other.
*
* @param p the permission to check against.
*
* @return <code>true</code> if the specified permission is not
* <code>null</code> and is implied by this object,
* <code>false</code> otherwise.
*/
@Override
public boolean implies(Permission p) {
if (!(p instanceof FilePermission))
return false;
FilePermission that = (FilePermission) p;
// we get the effective mask. i.e., the "and" of this and that.
// They must be equal to that.mask for implies to return true.
return ((this.mask & that.mask) == that.mask) && impliesIgnoreMask(that);
}
/**
* Checks if the Permission's actions are a proper subset of the
* this object's actions. Returns the effective mask iff the
* this FilePermission's path also implies that FilePermission's path.
*
* @param that the FilePermission to check against.
* @return the effective mask
*/
boolean impliesIgnoreMask(FilePermission that) {
if (FilePermCompat.nb) {
if (this == that) {
return true;
}
if (allFiles) {
return true;
}
if (this.invalid || that.invalid) {
return false;
}
if (that.allFiles) {
return false;
}
// Left at least same level of wildness as right
if ((this.recursive && that.recursive) != that.recursive
|| (this.directory && that.directory) != that.directory) {
return false;
}
// Same npath is good as long as both or neither are directories
if (this.npath.equals(that.npath)
&& this.directory == that.directory) {
return true;
}
int diff = containsPath(this.npath, that.npath);
// Right inside left is good if recursive
if (diff >= 1 && recursive) {
return true;
}
// Right right inside left if it is element in set
if (diff == 1 && directory && !that.directory) {
return true;
}
// Hack: if a npath2 field exists, apply the same checks
// on it as a fallback.
if (this.npath2 != null) {
if (this.npath2.equals(that.npath)
&& this.directory == that.directory) {
return true;
}
diff = containsPath(this.npath2, that.npath);
if (diff >= 1 && recursive) {
return true;
}
if (diff == 1 && directory && !that.directory) {
return true;
}
}
return false;
} else {
if (this.directory) {
if (this.recursive) {
// make sure that.path is longer then path so
// something like /foo/- does not imply /foo
if (that.directory) {
return (that.cpath.length() >= this.cpath.length()) &&
that.cpath.startsWith(this.cpath);
} else {
return ((that.cpath.length() > this.cpath.length()) &&
that.cpath.startsWith(this.cpath));
}
} else {
if (that.directory) {
// if the permission passed in is a directory
// specification, make sure that a non-recursive
// permission (i.e., this object) can't imply a recursive
// permission.
if (that.recursive)
return false;
else
return (this.cpath.equals(that.cpath));
} else {
int last = that.cpath.lastIndexOf(File.separatorChar);
if (last == -1)
return false;
else {
// this.cpath.equals(that.cpath.substring(0, last+1));
// Use regionMatches to avoid creating new string
return (this.cpath.length() == (last + 1)) &&
this.cpath.regionMatches(0, that.cpath, 0, last + 1);
}
}
}
} else if (that.directory) {
// if this is NOT recursive/wildcarded,
// do not let it imply a recursive/wildcarded permission
return false;
} else {
return (this.cpath.equals(that.cpath));
}
}
}
/**
* Returns the depth between an outer path p1 and an inner path p2. -1
* is returned if
*
* - p1 does not contains p2.
* - this is not decidable. For example, p1="../x", p2="y".
* - the depth is not decidable. For example, p1="/", p2="x".
*
* This method can return 2 if the depth is greater than 2.
*
* @param p1 the expected outer path, normalized
* @param p2 the expected inner path, normalized
* @return the depth in between
*/
private static int containsPath(Path p1, Path p2) {
// Two paths must have the same root. For example,
// there is no contains relation between any two of
// "/x", "x", "C:/x", "C:x", and "//host/share/x".
if (!Objects.equals(p1.getRoot(), p2.getRoot())) {
return -1;
}
// Empty path (i.e. "." or "") is a strange beast,
// because its getNameCount()==1 but getName(0) is null.
// It's better to deal with it separately.
if (p1.equals(EMPTY_PATH)) {
if (p2.equals(EMPTY_PATH)) {
return 0;
} else if (p2.getName(0).equals(DOTDOT_PATH)) {
// "." contains p2 iff p2 has no "..". Since
// a normalized path can only have 0 or more
// ".." at the beginning. We only need to look
// at the head.
return -1;
} else {
// and the distance is p2's name count. i.e.
// 3 between "." and "a/b/c".
return p2.getNameCount();
}
} else if (p2.equals(EMPTY_PATH)) {
int c1 = p1.getNameCount();
if (!p1.getName(c1 - 1).equals(DOTDOT_PATH)) {
// "." is inside p1 iff p1 is 1 or more "..".
// For the same reason above, we only need to
// look at the tail.
return -1;
}
// and the distance is the count of ".."
return c1;
}
// Good. No more empty paths.
// Common heads are removed
int c1 = p1.getNameCount();
int c2 = p2.getNameCount();
int n = Math.min(c1, c2);
int i = 0;
while (i < n) {
if (!p1.getName(i).equals(p2.getName(i)))
break;
i++;
}
// for p1 containing p2, p1 must be 0-or-more "..",
// and p2 cannot have "..". For the same reason, we only
// check tail of p1 and head of p2.
if (i < c1 && !p1.getName(c1 - 1).equals(DOTDOT_PATH)) {
return -1;
}
if (i < c2 && p2.getName(i).equals(DOTDOT_PATH)) {
return -1;
}
// and the distance is the name counts added (after removing
// the common heads).
// For example: p1 = "../../..", p2 = "../a".
// After removing the common heads, they become "../.." and "a",
// and the distance is (3-1)+(2-1) = 3.
return c1 - i + c2 - i;
}
/**
* Checks two FilePermission objects for equality. Checks that <i>obj</i> is
* a FilePermission, and has the same pathname and actions as this object.
*
* @implNote More specifically, two pathnames are the same if and only if
* they have the same wildcard flag and their {@code cpath}
* (if {@code jdk.io.permissionsUseCanonicalPath} is {@code true}) or
* {@code npath} (if {@code jdk.io.permissionsUseCanonicalPath}
* is {@code false}) are equal. Or they are both {@literal "<<ALL FILES>>"}.
* <p>
* When {@code jdk.io.permissionsUseCanonicalPath} is {@code false}, an
* invalid {@code FilePermission} does not equal to any object except
* for itself, even if they are created using the same invalid path.
*
* @param obj the object we are testing for equality with this object.
* @return <code>true</code> if obj is a FilePermission, and has the same
* pathname and actions as this FilePermission object,
* <code>false</code> otherwise.
*/
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (! (obj instanceof FilePermission))
return false;
FilePermission that = (FilePermission) obj;
if (FilePermCompat.nb) {
if (this.invalid || that.invalid) {
return false;
}
return (this.mask == that.mask) &&
(this.allFiles == that.allFiles) &&
this.npath.equals(that.npath) &&
Objects.equals(npath2, that.npath2) &&
(this.directory == that.directory) &&
(this.recursive == that.recursive);
} else {
return (this.mask == that.mask) &&
this.cpath.equals(that.cpath) &&
(this.directory == that.directory) &&
(this.recursive == that.recursive);
}
}
/**
* Returns the hash code value for this object.
*
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
if (FilePermCompat.nb) {
return Objects.hash(
mask, allFiles, directory, recursive, npath, npath2, invalid);
} else {
return 0;
}
}
/**
* Converts an actions String to an actions mask.
*
* @param actions the action string.
* @return the actions mask.
*/
private static int getMask(String actions) {
int mask = NONE;
// Null action valid?
if (actions == null) {
return mask;
}
// Use object identity comparison against known-interned strings for
// performance benefit (these values are used heavily within the JDK).
if (actions == SecurityConstants.FILE_READ_ACTION) {
return READ;
} else if (actions == SecurityConstants.FILE_WRITE_ACTION) {
return WRITE;
} else if (actions == SecurityConstants.FILE_EXECUTE_ACTION) {
return EXECUTE;
} else if (actions == SecurityConstants.FILE_DELETE_ACTION) {
return DELETE;
} else if (actions == SecurityConstants.FILE_READLINK_ACTION) {
return READLINK;
}
char[] a = actions.toCharArray();
int i = a.length - 1;
if (i < 0)
return mask;
while (i != -1) {
char c;
// skip whitespace
while ((i!=-1) && ((c = a[i]) == ' ' ||
c == '\r' ||
c == '\n' ||
c == '\f' ||
c == '\t'))
i--;
// check for the known strings
int matchlen;
if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') &&
(a[i-2] == 'e' || a[i-2] == 'E') &&
(a[i-1] == 'a' || a[i-1] == 'A') &&
(a[i] == 'd' || a[i] == 'D'))
{
matchlen = 4;
mask |= READ;
} else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') &&
(a[i-3] == 'r' || a[i-3] == 'R') &&
(a[i-2] == 'i' || a[i-2] == 'I') &&
(a[i-1] == 't' || a[i-1] == 'T') &&
(a[i] == 'e' || a[i] == 'E'))
{
matchlen = 5;
mask |= WRITE;
} else if (i >= 6 && (a[i-6] == 'e' || a[i-6] == 'E') &&
(a[i-5] == 'x' || a[i-5] == 'X') &&
(a[i-4] == 'e' || a[i-4] == 'E') &&
(a[i-3] == 'c' || a[i-3] == 'C') &&
(a[i-2] == 'u' || a[i-2] == 'U') &&
(a[i-1] == 't' || a[i-1] == 'T') &&
(a[i] == 'e' || a[i] == 'E'))
{
matchlen = 7;
mask |= EXECUTE;
} else if (i >= 5 && (a[i-5] == 'd' || a[i-5] == 'D') &&
(a[i-4] == 'e' || a[i-4] == 'E') &&
(a[i-3] == 'l' || a[i-3] == 'L') &&
(a[i-2] == 'e' || a[i-2] == 'E') &&
(a[i-1] == 't' || a[i-1] == 'T') &&
(a[i] == 'e' || a[i] == 'E'))
{
matchlen = 6;
mask |= DELETE;
} else if (i >= 7 && (a[i-7] == 'r' || a[i-7] == 'R') &&
(a[i-6] == 'e' || a[i-6] == 'E') &&
(a[i-5] == 'a' || a[i-5] == 'A') &&
(a[i-4] == 'd' || a[i-4] == 'D') &&
(a[i-3] == 'l' || a[i-3] == 'L') &&
(a[i-2] == 'i' || a[i-2] == 'I') &&
(a[i-1] == 'n' || a[i-1] == 'N') &&
(a[i] == 'k' || a[i] == 'K'))
{
matchlen = 8;
mask |= READLINK;
} else {
// parse error
throw new IllegalArgumentException(
"invalid permission: " + actions);
}
// make sure we didn't just match the tail of a word
// like "ackbarfaccept". Also, skip to the comma.
boolean seencomma = false;
while (i >= matchlen && !seencomma) {
switch(a[i-matchlen]) {
case ',':
seencomma = true;
break;
case ' ': case '\r': case '\n':
case '\f': case '\t':
break;
default:
throw new IllegalArgumentException(
"invalid permission: " + actions);
}
i--;
}
// point i at the location of the comma minus one (or -1).
i -= matchlen;
}
return mask;
}
/**
* Return the current action mask. Used by the FilePermissionCollection.
*
* @return the actions mask.
*/
int getMask() {
return mask;
}
/**
* Return the canonical string representation of the actions.
* Always returns present actions in the following order:
* read, write, execute, delete, readlink.
*
* @return the canonical string representation of the actions.
*/
private static String getActions(int mask) {
StringJoiner sj = new StringJoiner(",");
if ((mask & READ) == READ) {
sj.add("read");
}
if ((mask & WRITE) == WRITE) {
sj.add("write");
}
if ((mask & EXECUTE) == EXECUTE) {
sj.add("execute");
}
if ((mask & DELETE) == DELETE) {
sj.add("delete");
}
if ((mask & READLINK) == READLINK) {
sj.add("readlink");
}
return sj.toString();
}
/**
* Returns the "canonical string representation" of the actions.
* That is, this method always returns present actions in the following order:
* read, write, execute, delete, readlink. For example, if this FilePermission
* object allows both write and read actions, a call to <code>getActions</code>
* will return the string "read,write".
*
* @return the canonical string representation of the actions.
*/
@Override
public String getActions() {
if (actions == null)
actions = getActions(this.mask);
return actions;
}
/**
* Returns a new PermissionCollection object for storing FilePermission
* objects.
* <p>
* FilePermission objects must be stored in a manner that allows them
* to be inserted into the collection in any order, but that also enables the
* PermissionCollection <code>implies</code>
* method to be implemented in an efficient (and consistent) manner.
*
* <p>For example, if you have two FilePermissions:
* <OL>
* <LI> <code>"/tmp/-", "read"</code>
* <LI> <code>"/tmp/scratch/foo", "write"</code>
* </OL>
*
* <p>and you are calling the <code>implies</code> method with the FilePermission:
*
* <pre>
* "/tmp/scratch/foo", "read,write",
* </pre>
*
* then the <code>implies</code> function must
* take into account both the "/tmp/-" and "/tmp/scratch/foo"
* permissions, so the effective permission is "read,write",
* and <code>implies</code> returns true. The "implies" semantics for
* FilePermissions are handled properly by the PermissionCollection object
* returned by this <code>newPermissionCollection</code> method.
*
* @return a new PermissionCollection object suitable for storing
* FilePermissions.
*/
@Override
public PermissionCollection newPermissionCollection() {
return new FilePermissionCollection();
}
/**
* WriteObject is called to save the state of the FilePermission
* to a stream. The actions are serialized, and the superclass
* takes care of the name.
*/
private void writeObject(ObjectOutputStream s)
throws IOException
{
// Write out the actions. The superclass takes care of the name
// call getActions to make sure actions field is initialized
if (actions == null)
getActions();
s.defaultWriteObject();
}
/**
* readObject is called to restore the state of the FilePermission from
* a stream.
*/
private void readObject(ObjectInputStream s)
throws IOException, ClassNotFoundException
{
// Read in the actions, then restore everything else by calling init.
s.defaultReadObject();
init(getMask(actions));
}
/**
* Create a cloned FilePermission with a different actions.
* @param effective the new actions
* @return a new object
*/
FilePermission withNewActions(int effective) {
return new FilePermission(this.getName(),
this,
this.npath,
this.npath2,
effective,
null);
}
}
/**
* A FilePermissionCollection stores a set of FilePermission permissions.
* FilePermission objects
* must be stored in a manner that allows them to be inserted in any
* order, but enable the implies function to evaluate the implies
* method.
* For example, if you have two FilePermissions:
* <OL>
* <LI> "/tmp/-", "read"
* <LI> "/tmp/scratch/foo", "write"
* </OL>
* And you are calling the implies function with the FilePermission:
* "/tmp/scratch/foo", "read,write", then the implies function must
* take into account both the /tmp/- and /tmp/scratch/foo
* permissions, so the effective permission is "read,write".
*
* @see java.security.Permission
* @see java.security.Permissions
* @see java.security.PermissionCollection
*
*
* @author Marianne Mueller
* @author Roland Schemers
*
* @serial include
*
*/
final class FilePermissionCollection extends PermissionCollection
implements Serializable
{
// Not serialized; see serialization section at end of class
private transient ConcurrentHashMap<String, Permission> perms;
/**
* Create an empty FilePermissionCollection object.
*/
public FilePermissionCollection() {
perms = new ConcurrentHashMap<>();
}
/**
* Adds a permission to the FilePermissionCollection. The key for the hash is
* permission.path.
*
* @param permission the Permission object to add.
*
* @exception IllegalArgumentException - if the permission is not a
* FilePermission
*
* @exception SecurityException - if this FilePermissionCollection object
* has been marked readonly
*/
@Override
public void add(Permission permission) {
if (! (permission instanceof FilePermission))
throw new IllegalArgumentException("invalid permission: "+
permission);
if (isReadOnly())
throw new SecurityException(
"attempt to add a Permission to a readonly PermissionCollection");
FilePermission fp = (FilePermission)permission;
// Add permission to map if it is absent, or replace with new
// permission if applicable.
perms.merge(fp.getName(), fp,
new java.util.function.BiFunction<>() {
@Override
public Permission apply(Permission existingVal,
Permission newVal) {
int oldMask = ((FilePermission)existingVal).getMask();
int newMask = ((FilePermission)newVal).getMask();
if (oldMask != newMask) {
int effective = oldMask | newMask;
if (effective == newMask) {
return newVal;
}
if (effective != oldMask) {
return ((FilePermission)newVal)
.withNewActions(effective);
}
}
return existingVal;
}
}
);
}
/**
* Check and see if this set of permissions implies the permissions
* expressed in "permission".
*
* @param permission the Permission object to compare
*
* @return true if "permission" is a proper subset of a permission in
* the set, false if not.
*/
@Override
public boolean implies(Permission permission) {
if (! (permission instanceof FilePermission))
return false;
FilePermission fperm = (FilePermission) permission;
int desired = fperm.getMask();
int effective = 0;
int needed = desired;
for (Permission perm : perms.values()) {
FilePermission fp = (FilePermission)perm;
if (((needed & fp.getMask()) != 0) && fp.impliesIgnoreMask(fperm)) {
effective |= fp.getMask();
if ((effective & desired) == desired) {
return true;
}
needed = (desired ^ effective);
}
}
return false;
}
/**
* Returns an enumeration of all the FilePermission objects in the
* container.
*
* @return an enumeration of all the FilePermission objects.
*/
@Override
public Enumeration<Permission> elements() {
return perms.elements();
}
private static final long serialVersionUID = 2202956749081564585L;
// Need to maintain serialization interoperability with earlier releases,
// which had the serializable field:
// private Vector permissions;
/**
* @serialField permissions java.util.Vector
* A list of FilePermission objects.
*/
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("permissions", Vector.class),
};
/**
* @serialData "permissions" field (a Vector containing the FilePermissions).
*/
/*
* Writes the contents of the perms field out as a Vector for
* serialization compatibility with earlier releases.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
// Don't call out.defaultWriteObject()
// Write out Vector
Vector<Permission> permissions = new Vector<>(perms.values());
ObjectOutputStream.PutField pfields = out.putFields();
pfields.put("permissions", permissions);
out.writeFields();
}
/*
* Reads in a Vector of FilePermissions and saves them in the perms field.
*/
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException
{
// Don't call defaultReadObject()
// Read in serialized fields
ObjectInputStream.GetField gfields = in.readFields();
// Get the one we want
@SuppressWarnings("unchecked")
Vector<Permission> permissions = (Vector<Permission>)gfields.get("permissions", null);
perms = new ConcurrentHashMap<>(permissions.size());
for (Permission perm : permissions) {
perms.put(perm.getName(), perm);
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/stefanpy/Java12Source.git
git@gitee.com:stefanpy/Java12Source.git
stefanpy
Java12Source
Java12Source
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /