Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_HeadersCommonKt.headersCheckValue caused by IllegalArgumentException - Unexpected char <address> at 45 in x-amz-meta-text #2657

Closed
1 task done
yaroslav-v opened this issue Dec 18, 2023 · 8 comments
Labels
bug Something isn't working closing soon This issue will be closed in 7 days unless further comments are made. storage Related to the Storage category/plugins

Comments

@yaroslav-v
Copy link

Before opening, please confirm:

Language and Async Model

Java

Amplify Categories

Storage

Gradle script dependencies

// Put output below this line
implementation "com.amplifyframework:core:2.14.5"
implementation "com.amplifyframework:aws-storage-s3:2.14.5"
implementation "com.amplifyframework:aws-auth-cognito:2.14.5"

Environment information

# Put output below this line
------------------------------------------------------------
Gradle 8.1.1
------------------------------------------------------------

Build time:   2023-04-21 12:31:26 UTC
Revision:     1cf537a851c635c364a4214885f8b9798051175b

Kotlin:       1.8.10
Groovy:       3.0.15
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          1.8.0_201 (Oracle Corporation 25.201-b09)
OS:           Mac OS X 10.16 x86_64

Please include any relevant guides or documentation you're referencing

No response

Describe the bug

Hi there!

A part of our users experience issues with uploading images to AWS. The issue appears on all Android version from 10 to 14, affected devices include Samsung, Motorola, Oneplus, Google.

As far as I can see, the file name might contain some emoji symbols. For example:
Caused by java.lang.IllegalArgumentException
Unexpected char 0xd83d at 0 in x-amz-meta-text value: 😏

or
Caused by java.lang.IllegalArgumentException
Unexpected char 0xd83e at 6 in x-amz-meta-name value: hayden🩷

etc.

You can find a crashlog from Firebase in the attachment.

Previously, we've used AWS Amplify v1.x and there were no such issues. So, I'm not sure what's correct way to handle this issue. Should it be handled by Amplify SDK or you have some recommendations for the data?

Reproduction steps (if applicable)

No response

Code Snippet

// Put your code below this line.
// Initialization in Application class

Amplify.Logging.disable();

Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.addPlugin(new AWSS3StoragePlugin());

AmplifyConfiguration amplifyConfig = AmplifyConfiguration.builder(context)
		.devMenuEnabled(false)
		.build();
Amplify.configure(amplifyConfig, context);


// Uploading, somewhere in the app

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

Map<String, String> metadata = new ArrayMap<>(4);
metadata.put("date", dateFormat.format(date));
metadata.put("session_uuid", uid);
metadata.put("token", authToken);
metadata.put("service", serviceName);

StorageUploadFileOptions options =
		StorageUploadFileOptions.builder()
				.accessLevel(StorageAccessLevel.PUBLIC)
				.contentType(mimeType)
				.metadata(metadata)
				.build();

final File file = new File(path);
if (file.canRead()) {
	Amplify.Storage.uploadFile(
			file.getName(),
			file,
			options,
			result -> {
				// write success logs
			},
			failure -> {
				// write failure logs
			}
	);
}

Log output

// Put your logs below this line
Non-fatal Exception: com.amplifyframework.storage.StorageException
Something went wrong with your AWS S3 Storage upload file operation
	com.amplifyframework.storage.s3.operation.AWSS3StorageUploadFileOperation$UploadTransferListener.onError (AWSS3StorageUploadFileOperation.kt:228)
	com.amplifyframework.storage.s3.transfer.TransferStatusUpdater.updateOnError$lambda$11$lambda$10 (TransferStatusUpdater.kt:160)
	android.os.Handler.handleCallback (Handler.java:984)
	android.os.Handler.dispatchMessage (Handler.java:104)
	android.os.Looper.loopOnce (Looper.java:238)
	android.os.Looper.loop (Looper.java:357)
	android.app.ActivityThread.main (ActivityThread.java:8101)
	java.lang.reflect.Method.invoke (Method.java)
	com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548)
	com.android.internal.os.ZygoteInit.main (ZygoteInit.java:957)

Caused by java.lang.Exception
java.lang.IllegalArgumentException: Unexpected char 0xd83d at 0 in x-amz-meta-text value: 😏
	com.amplifyframework.storage.s3.transfer.worker.BaseTransferWorker.doWork$suspendImpl (BaseTransferWorker.kt:121)
	com.amplifyframework.storage.s3.transfer.worker.BaseTransferWorker$doWork$1.invokeSuspend (BaseTransferWorker.kt:11)
	kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (ContinuationImpl.kt:33)
	kotlinx.coroutines.UndispatchedCoroutine.afterResume (CoroutineContext.kt:270)
	kotlinx.coroutines.AbstractCoroutine.resumeWith (AbstractCoroutine.kt:102)
	kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (ContinuationImpl.kt:46)
	kotlinx.coroutines.UndispatchedCoroutine.afterResume (CoroutineContext.kt:270)
	kotlinx.coroutines.AbstractCoroutine.resumeWith (AbstractCoroutine.kt:102)
	kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (ContinuationImpl.kt:46)
	kotlinx.coroutines.DispatchedTask.run (DispatchedTask.kt:106)
	kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely (CoroutineScheduler.kt:584)
	kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask (CoroutineScheduler.kt:793)
	kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker (CoroutineScheduler.kt:697)
	kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run (CoroutineScheduler.kt:684)

