From 01ecc12f13283b4bef9202dc2723ac67aff360bf Mon Sep 17 00:00:00 2001 From: andrethomas Date: Thu, 17 Jan 2019 01:22:51 +0200 Subject: [PATCH 1/2] PN532: Prevent non AlphaNumberic on DATA --- sonoff/xsns_40_pn532_i2c.ino | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sonoff/xsns_40_pn532_i2c.ino b/sonoff/xsns_40_pn532_i2c.ino index c2fdf65e7..dd5fb4aba 100644 --- a/sonoff/xsns_40_pn532_i2c.ino +++ b/sonoff/xsns_40_pn532_i2c.ino @@ -406,7 +406,13 @@ void PN532_ScanForTag(void) uint8_t keyuniversal[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; if (mifareclassic_AuthenticateBlock (uid, uid_len, 1, 1, keyuniversal)) { if (mifareclassic_ReadDataBlock(1, card_data)) { - memcpy(&card_datas,&card_data,sizeof(card_data)); // Cast block 1 to a string + for (uint8_t i = 0;i < sizeof(card_data);i++) { + if ((isalpha(card_data[i])) || ((isdigit(card_data[i])))) { + card_datas[i] = char(card_data[i]); + } else { + card_datas[i] = '\0'; + } + } } if (pn532_i2c_function == 1) { // erase block 1 of card for (uint8_t i = 0;i<16;i++) { From 66c39ce88c60b628ff9d9109ea1d19b38d77ceca Mon Sep 17 00:00:00 2001 From: andrethomas Date: Thu, 17 Jan 2019 02:10:22 +0200 Subject: [PATCH 2/2] PN532: Prevent non AlphaNumberic on DATA --- sonoff/xsns_40_pn532_i2c.ino | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/sonoff/xsns_40_pn532_i2c.ino b/sonoff/xsns_40_pn532_i2c.ino index dd5fb4aba..a03b30e6b 100644 --- a/sonoff/xsns_40_pn532_i2c.ino +++ b/sonoff/xsns_40_pn532_i2c.ino @@ -67,6 +67,7 @@ uint8_t pn532_i2c_disable = 0; #ifdef USE_PN532_DATA_FUNCTION uint8_t pn532_i2c_function = 0; uint8_t pn532_i2c_newdata[16]; +uint8_t pn532_i2c_newdata_len = 0; #endif // USE_PN532_DATA_FUNCTION const uint8_t PROGMEM pn532_global_timeout = 10; @@ -426,12 +427,23 @@ void PN532_ScanForTag(void) } } if (pn532_i2c_function == 2) { - memcpy(&card_data,&pn532_i2c_newdata,sizeof(card_data)); - if (mifareclassic_WriteDataBlock(1, card_data)) { - set_success = true; - snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Data write successful"); + boolean IsAlphaNumeric = true; + for (uint8_t i = 0;i < pn532_i2c_newdata_len;i++) { + if ((!isalpha(pn532_i2c_newdata[i])) || (!isdigit(pn532_i2c_newdata[i]))) { + IsAlphaNumeric = false; + } + } + if (IsAlphaNumeric) { + if (mifareclassic_WriteDataBlock(1, card_data)) { + memcpy(&card_data,&pn532_i2c_newdata,sizeof(card_data)); + set_success = true; + snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Data write successful"); + AddLog(LOG_LEVEL_INFO); + memcpy(&card_datas,&card_data,sizeof(card_data)); // Cast block 1 to a string + } + } else { + snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Data must be alphanumeric"); AddLog(LOG_LEVEL_INFO); - memcpy(&card_datas,&card_data,sizeof(card_data)); // Cast block 1 to a string } } } else { @@ -518,10 +530,10 @@ boolean PN532_Command(void) return serviced; } sprintf(sub_string_tmp,subStr(sub_string, XdrvMailbox.data, ",", 2)); - uint8_t dlen = strlen(sub_string_tmp); - if (dlen > 15) { dlen = 15; } - memcpy(&pn532_i2c_newdata,&sub_string_tmp,dlen); - pn532_i2c_newdata[dlen] = 0x00; // Null terminate the string + pn532_i2c_newdata_len = strlen(sub_string_tmp); + if (pn532_i2c_newdata_len > 15) { pn532_i2c_newdata_len = 15; } + memcpy(&pn532_i2c_newdata,&sub_string_tmp,pn532_i2c_newdata_len); + pn532_i2c_newdata[pn532_i2c_newdata_len] = 0x00; // Null terminate the string pn532_i2c_function = 2; snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Next scanned tag data block 1 will be set to '%s'",pn532_i2c_newdata); AddLog(LOG_LEVEL_INFO);