From dc6a92dc1593b85a0d9ec785e85532744843c9ec Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Tue, 12 Apr 2022 21:13:21 +0000 Subject: [PATCH] Fix upload non-authenticated crash FossilOrigin-Name: 126a9da0db65d4f7803fef11d40ebc3d681c6afc339b0103eba3246754810dea --- dist/treebird20.css | 1 - src/attachments.c | 50 +++++++++++++++++++++++++++++++++--------- src/attachments.h | 6 ++++- src/local_config_set.c | 7 +++++- src/main.c | 1 + src/status.c | 13 +++++++++-- src/status.h | 3 +++ src/timeline.c | 2 +- 8 files changed, 67 insertions(+), 16 deletions(-) diff --git a/dist/treebird20.css b/dist/treebird20.css index d615816..d109666 100644 --- a/dist/treebird20.css +++ b/dist/treebird20.css @@ -58,7 +58,6 @@ ul border-top: 0 !important; margin-left: auto; margin-right: auto; - border: 1px solid #bbbbbb !important; box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3); border-width: 0; border-radius: 5px; diff --git a/src/attachments.c b/src/attachments.c index 00cbe2b..2c0bd10 100644 --- a/src/attachments.c +++ b/src/attachments.c @@ -32,22 +32,23 @@ struct attachments_args mstdnt_bool sensitive; }; -int try_upload_media(struct mstdnt_storage* storage, +int try_upload_media(struct mstdnt_storage** storage, struct session* ssn, mastodont_t* api, struct mstdnt_attachment** attachments, char*** media_ids) { - if (!ssn->post.files.array_size || - !(ssn->post.files.content && ssn->post.files.content[0].content_size)) + size_t size = ssn->post.files.array_size; + if (!FILES_READY(ssn)) return 1; if (media_ids) - *media_ids = malloc(sizeof(char*) * ssn->post.files.array_size); + *media_ids = malloc(sizeof(char*) * size); - *attachments = malloc(sizeof(struct mstdnt_attachment) * ssn->post.files.array_size); + *attachments = malloc(sizeof(struct mstdnt_attachment) * size); + *storage = calloc(1, sizeof(struct mstdnt_storage) * size); - for (int i = 0; i < ssn->post.files.array_size; ++i) + for (int i = 0; i < size; ++i) { struct file_content* content = ssn->post.files.content + i; struct mstdnt_upload_media_args args = { @@ -61,10 +62,30 @@ int try_upload_media(struct mstdnt_storage* storage, .description = "Treebird image" }; - mastodont_upload_media(api, - &args, - storage, - *attachments + i); + if (mastodont_upload_media(api, + &args, + *storage + i, + *attachments + i)) + { + // EPICFAIL + for (size_t j = 0; j < i; ++j) + { + if (media_ids) free((*media_ids)[j]); + mastodont_storage_cleanup(*storage + j); + } + + if (media_ids) + { + free(*media_ids); + *media_ids = NULL; + } + + free(*attachments); + *attachments = NULL; + free(*storage); + *storage = NULL; + return 1; + } if (media_ids) { @@ -76,8 +97,17 @@ int try_upload_media(struct mstdnt_storage* storage, return 0; } +void cleanup_media_storages(struct session* ssn, struct mstdnt_storage* storage) +{ + if (!FILES_READY(ssn)) return; + for (size_t i = 0; i < ssn->post.files.array_size; ++i) + mastodont_storage_cleanup(storage + i); + free(storage); +} + void cleanup_media_ids(struct session* ssn, char** media_ids) { + if (!FILES_READY(ssn)) return; if (!media_ids) return; for (size_t i = 0; i < ssn->post.files.array_size; ++i) free(media_ids[i]); diff --git a/src/attachments.h b/src/attachments.h index 171a66e..91c3738 100644 --- a/src/attachments.h +++ b/src/attachments.h @@ -21,11 +21,15 @@ #include #include "session.h" -int try_upload_media(struct mstdnt_storage* storage, +#define FILES_READY(ssn) (ssn->post.files.array_size && \ + ssn->post.files.content && ssn->post.files.content[0].content_size) + +int try_upload_media(struct mstdnt_storage** storage, struct session* ssn, mastodont_t* api, struct mstdnt_attachment** attachments, char*** media_ids); +void cleanup_media_storages(struct session* ssn, struct mstdnt_storage* storage); void cleanup_media_ids(struct session* ssn, char** media_ids); char* construct_attachment(mstdnt_bool sensitive, struct mstdnt_attachment* att, int* str_size); char* construct_attachments(mstdnt_bool sensitive, struct mstdnt_attachment* atts, size_t atts_len, size_t* str_size); diff --git a/src/local_config_set.c b/src/local_config_set.c index 87a46dc..d2980f9 100644 --- a/src/local_config_set.c +++ b/src/local_config_set.c @@ -63,9 +63,14 @@ void load_config(struct session* ssn, mastodont_t* api) if (ssn->post.theme) { struct mstdnt_attachment* attachments = NULL; - struct mstdnt_storage storage = { 0 }; + struct mstdnt_storage* storage = NULL; if (try_upload_media(&storage, ssn, api, &attachments, NULL) == 0) + { set_config_str(&(ssn->config.background_url), "background_url", attachments[0].url); + } + + if (storage) + cleanup_media_storages(ssn, storage); } set_config_str(&(ssn->config.theme), "theme", ssn->post.theme); set_config_int(&(ssn->config.themeclr), "themeclr", ssn->post.themeclr); diff --git a/src/main.c b/src/main.c index 6b560ac..b704d44 100644 --- a/src/main.c +++ b/src/main.c @@ -91,6 +91,7 @@ int main(void) { "/status/:/interact", status_interact }, { "/status/:/reply", status_reply }, { "/status/:", status_view }, + { "/notice/:", notice_redirect }, { "/lists/for/:", content_tl_list }, { "/lists", content_lists }, { "/federated", content_tl_federated }, diff --git a/src/status.c b/src/status.c index de698d1..eafe324 100644 --- a/src/status.c +++ b/src/status.c @@ -50,7 +50,7 @@ int try_post_status(struct session* ssn, mastodont_t* api) { if (!(ssn->post.content)) return 1; - struct mstdnt_storage storage, att_storage = { 0 }; + struct mstdnt_storage storage, *att_storage = NULL; char** files; size_t files_len; @@ -82,7 +82,8 @@ int try_post_status(struct session* ssn, mastodont_t* api) // TODO cleanup when errors are properly implemented mastodont_storage_cleanup(&storage); - mastodont_storage_cleanup(&att_storage); + if (att_storage) + cleanup_media_storages(ssn, att_storage); cleanup_media_ids(ssn, media_ids); if (attachments) free(attachments); @@ -384,3 +385,11 @@ void content_status(struct session* ssn, mastodont_t* api, char** data, int is_r mastodont_storage_cleanup(&storage); mastodont_storage_cleanup(&status_storage); } + +void notice_redirect(struct session* ssn, mastodont_t* api, char** data) +{ + char* url; + easprintf(&url, "%s/status/%s", config_url_prefix, data[0]); + redirect(REDIRECT_303, url); + free(url); +} diff --git a/src/status.h b/src/status.h index d0a5692..0e13bad 100644 --- a/src/status.h +++ b/src/status.h @@ -50,4 +50,7 @@ void content_status(struct session* ssn, mastodont_t* api, char** data, int is_r // Cleanup void cleanup_media_ids(struct session* ssn, char** media_ids); +// Redirects +void notice_redirect(struct session* ssn, mastodont_t* api, char** data); + #endif // STATUS_H diff --git a/src/timeline.c b/src/timeline.c index 0342e6c..2900bee 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -103,7 +103,7 @@ void tl_public(struct session* ssn, mastodont_t* api, int local) void tl_list(struct session* ssn, mastodont_t* api, char* list_id) { size_t status_count, statuses_html_count; - struct mstdnt_status* statuses; + struct mstdnt_status* statuses = NULL; struct mstdnt_storage storage = { 0 }; char* status_format, *post_box; char* output = NULL;