Skip to content

Commit f8a98a3

Browse files
authored
bugfix: correct validation of empty segments in ARN parser (#798)
1 parent ebc8747 commit f8a98a3

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "a4579c64-4158-47f0-86a2-c62d7d523529",
3+
"type": "bugfix",
4+
"description": "Correct validation of empty segments in ARN parser"
5+
}

aws-runtime/aws-endpoint/common/src/aws/sdk/kotlin/runtime/endpoint/functions/Functions.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import aws.smithy.kotlin.runtime.util.net.isIpv6
1313
// the number of top-level components an arn contains (separated by colons)
1414
private const val ARN_COMPONENT_COUNT = 6
1515

16+
private const val ARN_INDEX_PARTITION = 1
17+
private const val ARN_INDEX_VENDOR = 2
18+
private const val ARN_INDEX_REGION = 3
19+
private const val ARN_INDEX_NAMESPACE = 4
20+
private const val ARN_INDEX_RELATIVE_ID = 5
21+
1622
/**
1723
* Identifies the partition for the given AWS region.
1824
*/
@@ -83,14 +89,14 @@ public fun parseArn(value: String?): Arn? =
8389
val split = it.split(':', limit = ARN_COMPONENT_COUNT)
8490
if (split[0] != "arn") return null
8591
if (split.size != ARN_COMPONENT_COUNT) return null
86-
if (split[5] == "") return null
92+
if (split[ARN_INDEX_PARTITION].isEmpty() || split[ARN_INDEX_VENDOR].isEmpty() || split[ARN_INDEX_RELATIVE_ID].isEmpty()) return null
8793

8894
return Arn(
89-
split[1],
90-
split[2],
91-
split[3],
92-
split[4],
93-
split[5].split(':', '/'),
95+
split[ARN_INDEX_PARTITION],
96+
split[ARN_INDEX_VENDOR],
97+
split[ARN_INDEX_REGION],
98+
split[ARN_INDEX_NAMESPACE],
99+
split[ARN_INDEX_RELATIVE_ID].split(':', '/'),
94100
)
95101
}
96102

aws-runtime/aws-endpoint/common/test/aws/sdk/kotlin/runtime/endpoint/functions/FunctionsTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ class FunctionsTest {
5858
fun testParseInvalidLengthArn() =
5959
assertNull(parseArn("arn:aws"))
6060

61+
@Test
62+
fun testParseInvalidArnNoPartition() =
63+
assertNull(
64+
parseArn("arn::service:region:account-id:resource-type/resource-id"),
65+
)
66+
67+
@Test
68+
fun testParseInvalidArnNoService() =
69+
assertNull(
70+
parseArn("arn:partition::region:account-id:resource-type/resource-id"),
71+
)
72+
73+
@Test
74+
fun testParseInvalidArnNoResource() =
75+
assertNull(
76+
parseArn("arn:partition:service:region:account-id:"),
77+
)
78+
6179
@Test
6280
fun testParseArn() =
6381
assertEquals(

0 commit comments

Comments
 (0)