mirror of
https://github.com/twinksoftware/Gearslip.git
synced 2026-11-12 16:52:24 +01:00
A site's icon is only ever saved inside the icons folder, and the translations check runs with no token
Fixes the code scanning alerts: a host made of dots got through the file name filter, and the workflow didn't say it needs no permissions.
This commit is contained in:
1 parent
80a5d68d4c
commit
8e1df8dc34
2 files changed
+17
-4
No files matched your search
@@ -9,6 +9,9 @@ on:
|
|||||||
paths:
|
paths:
|
||||||
- "gearslip/src/main/res/values-*/strings.xml"
|
- "gearslip/src/main/res/values-*/strings.xml"
|
||||||
|
|
||||||
|
# It only reads the pull request's own details from the event, so it needs no token at all.
|
||||||
|
permissions: {}
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
weblate-only:
|
weblate-only:
|
||||||
if: github.event.pull_request.user.login != 'weblate'
|
if: github.event.pull_request.user.login != 'weblate'
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ object SiteIcons {
|
|||||||
fun icon(context: Context, host: String): Bitmap? {
|
fun icon(context: Context, host: String): Bitmap? {
|
||||||
if (host.isEmpty()) return null
|
if (host.isEmpty()) return null
|
||||||
memory[host]?.let { return it }
|
memory[host]?.let { return it }
|
||||||
val file = file(context, host)
|
val file = file(context, host) ?: return null
|
||||||
if (!file.isFile) return null
|
if (!file.isFile) return null
|
||||||
return runCatching { BitmapFactory.decodeFile(file.path) }.getOrNull()?.also { memory[host] = it }
|
return runCatching { BitmapFactory.decodeFile(file.path) }.getOrNull()?.also { memory[host] = it }
|
||||||
}
|
}
|
||||||
@@ -64,7 +64,7 @@ object SiteIcons {
|
|||||||
private fun store(context: Context, host: String, bitmap: Bitmap) {
|
private fun store(context: Context, host: String, bitmap: Bitmap) {
|
||||||
memory[host] = bitmap
|
memory[host] = bitmap
|
||||||
runCatching {
|
runCatching {
|
||||||
val file = file(context, host)
|
val file = file(context, host) ?: return@runCatching
|
||||||
file.parentFile?.mkdirs()
|
file.parentFile?.mkdirs()
|
||||||
file.outputStream().use { bitmap.compress(Bitmap.CompressFormat.PNG, 100, it) }
|
file.outputStream().use { bitmap.compress(Bitmap.CompressFormat.PNG, 100, it) }
|
||||||
}.onFailure { GearslipLog.w("browser: couldn't save the icon for $host: ${it.message}") }
|
}.onFailure { GearslipLog.w("browser: couldn't save the icon for $host: ${it.message}") }
|
||||||
@@ -86,6 +86,16 @@ object SiteIcons {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun file(context: Context, host: String) =
|
/**
|
||||||
File(File(context.filesDir, DIRECTORY), host.replace(Regex("[^A-Za-z0-9.-]"), "_") + ".png")
|
* Where [host]'s icon is kept, or null for a host that can't name a file in the icons folder.
|
||||||
|
* The host comes from a web page, so the name is cut down to letters, digits, dots and dashes,
|
||||||
|
* a host made of dots only is refused, and the result has to land inside the folder.
|
||||||
|
*/
|
||||||
|
private fun file(context: Context, host: String): File? {
|
||||||
|
val name = host.replace(Regex("[^A-Za-z0-9.-]"), "_")
|
||||||
|
if (name.isEmpty() || name.all { it == '.' }) return null
|
||||||
|
val dir = File(context.filesDir, DIRECTORY).canonicalFile
|
||||||
|
val file = File(dir, "$name.png").canonicalFile
|
||||||
|
return file.takeIf { it.toPath().normalize().startsWith(dir.toPath()) && it.parentFile == dir }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user