Skip to content

Commit 6137e42

Browse files
authored
Core: Prevent AIOOBE for negative codes in PolarisEntityType, PolarisPrivilege, ReturnStatus (#2490)
1 parent f41d5bf commit 6137e42

File tree

6 files changed

+237
-3
lines changed

6 files changed

+237
-3
lines changed

polaris-core/src/main/java/org/apache/polaris/core/entity/PolarisEntityType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public boolean isParentSelfReference() {
111111
@JsonCreator
112112
public static @Nullable PolarisEntityType fromCode(int entityTypeCode) {
113113
// ensure it is within bounds
114-
if (entityTypeCode >= REVERSE_MAPPING_ARRAY.length) {
114+
if (entityTypeCode < 0 || entityTypeCode >= REVERSE_MAPPING_ARRAY.length) {
115115
return null;
116116
}
117117

polaris-core/src/main/java/org/apache/polaris/core/entity/PolarisPrivilege.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public int getCode() {
272272
@JsonCreator
273273
public static @Nullable PolarisPrivilege fromCode(int code) {
274274
// ensure it is within bounds
275-
if (code >= REVERSE_MAPPING_ARRAY.length) {
275+
if (code < 0 || code >= REVERSE_MAPPING_ARRAY.length) {
276276
return null;
277277
}
278278

polaris-core/src/main/java/org/apache/polaris/core/persistence/dao/entity/BaseResult.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ int getCode() {
156156
}
157157

158158
static ReturnStatus getStatus(int code) {
159-
return code >= REVERSE_MAPPING_ARRAY.length ? null : REVERSE_MAPPING_ARRAY[code];
159+
return (code < 0 || code >= REVERSE_MAPPING_ARRAY.length)
160+
? null
161+
: REVERSE_MAPPING_ARRAY[code];
160162
}
161163
}
162164
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.polaris.core.entity;
20+
21+
import java.util.stream.Stream;
22+
import org.assertj.core.api.Assertions;
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.Arguments;
25+
import org.junit.jupiter.params.provider.MethodSource;
26+
27+
public class PolarisEntityTypeTest {
28+
29+
static Stream<Arguments> entityTypes() {
30+
return Stream.of(
31+
Arguments.of(-1, null),
32+
Arguments.of(0, PolarisEntityType.NULL_TYPE),
33+
Arguments.of(1, PolarisEntityType.ROOT),
34+
Arguments.of(2, PolarisEntityType.PRINCIPAL),
35+
Arguments.of(3, PolarisEntityType.PRINCIPAL_ROLE),
36+
Arguments.of(4, PolarisEntityType.CATALOG),
37+
Arguments.of(5, PolarisEntityType.CATALOG_ROLE),
38+
Arguments.of(6, PolarisEntityType.NAMESPACE),
39+
Arguments.of(7, PolarisEntityType.TABLE_LIKE),
40+
Arguments.of(8, PolarisEntityType.TASK),
41+
Arguments.of(9, PolarisEntityType.FILE),
42+
Arguments.of(10, PolarisEntityType.POLICY),
43+
Arguments.of(11, null));
44+
}
45+
46+
@ParameterizedTest
47+
@MethodSource("entityTypes")
48+
public void testFromCode(int code, PolarisEntityType expected) {
49+
Assertions.assertThat(PolarisEntityType.fromCode(code)).isEqualTo(expected);
50+
}
51+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.polaris.core.entity;
20+
21+
import java.util.stream.Stream;
22+
import org.assertj.core.api.Assertions;
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.Arguments;
25+
import org.junit.jupiter.params.provider.MethodSource;
26+
27+
public class PolarisPrivilegeTest {
28+
29+
static Stream<Arguments> polarisPrivileges() {
30+
return Stream.of(
31+
Arguments.of(-1, null),
32+
Arguments.of(1, PolarisPrivilege.SERVICE_MANAGE_ACCESS),
33+
Arguments.of(2, PolarisPrivilege.CATALOG_MANAGE_ACCESS),
34+
Arguments.of(3, PolarisPrivilege.CATALOG_ROLE_USAGE),
35+
Arguments.of(4, PolarisPrivilege.PRINCIPAL_ROLE_USAGE),
36+
Arguments.of(5, PolarisPrivilege.NAMESPACE_CREATE),
37+
Arguments.of(6, PolarisPrivilege.TABLE_CREATE),
38+
Arguments.of(7, PolarisPrivilege.VIEW_CREATE),
39+
Arguments.of(8, PolarisPrivilege.NAMESPACE_DROP),
40+
Arguments.of(9, PolarisPrivilege.TABLE_DROP),
41+
Arguments.of(10, PolarisPrivilege.VIEW_DROP),
42+
Arguments.of(11, PolarisPrivilege.NAMESPACE_LIST),
43+
Arguments.of(12, PolarisPrivilege.TABLE_LIST),
44+
Arguments.of(13, PolarisPrivilege.VIEW_LIST),
45+
Arguments.of(14, PolarisPrivilege.NAMESPACE_READ_PROPERTIES),
46+
Arguments.of(15, PolarisPrivilege.TABLE_READ_PROPERTIES),
47+
Arguments.of(16, PolarisPrivilege.VIEW_READ_PROPERTIES),
48+
Arguments.of(17, PolarisPrivilege.NAMESPACE_WRITE_PROPERTIES),
49+
Arguments.of(18, PolarisPrivilege.TABLE_WRITE_PROPERTIES),
50+
Arguments.of(19, PolarisPrivilege.VIEW_WRITE_PROPERTIES),
51+
Arguments.of(20, PolarisPrivilege.TABLE_READ_DATA),
52+
Arguments.of(21, PolarisPrivilege.TABLE_WRITE_DATA),
53+
Arguments.of(22, PolarisPrivilege.NAMESPACE_FULL_METADATA),
54+
Arguments.of(23, PolarisPrivilege.TABLE_FULL_METADATA),
55+
Arguments.of(24, PolarisPrivilege.VIEW_FULL_METADATA),
56+
Arguments.of(25, PolarisPrivilege.CATALOG_CREATE),
57+
Arguments.of(26, PolarisPrivilege.CATALOG_DROP),
58+
Arguments.of(27, PolarisPrivilege.CATALOG_LIST),
59+
Arguments.of(28, PolarisPrivilege.CATALOG_READ_PROPERTIES),
60+
Arguments.of(29, PolarisPrivilege.CATALOG_WRITE_PROPERTIES),
61+
Arguments.of(30, PolarisPrivilege.CATALOG_FULL_METADATA),
62+
Arguments.of(31, PolarisPrivilege.CATALOG_MANAGE_METADATA),
63+
Arguments.of(32, PolarisPrivilege.CATALOG_MANAGE_CONTENT),
64+
Arguments.of(33, PolarisPrivilege.PRINCIPAL_LIST_GRANTS),
65+
Arguments.of(34, PolarisPrivilege.PRINCIPAL_ROLE_LIST_GRANTS),
66+
Arguments.of(35, PolarisPrivilege.CATALOG_ROLE_LIST_GRANTS),
67+
Arguments.of(36, PolarisPrivilege.CATALOG_LIST_GRANTS),
68+
Arguments.of(37, PolarisPrivilege.NAMESPACE_LIST_GRANTS),
69+
Arguments.of(38, PolarisPrivilege.TABLE_LIST_GRANTS),
70+
Arguments.of(39, PolarisPrivilege.VIEW_LIST_GRANTS),
71+
Arguments.of(40, PolarisPrivilege.CATALOG_MANAGE_GRANTS_ON_SECURABLE),
72+
Arguments.of(41, PolarisPrivilege.NAMESPACE_MANAGE_GRANTS_ON_SECURABLE),
73+
Arguments.of(42, PolarisPrivilege.TABLE_MANAGE_GRANTS_ON_SECURABLE),
74+
Arguments.of(43, PolarisPrivilege.VIEW_MANAGE_GRANTS_ON_SECURABLE),
75+
Arguments.of(44, PolarisPrivilege.PRINCIPAL_CREATE),
76+
Arguments.of(45, PolarisPrivilege.PRINCIPAL_DROP),
77+
Arguments.of(46, PolarisPrivilege.PRINCIPAL_LIST),
78+
Arguments.of(47, PolarisPrivilege.PRINCIPAL_READ_PROPERTIES),
79+
Arguments.of(48, PolarisPrivilege.PRINCIPAL_WRITE_PROPERTIES),
80+
Arguments.of(49, PolarisPrivilege.PRINCIPAL_FULL_METADATA),
81+
Arguments.of(50, PolarisPrivilege.PRINCIPAL_MANAGE_GRANTS_ON_SECURABLE),
82+
Arguments.of(51, PolarisPrivilege.PRINCIPAL_MANAGE_GRANTS_FOR_GRANTEE),
83+
Arguments.of(52, PolarisPrivilege.PRINCIPAL_ROTATE_CREDENTIALS),
84+
Arguments.of(53, PolarisPrivilege.PRINCIPAL_RESET_CREDENTIALS),
85+
Arguments.of(54, PolarisPrivilege.PRINCIPAL_ROLE_CREATE),
86+
Arguments.of(55, PolarisPrivilege.PRINCIPAL_ROLE_DROP),
87+
Arguments.of(56, PolarisPrivilege.PRINCIPAL_ROLE_LIST),
88+
Arguments.of(57, PolarisPrivilege.PRINCIPAL_ROLE_READ_PROPERTIES),
89+
Arguments.of(58, PolarisPrivilege.PRINCIPAL_ROLE_WRITE_PROPERTIES),
90+
Arguments.of(59, PolarisPrivilege.PRINCIPAL_ROLE_FULL_METADATA),
91+
Arguments.of(60, PolarisPrivilege.PRINCIPAL_ROLE_MANAGE_GRANTS_ON_SECURABLE),
92+
Arguments.of(61, PolarisPrivilege.PRINCIPAL_ROLE_MANAGE_GRANTS_FOR_GRANTEE),
93+
Arguments.of(62, PolarisPrivilege.CATALOG_ROLE_CREATE),
94+
Arguments.of(63, PolarisPrivilege.CATALOG_ROLE_DROP),
95+
Arguments.of(64, PolarisPrivilege.CATALOG_ROLE_LIST),
96+
Arguments.of(65, PolarisPrivilege.CATALOG_ROLE_READ_PROPERTIES),
97+
Arguments.of(66, PolarisPrivilege.CATALOG_ROLE_WRITE_PROPERTIES),
98+
Arguments.of(67, PolarisPrivilege.CATALOG_ROLE_FULL_METADATA),
99+
Arguments.of(68, PolarisPrivilege.CATALOG_ROLE_MANAGE_GRANTS_ON_SECURABLE),
100+
Arguments.of(69, PolarisPrivilege.CATALOG_ROLE_MANAGE_GRANTS_FOR_GRANTEE),
101+
Arguments.of(70, PolarisPrivilege.POLICY_CREATE),
102+
Arguments.of(71, PolarisPrivilege.POLICY_READ),
103+
Arguments.of(72, PolarisPrivilege.POLICY_DROP),
104+
Arguments.of(73, PolarisPrivilege.POLICY_WRITE),
105+
Arguments.of(74, PolarisPrivilege.POLICY_LIST),
106+
Arguments.of(75, PolarisPrivilege.POLICY_FULL_METADATA),
107+
Arguments.of(76, PolarisPrivilege.POLICY_ATTACH),
108+
Arguments.of(77, PolarisPrivilege.POLICY_DETACH),
109+
Arguments.of(78, PolarisPrivilege.CATALOG_ATTACH_POLICY),
110+
Arguments.of(79, PolarisPrivilege.NAMESPACE_ATTACH_POLICY),
111+
Arguments.of(80, PolarisPrivilege.TABLE_ATTACH_POLICY),
112+
Arguments.of(81, PolarisPrivilege.CATALOG_DETACH_POLICY),
113+
Arguments.of(82, PolarisPrivilege.NAMESPACE_DETACH_POLICY),
114+
Arguments.of(83, PolarisPrivilege.TABLE_DETACH_POLICY),
115+
Arguments.of(84, PolarisPrivilege.POLICY_MANAGE_GRANTS_ON_SECURABLE),
116+
Arguments.of(85, null));
117+
}
118+
119+
@ParameterizedTest
120+
@MethodSource("polarisPrivileges")
121+
public void testFromCode(int code, PolarisPrivilege expected) {
122+
Assertions.assertThat(PolarisPrivilege.fromCode(code)).isEqualTo(expected);
123+
}
124+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.polaris.core.persistence.dao.entity;
20+
21+
import java.util.stream.Stream;
22+
import org.assertj.core.api.Assertions;
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.Arguments;
25+
import org.junit.jupiter.params.provider.MethodSource;
26+
27+
public class ReturnStatusTest {
28+
29+
static Stream<Arguments> returnStatuses() {
30+
return Stream.of(
31+
Arguments.of(-1, null),
32+
Arguments.of(1, BaseResult.ReturnStatus.SUCCESS),
33+
Arguments.of(2, BaseResult.ReturnStatus.UNEXPECTED_ERROR_SIGNALED),
34+
Arguments.of(3, BaseResult.ReturnStatus.CATALOG_PATH_CANNOT_BE_RESOLVED),
35+
Arguments.of(4, BaseResult.ReturnStatus.ENTITY_CANNOT_BE_RESOLVED),
36+
Arguments.of(5, BaseResult.ReturnStatus.ENTITY_NOT_FOUND),
37+
Arguments.of(6, BaseResult.ReturnStatus.GRANT_NOT_FOUND),
38+
Arguments.of(7, BaseResult.ReturnStatus.ENTITY_ALREADY_EXISTS),
39+
Arguments.of(8, BaseResult.ReturnStatus.ENTITY_UNDROPPABLE),
40+
Arguments.of(9, BaseResult.ReturnStatus.NAMESPACE_NOT_EMPTY),
41+
Arguments.of(10, BaseResult.ReturnStatus.CATALOG_NOT_EMPTY),
42+
Arguments.of(11, BaseResult.ReturnStatus.TARGET_ENTITY_CONCURRENTLY_MODIFIED),
43+
Arguments.of(12, BaseResult.ReturnStatus.ENTITY_CANNOT_BE_RENAMED),
44+
Arguments.of(13, BaseResult.ReturnStatus.SUBSCOPE_CREDS_ERROR),
45+
Arguments.of(14, BaseResult.ReturnStatus.POLICY_MAPPING_NOT_FOUND),
46+
Arguments.of(15, BaseResult.ReturnStatus.POLICY_MAPPING_OF_SAME_TYPE_ALREADY_EXISTS),
47+
Arguments.of(16, BaseResult.ReturnStatus.POLICY_HAS_MAPPINGS),
48+
Arguments.of(17, null));
49+
}
50+
51+
@ParameterizedTest
52+
@MethodSource("returnStatuses")
53+
public void testReturnStatusFromCode(int code, BaseResult.ReturnStatus expected) {
54+
BaseResult.ReturnStatus actual = BaseResult.ReturnStatus.getStatus(code);
55+
Assertions.assertThat(actual).isEqualTo(expected);
56+
}
57+
}

0 commit comments

Comments
 (0)