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

fix: escape strings.xml app name #1384

Merged

Conversation

tiagoappereira
Copy link
Contributor

Platforms affected

Android

Motivation and Context

Error on cordova platform add when the Cordova app name contains the character &. The issue occurs when parsing the strings.xml file. The issue was introduced on cordova-android 10.1.0

Fixes #1380

Description

Simply replaces the & characters in the project name (if present) by an _.

Testing

npm test and project creation successful

Checklist

  • I've run the tests to see all new and existing tests pass
  • I added automated test coverage as appropriate for this change
  • Commit is prefixed with (platform) if this change only applies to one platform (e.g. (android))
  • If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct keyword to close issues using keywords)
  • I've updated the documentation if necessary

@dpogue
Copy link
Member

dpogue commented Dec 7, 2021

Wouldn't the correct fix be to XML-escape the & (or other special characters like < and >)?

@tiagoappereira tiagoappereira changed the title fix: replace & characters in project name to avoid error on platform add fix: escape strings.xml app name Dec 9, 2021
@codecov-commenter
Copy link

codecov-commenter commented Dec 9, 2021

Codecov Report

Merging #1384 (64ed3b7) into master (a1ed1c0) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1384      +/-   ##
==========================================
+ Coverage   73.15%   73.17%   +0.01%     
==========================================
  Files          21       21              
  Lines        1643     1644       +1     
==========================================
+ Hits         1202     1203       +1     
  Misses        441      441              
Impacted Files Coverage Δ
lib/create.js 94.28% <100.00%> (+0.04%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a1ed1c0...64ed3b7. Read the comment docs.

Copy link
Contributor

@breautek breautek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

@breautek breautek requested review from erisu and dpogue December 9, 2021 16:58
Copy link
Member

@jcesarmobile jcesarmobile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package.json doesn't include lodash.
It works because there are 3 other dependencies that include lodash as dependency themselves but it could break in the future if those dependencies are replaced/removed or they stop using lodash in a newer version.
So I think it should be included in the package.json to prevent that.

Copy link
Member

@erisu erisu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not infavor for installing the entire lodash package just to use one method.

Either install the exact package, lodash.escape, or write our own escape method please.

Lodash
package size: 319.0 kB
unpacked size: 1.4 MB

Lodash Escape
package size: 3.9 kB
unpacked size: 9.1 kB

@jcesarmobile
Copy link
Member

It's already being installed because it's a dependency of other 3 dependencies.
As you can see, the package-lock.json doesn't change because it's already installed.

I would be in favor of not installing lodash, but that would mean updating a lot of our dependencies (including cordova-common as it depends on @netflix/nerror which depends on lodash)

@erisu
Copy link
Member

erisu commented Jan 5, 2022

We have been removing lodash from our dependencies because of the vuln. report frequencies.

I have discussed with others on dropping @netflix/nerror because they neglected to update their package over 2 years now and even with a PR open fixing their issues.

Netflix/nerror#18

@breautek
Copy link
Contributor

breautek commented Jan 5, 2022

We have been removing lodash from our dependencies because of the vuln. report frequencies.

I have discussed with others on dropping @netflix/nerror because they neglected to update their package over 2 years now and even with PRs open to resolve some issues.

Netflix/nerror#18

To add onto this, we can probably solve the same issue using the JS native method encodeURIComponent so that we can avoid introducing another lodash dependency in the code. I'm pretty sure the method exists in NodeJS environments.

@jcesarmobile
Copy link
Member

encodeURIComponent won't work because it scapes <Incredible&App> to %3CIncredible%26App%3E, which is not valid on the strings.xml.

I solved a similar problem in Capacitor using replace like this
https://github.com/ionic-team/capacitor/pull/5325/files#diff-ed3a1ffe6172324599c40aaeea6832beab33815ec5c28ba7f87e1a37fb55fae1R74-R78

note that it doesn't include > replacement because it wasn't needed in Capacitor, but might still be needed in Cordova as it looks like it does some previous conversion
also note that it includes ", which is not escaped by lodash

@erisu
Copy link
Member

erisu commented Jan 5, 2022

Here is what lodash escape does, just for additional details:

  '&': '&amp;',
  '<': '&lt;',
  '>': '&gt;',
  '"': '&quot;',
  "'": '&#39;',
  '`': '&#96;'

@jcesarmobile
Copy link
Member

jcesarmobile commented Jan 5, 2022

there is some previous conversion somewhere else, because I tested with ' and " and ' was converted to \' and " wasn't converted at all.
Note that for them to work in strings.xml they have to be \' and \", didn't try ``` (by work I mean that the app name is displayed on the phone exactly as on the strings.xml)

@jcesarmobile
Copy link
Member

I found where the ' is converted to \', is not done by lodash

strings.find('string[@name="app_name"]').text = name.replace(/'/g, '\\\'');

@erisu erisu merged commit f100809 into apache:master Feb 8, 2022
@tiagoappereira tiagoappereira deleted the fix/project_name_with_ampersand branch February 9, 2022 13:59
wedgberto pushed a commit to wedgberto/cordova-android that referenced this pull request May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error : Invalid character in entity name when &amp; is in the <name>
6 participants