Caused by java.lang.IllegalArgumentException
Unexpected char 0xd83d at 0 in x-amz-meta-text value: 😏
	okhttp3.internal._HeadersCommonKt.headersCheckValue (-HeadersCommon.kt:140)
	okhttp3.internal._HeadersCommonKt.commonAdd (-HeadersCommon.kt:77)
	okhttp3.Headers$Builder.add (Headers.kt:166)
	okhttp3.internal._RequestCommonKt.commonAddHeader (-RequestCommon.kt:60)
	okhttp3.Request$Builder.addHeader (Request.kt:221)
	aws.smithy.kotlin.runtime.http.engine.okhttp.OkHttpUtilsKt$toOkHttpRequest$1.invoke (OkHttpUtils.kt:54)
	aws.smithy.kotlin.runtime.http.engine.okhttp.OkHttpUtilsKt$toOkHttpRequest$1.invoke (OkHttpUtils.kt:52)
	aws.smithy.kotlin.runtime.util.ValuesMap$DefaultImpls.forEach (ValuesMap.kt:54)
	aws.smithy.kotlin.runtime.util.ValuesMapImpl.forEach (ValuesMap.kt:69)
	aws.smithy.kotlin.runtime.http.engine.okhttp.OkHttpUtilsKt.toOkHttpRequest (OkHttpUtils.kt:52)
	aws.smithy.kotlin.runtime.http.engine.okhttp.OkHttpEngine.roundTrip (OkHttpEngine.kt:49)
	aws.smithy.kotlin.runtime.http.engine.internal.ManagedHttpClientEngine.roundTrip (ManagedHttpClientEngine.kt:1)
	aws.smithy.kotlin.runtime.http.SdkHttpClient$executeWithCallContext$2.invokeSuspend (SdkHttpClient.kt:44)
	kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (ContinuationImpl.kt:33)
	kotlinx.coroutines.DispatchedTask.run (DispatchedTask.kt:108)
	kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely (CoroutineScheduler.kt:584)
	kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask (CoroutineScheduler.kt:793)
	kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker (CoroutineScheduler.kt:697)
	kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run (CoroutineScheduler.kt:684)

amplifyconfiguration.json

No response

GraphQL Schema

// Put your schema below this line

Additional information and screenshots

No response

@yaroslav-v
Copy link
Author

yaroslav-v commented Dec 20, 2023

Another cause for statistics:
Caused by java.lang.IllegalArgumentException
Unexpected char 0x3042 at 0 in x-amz-meta-name value: あいする(ノ≧▽≦)ノ

@tjleing tjleing added bug Something isn't working storage Related to the Storage category/plugins labels Dec 21, 2023
@tylerjroach
Copy link
Member

@yaroslav-v Thank you for the detailed explanation. One of the members on our team will be taking a look.

@tylerjroach
Copy link
Member

@yaroslav-v This is coming from OkHttp. OkHttp does not allow non ASCII characters in headers by default. See (square/okhttp#2016) and the workaround while generating OkHttp requests (https://square.github.io/okhttp/5.x/okhttp/okhttp3/-headers/-builder/add-unsafe-non-ascii.html).

I'll have to look at our V1 dependencies to see why v1 would possibly be working here.

However, I do not believe the issue here is with the "key" being provided, but additional headers you may be passing that are not shown in the snippet?

Are you adding "x-amz-meta-text" and "x-amz-meta-name" headers yourself? I do not see us adding these. If possible, using only ASCII characters would allow the upload to suceed. I'll continue to look into how this was working in v1.

@tylerjroach
Copy link
Member

tylerjroach commented Dec 22, 2023

@yaroslav-v After further research on v1, a non-ascii header name will cause a failure but non-ascii header value works.

@yaroslav-v
Copy link
Author

@yaroslav-v This is coming from OkHttp. OkHttp does not allow non ASCII characters in headers by default. See (square/okhttp#2016) and the workaround while generating OkHttp requests (https://square.github.io/okhttp/5.x/okhttp/okhttp3/-headers/-builder/add-unsafe-non-ascii.html).

I'll have to look at our V1 dependencies to see why v1 would possibly be working here.

However, I do not believe the issue here is with the "key" being provided, but additional headers you may be passing that are not shown in the snippet?

Are you adding "x-amz-meta-text" and "x-amz-meta-name" headers yourself? I do not see us adding these. If possible, using only ASCII characters would allow the upload to suceed. I'll continue to look into how this was working in v1.

Hi! Thx for the answer.

Well, in some cases we add a couple of additional values from users to the metadata array (check the code snippet):
metadata.put("name", name);
metadata.put("text", text);

So, probably, this is the source of the data, but we don't add any headers directly.

@tylerjroach
Copy link
Member

@yaroslav-v We believe Amplify v2.14.10 fixes this issue, as the underlying AWS Kotlin SDK was updated to include the fix.

@tylerjroach tylerjroach added the closing soon This issue will be closed in 7 days unless further comments are made. label Feb 8, 2024
@yaroslav-v
Copy link
Author

Thx, will test

Copy link
Contributor

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working closing soon This issue will be closed in 7 days unless further comments are made. storage Related to the Storage category/plugins
Projects
None yet
Development

No branches or pull requests

3 participants