Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 8b7c5d8

Browse files
Verify polling loop patterns and filters in ApduServiceInfo
Test: atest ApduServiceInfoTest Bug: 395728255 Flag: EXEMPT bugfix Change-Id: I3b56289b59684cd71ffaa0fcaf7b5773301381af
1 parent d57648f commit 8b7c5d8

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

nfc-non-updatable/java/android/nfc/cardemulation/ApduServiceInfo.java

+28-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
public final class ApduServiceInfo implements Parcelable {
7171
private static final String TAG = "ApduServiceInfo";
7272

73+
private static final Pattern PLPF_PATTERN =
74+
Pattern.compile("[0-9A-Fa-f]{2,}[0-9A-Fa-f,\\?,\\*\\.]*");
75+
private static final Pattern PLF_PATTERN =
76+
Pattern.compile("[0-9A-Fa-f]{2,}");
77+
7378
/**
7479
* Component level {@link android.content.pm.PackageManager.Property PackageManager
7580
* .Property} for a system application to change its icon and label
@@ -472,7 +477,12 @@ public ApduServiceInfo(@NonNull PackageManager pm, @NonNull ResolveInfo info, bo
472477
boolean autoTransact = a.getBoolean(
473478
com.android.internal.R.styleable.PollingLoopFilter_autoTransact,
474479
false);
475-
if (!mOnHost && !autoTransact) {
480+
boolean isValidFilter = PLF_PATTERN.matcher(plf).matches()
481+
&& plf.length() % 2 == 0;
482+
if (!isValidFilter) {
483+
Log.e(TAG, "Ignoring polling-loop-filter " + plf
484+
+ " it is not a valid filter");
485+
} else if (!mOnHost && !autoTransact) {
476486
Log.e(TAG, "Ignoring polling-loop-filter " + plf
477487
+ " for offhost service that isn't autoTransact");
478488
} else {
@@ -489,8 +499,12 @@ public ApduServiceInfo(@NonNull PackageManager pm, @NonNull ResolveInfo info, bo
489499
boolean autoTransact = a.getBoolean(
490500
com.android.internal.R.styleable.PollingLoopFilter_autoTransact,
491501
false);
492-
if (!mOnHost && !autoTransact) {
493-
Log.e(TAG, "Ignoring polling-loop-filter " + plf
502+
boolean isValidFilter = PLPF_PATTERN.matcher(plf).matches();
503+
if (!isValidFilter) {
504+
Log.e(TAG, "Ignoring polling-loop-pattern-filter " + plf
505+
+ " it is not a valid pattern filter");
506+
} else if (!mOnHost && !autoTransact) {
507+
Log.e(TAG, "Ignoring polling-loop-pattern-filter " + plf
494508
+ " for offhost service that isn't autoTransact");
495509
} else {
496510
mAutoTransactPatterns.put(Pattern.compile(plf), autoTransact);
@@ -814,6 +828,12 @@ public void setDynamicAidGroup(@NonNull AidGroup aidGroup) {
814828
@FlaggedApi(Flags.FLAG_NFC_READ_POLLING_LOOP)
815829
public void addPollingLoopFilter(@NonNull String pollingLoopFilter,
816830
boolean autoTransact) {
831+
if (!PLF_PATTERN.matcher(pollingLoopFilter).matches()
832+
|| pollingLoopFilter.length() % 2 != 0) {
833+
throw new IllegalArgumentException(
834+
"Polling loop filter must contain an even number of characters 0-9 or A-F"
835+
);
836+
}
817837
if (!mOnHost && !autoTransact) {
818838
return;
819839
}
@@ -842,6 +862,11 @@ public void removePollingLoopFilter(@NonNull String pollingLoopFilter) {
842862
@FlaggedApi(Flags.FLAG_NFC_READ_POLLING_LOOP)
843863
public void addPollingLoopPatternFilter(@NonNull String pollingLoopPatternFilter,
844864
boolean autoTransact) {
865+
if (!PLPF_PATTERN.matcher(pollingLoopPatternFilter).matches()) {
866+
throw new IllegalArgumentException(
867+
"Polling loop pattern filter is invalid"
868+
);
869+
}
845870
if (!mOnHost && !autoTransact) {
846871
return;
847872
}

0 commit comments

Comments
 (0)