Skip to content

Commit

Permalink
Merge pull request #103 from onionshare/random-content-url
Browse files Browse the repository at this point in the history
Use a random path to prevent other apps from accessing content
  • Loading branch information
grote authored Sep 11, 2023
2 parents 3a3657b + f023a0f commit 144eb6b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/src/main/assets/templates/send.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h1>{% if title %}{{ title }}{% else %}OnionShare{% endif %}</h1>
<div>Total size: <strong>{{ filesize_human }}</strong> {% if is_zipped %} (compressed){%
endif %}
</div>
<a class="button" href='/download'>Download Files</a>
<a class="button" href='{{ content_path }}/download'>Download Files</a>
</div>
</header>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/onionshare/android/ShareManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class ShareManager @Inject constructor(
// We only create the hidden service after files have been zipped and webserver was started,
// so we are in sharing state once the first HS descriptor has been published.
notificationManager.onSharing()
ShareUiState.Sharing("http://${torState.onion}.onion")
ShareUiState.Sharing("http://${torState.onion}.onion/${webserverManager.contentPath}")
}

TorState.FailedToConnect -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ class WebserverManager @Inject constructor() {
private var server: ApplicationEngine? = null
private val _state = MutableStateFlow<WebServerState>(WebServerState.Stopped(false))
val state = _state.asStateFlow()
val contentPath = getRandomPath()

suspend fun start(sendPage: SendPage): Int {
_state.value = WebServerState.Starting
val staticPath = getStaticPath()
val staticPathMap = mapOf("static_url_path" to staticPath)
val pathMap = mapOf("static_url_path" to staticPath, "content_path" to contentPath)
TrafficStats.setThreadStatsTag(0x42)
val server = embeddedServer(
factory = Netty,
Expand All @@ -72,11 +73,11 @@ class WebserverManager @Inject constructor() {
install(Pebble) {
loader(ClasspathLoader().apply { prefix = "assets/templates" })
}
installStatusPages(staticPathMap)
installStatusPages(pathMap)
addListener()
routing {
defaultRoutes(staticPath)
sendRoutes(sendPage, staticPathMap)
sendRoutes(sendPage, pathMap)
}
}.also { it.start() }
this.server = server
Expand All @@ -98,11 +99,13 @@ class WebserverManager @Inject constructor() {
}
}

private fun getRandomPath(): String {
val randomBytes = ByteArray(16).apply { secureRandom.nextBytes(this) }
return Base64.encodeToString(randomBytes, NO_PADDING or URL_SAFE).trimEnd()
}

private fun getStaticPath(): String {
val staticSuffixBytes = ByteArray(16).apply { secureRandom.nextBytes(this) }
val staticSuffix =
Base64.encodeToString(staticSuffixBytes, NO_PADDING or URL_SAFE).trimEnd()
return "/static_$staticSuffix"
return "/static_${getRandomPath()}"
}

private fun Application.addListener() {
Expand Down Expand Up @@ -142,11 +145,11 @@ class WebserverManager @Inject constructor() {
}

private fun Route.sendRoutes(sendPage: SendPage, staticPathMap: Map<String, String>) {
get("/") {
get("/$contentPath") {
val model = sendPage.model + staticPathMap
call.respond(PebbleContent("send.html", model))
}
get("/download") {
get("/$contentPath/download") {
call.response.header(
ContentDisposition,
Attachment.withParameter(FileName, sendPage.fileName).toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ fun ShareBottomSheetSharingPreview() {
Surface(color = MaterialTheme.colors.background) {
BottomSheet(
state = ShareUiState.Sharing(
"http://openpravyvc6spbd4flzn4g2iqu4sxzsizbtb5aqec25t76dnoo5w7yd.onion/",
"http://openpravyvc6spbd4flzn4g2iqu4sxzsizbtb5aqec25t76dnoo5w7yd.onion/eW91IGFyZSBhIG5lcmQ7KQ",
),
onSheetButtonClicked = {},
)
Expand Down

0 comments on commit 144eb6b

Please sign in to comment.