Fix upload non-authenticated crash

FossilOrigin-Name: 126a9da0db65d4f7803fef11d40ebc3d681c6afc339b0103eba3246754810dea
This commit is contained in:
me@ow.nekobit.net 2022-04-12 21:13:21 +00:00
parent 3db8081bfc
commit dc6a92dc15
8 changed files with 67 additions and 16 deletions

1
dist/treebird20.css vendored
View file

@ -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;

View file

@ -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]);

View file

@ -21,11 +21,15 @@
#include <mastodont.h>
#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);

View file

@ -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);

View file

@ -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 },

View file

@ -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);
}

View file

@ -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

View file

@ -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;