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

Ensure URL normalization is safe for all input. Fixes #2695 - #2804

Merged
tmcw merged 1 commit into
masterfrom
2695-safe-normalization
Oct 28, 2015
Merged

tmcw merged 1 commit into
masterfrom
2695-safe-normalization

Conversation

@tmcw

@tmcw tmcw commented Oct 26, 2015

Copy link
Copy Markdown
Contributor

Check that a URL has enough segments before normalizing it.

cc @jfirebaugh for the review

Comment thread src/mbgl/util/mapbox.cpp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This one should be pathname.size() < 4, since pathname[3] is used unconditionally.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In this particular case, isDraft assumes a pathname longer than 3. That’s dead code now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is in the wrong section: I've adjusted the code to < 4 for the glyph endpoint

@tmcw
tmcw force-pushed the 2695-safe-normalization branch 2 times, most recently from 039a54f to e29967c Compare October 26, 2015 18:06
@tmcw

tmcw commented Oct 26, 2015

Copy link
Copy Markdown
Contributor Author

Addressed @jfirebaugh's issue, and added log messages as suggested by @1ec5: is it possible to test our log output?

@1ec5

1ec5 commented Oct 26, 2015

Copy link
Copy Markdown
Contributor

At a glance, I think assertions would be better than log messages in this case, because client code may assume “normalized” to mean “no more mapbox: URLs”. But this is definitely an improvement over the current behavior.

@tmcw

tmcw commented Oct 26, 2015

Copy link
Copy Markdown
Contributor Author

@jfirebaugh should I merge this as-is or switch to assertions?

@ljbade

ljbade commented Oct 27, 2015

Copy link
Copy Markdown
Contributor

One problem with asserts is that on Android it causes a native crash which confuses most Android devs as they are unlikely to have seen these before and will struggle to connect the crash to a malformed URL. I would much prefer logging, or even better a way to through a Java exception with a suitable error message.

/cc @bleege

@1ec5

1ec5 commented Oct 27, 2015

Copy link
Copy Markdown
Contributor

I think it’s generally best to assert when the issue is likely to be developer error; logging and otherwise failing gracefully is better for issues out of the developer’s direct control, like network failures. The JNI code should have exception handling to catch assertions and present them as Java exceptions.

@tmcw

tmcw commented Oct 27, 2015

Copy link
Copy Markdown
Contributor Author

Sounds like we should do logging first, and then the JNI work, and then switch to exceptions

@ljbade

ljbade commented Oct 27, 2015

Copy link
Copy Markdown
Contributor

@1ec5 Unfortunately JNI was desinged before C++ exceptions were invented and there is no reliable way to pass them to Java. Even the exception's message is not printed on Android, all you get in the log is a SIGABRT error.

I have tried various libs that claim to handle C++ exceptions in JNI, but they were either outdated (did not support newer Android versions), unreliable (missed most exceptions), or just complicated debugging normal crashes with false stack traces.

Quite simply C++ exceptions are not compatible with Android.

@bleege

bleege commented Oct 27, 2015

Copy link
Copy Markdown
Contributor

The JNI code should have exception handling to catch assertions and present them as Java exceptions.

Agreed. However, "what JNI should do" vs "what JNI can actually do" are often 2 different things. 😄 Remember JNI is an imperfect (and usually one way) bridge between C++ code and Java code.

@1ec5

1ec5 commented Oct 27, 2015

Copy link
Copy Markdown
Contributor

When you interface with C++ code, you have to be open to exceptions being thrown, and not just for the most catastrophic of reasons. So it sounds like we need to revisit this issue, probably as another layer beneath JNI that catches C++-style exceptions and turns them into C-style error codes. We'd do the same if we wanted to expose these error conditions as native exceptions on the iOS side too. But saying the C++ layer can't use C++ exceptions because of JNI is like the tail wagging the dog. 😊

I don't want to hold up this PR for a seemingly large task, so this is good to go.

@tmcw
tmcw force-pushed the 2695-safe-normalization branch from e29967c to 2830e4f Compare October 27, 2015 15:39
@jfirebaugh

Copy link
Copy Markdown
Contributor

LGTM.

What @1ec5 says about exceptions is spot on -- when writing bindings to C++, it's common to need to blanket catch all exceptions and translate them into some other error mechanism that's compatible with the platform.

@ljbade

ljbade commented Oct 27, 2015

Copy link
Copy Markdown
Contributor

I can see our jni.cpp growing in size :-( we really need to refactor that file into smaller ones.

@tmcw
tmcw force-pushed the 2695-safe-normalization branch from 2830e4f to c80c823 Compare October 28, 2015 14:44
@tmcw
tmcw merged commit c80c823 into master Oct 28, 2015
@tmcw
tmcw deleted the 2695-safe-normalization branch October 28, 2015 14:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants