package eu.friedcutlet.yokan.services.extensions.recipe import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertIs import kotlin.test.assertNotEquals import kotlin.test.assertFalse class DeclarativeExtensionRecipeTest { @Test fun acceptsEntrypointBundleFromDeclaredOrigin() { val accepted = assertIs(StrictRecipeBundleDecoder.decode(validBundle().encodeToByteArray())) assertEquals("primary", accepted.bundle.entrypoints.getValue("popular").url.origin.originId) assertEquals("https://example.test", accepted.bundle.manifest.declaredOrigins.single().origin) } @Test fun rejectsInvalidOriginsUnsafeUrlComponentsAndMissingCapabilities() { listOf( "http://example.test", "https://*.example.test", "https://user@example.test", "https://example.test/path", "https://example.test?query", "https://example.test#fragment", "https://EXAMPLE.test", "https://example.test:443", "https://192.0.2.1", ).forEach { origin -> assertRejected(validBundle().replace("https://example.test", origin), RecipeBundleErrorCode.InvalidValue) } assertRejected(validBundle().replace("\"originId\":\"primary\"", "\"originId\":\"missing\""), RecipeBundleErrorCode.InvalidValue) assertRejected(validBundle().replace("\"text\":\"/items\"", "\"text\":\"//attacker.test\""), RecipeBundleErrorCode.InvalidValue) assertRejected(validBundle().replace("\"text\":\"/items\"", "\"text\":\"/items#fragment\""), RecipeBundleErrorCode.InvalidValue) assertRejected(validBundle().replace("[\"networkRead\",\"documentExtraction\"]", "[\"documentExtraction\"]"), RecipeBundleErrorCode.InvalidValue) } @Test fun rejectsUnknownFieldsOperationsVersionsAndAuthorityDataWithoutLeakingMarkers() { val marker = "ATTACKER_MARKER_6af27" val rejected = assertIs(StrictRecipeBundleDecoder.decode( validBundle().replace("\"name\":\"Example\"", "\"name\":\"Example\",\"$marker\":\"$marker\"").encodeToByteArray(), )) assertEquals(RecipeBundleErrorCode.UnknownFieldOrOperation, rejected.error.code) assertFalse(rejected.error.detail.contains(marker)) assertRejected(validBundle().replace("\"literal\"", "\"executeJavaScript\""), RecipeBundleErrorCode.UnknownFieldOrOperation) } @Test fun scannerRejectsEscapedAndNestedDuplicatesAndDuplicateDiscriminators() { assertRejected(validBundle().replace("\"id\":\"example.source\"", "\"id\":\"example.source\",\"\\u0069d\":\"other\""), RecipeBundleErrorCode.DuplicateJsonKey) assertRejected(validBundle().replace("\"originId\":\"primary\"", "\"originId\":\"primary\",\"\\u006friginId\":\"other\""), RecipeBundleErrorCode.DuplicateJsonKey) assertRejected(validBundle().replaceFirst("\"value\":\"literal\"", "\"value\":\"literal\",\"value\":\"hostInput\""), RecipeBundleErrorCode.DuplicateJsonKey) } @Test fun scannerRejectsInvalidEncodingMalformedTokensTrailingCommaAndMultipleRoots() { assertRejected(byteArrayOf('{'.code.toByte(), '"'.code.toByte(), 'x'.code.toByte(), '"'.code.toByte(), ':'.code.toByte(), 0xC3.toByte()), RecipeBundleErrorCode.InvalidJson) listOf("{\"x\":\"\\u12\"}", "{\"x\":\"\\q\"}", "{\"x\":01}", "{\"x\":1e}", "{\"x\":-}").forEach { assertRejected(it, RecipeBundleErrorCode.InvalidJson) } assertRejected(validBundle().replace("\"documentExtraction\"]", "\"documentExtraction\",]"), RecipeBundleErrorCode.InvalidJson) assertRejected(validBundle() + validBundle(), RecipeBundleErrorCode.InvalidJson) } @Test fun scannerHonorsExactAndBeyondStringCollectionAndDepthLimits() { assertIs(StrictRecipeBundleDecoder.decode(validBundle().replace("\"Example\"", "\"${"a".repeat(RecipeBundleLimits.maxStringCharacters)}\"").encodeToByteArray())) assertRejected(validBundle().replace("\"Example\"", "\"${"a".repeat(RecipeBundleLimits.maxStringCharacters + 1)}\""), RecipeBundleErrorCode.StringLimitExceeded) val exactCapabilities = List(RecipeBundleLimits.maxCollectionEntries) { if (it == 0) "\"documentExtraction\"" else "\"networkRead\"" }.joinToString(",") assertIs(StrictRecipeBundleDecoder.decode(validBundle().replace("\"networkRead\",\"documentExtraction\"", exactCapabilities).encodeToByteArray())) val beyondCapabilities = List(RecipeBundleLimits.maxCollectionEntries + 1) { "\"networkRead\"" }.joinToString(",") assertRejected(validBundle().replace("\"networkRead\",\"documentExtraction\"", beyondCapabilities), RecipeBundleErrorCode.CollectionLimitExceeded) val exactDepth = "[".repeat(RecipeBundleLimits.maxJsonDepth) + "0" + "]".repeat(RecipeBundleLimits.maxJsonDepth) assertNotEquals(RecipeBundleErrorCode.NestingLimitExceeded, errorFor(exactDepth)) val beyondDepth = "[".repeat(RecipeBundleLimits.maxJsonDepth + 1) + "0" + "]".repeat(RecipeBundleLimits.maxJsonDepth + 1) assertRejected(beyondDepth, RecipeBundleErrorCode.NestingLimitExceeded) } @Test fun legacyStepFormatVersionsAreRejectedAtValidationInsteadOfInstallingDeadRecipes() { listOf("1", "2").forEach { version -> assertRejected(validBundle().replace("\"formatVersion\":\"2.1\"", "\"formatVersion\":\"$version\""), RecipeBundleErrorCode.UnsupportedFormatVersion) } } @Test fun nonEmptyLegacyStepsAreRejected() { assertRejected(validBundle().replace("\"steps\":[]", "\"steps\":[{\"operation\":\"buildUrl\"}]"), RecipeBundleErrorCode.InvalidValue) } private fun assertRejected(json: String, expected: RecipeBundleErrorCode) = assertRejected(json.encodeToByteArray(), expected) private fun assertRejected(bytes: ByteArray, expected: RecipeBundleErrorCode) = assertEquals(expected, errorFor(bytes)) private fun errorFor(json: String): RecipeBundleErrorCode = errorFor(json.encodeToByteArray()) private fun errorFor(bytes: ByteArray): RecipeBundleErrorCode = errorFor(StrictRecipeBundleDecoder.decode(bytes)) private fun errorFor(result: RecipeBundleDecodeResult): RecipeBundleErrorCode = assertIs(result).error.code } internal fun validBundle(id: String = "example.source"): String = """{"manifest":{"formatVersion":"2.1","id":"$id","name":"Example","declaredCapabilities":["networkRead","documentExtraction"],"declaredOrigins":[{"id":"primary","origin":"https://example.test"}]},"recipe":{"steps":[]},"entrypoints":{"popular":{"inputs":["pageIndex"],"url":{"origin":{"originId":"primary"},"path":[{"value":"literal","text":"/items"}]},"projection":{"pointer":"/data","mode":"many","fields":{"id":"/id","title":"/title"}},"terminal":{"type":"listing","pageSize":20}}}}"""