Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cookie to check share link access #691

Merged
merged 7 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions fileserver/fileop.go
Original file line number Diff line number Diff line change
Expand Up @@ -3499,7 +3499,7 @@ type ShareLinkInfo struct {
ShareType string `json:"share_type"`
}

func queryShareLinkInfo(token, opType string) (*ShareLinkInfo, *appError) {
func queryShareLinkInfo(token, cookie, opType string) (*ShareLinkInfo, *appError) {
claims := SeahubClaims{
time.Now().Add(time.Second * 300).Unix(),
true,
Expand All @@ -3512,10 +3512,13 @@ func queryShareLinkInfo(token, opType string) (*ShareLinkInfo, *appError) {
err := fmt.Errorf("failed to sign jwt token: %v", err)
return nil, &appError{err, "", http.StatusInternalServerError}
}
url := fmt.Sprintf("%s?token=%s&type=%s", seahubURL+"/share-link-info/", token, opType)
url := fmt.Sprintf("%s?token=%s&type=%s", seahubURL+"/check-share-link-access/", token, opType)
header := map[string][]string{
"Authorization": {"Token " + tokenString},
}
if cookie != "" {
header["Cookie"] = []string{cookie}
}
Copy link
Member

Choose a reason for hiding this comment

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

这个请求没有设置 applicateion/json 的头部。可以在 HttpCommon 里面自动设置。HttpCommon 也需要增加请求超时。

status, body, err := utils.HttpCommon("GET", url, header, nil)
if err != nil {
err := fmt.Errorf("failed to get share link info: %v", err)
Expand Down Expand Up @@ -3548,7 +3551,8 @@ func accessLinkCB(rsp http.ResponseWriter, r *http.Request) *appError {
return &appError{nil, msg, http.StatusBadRequest}
}
token := parts[1]
info, appErr := queryShareLinkInfo(token, "file")
cookie := r.Header.Get("Cookie")
info, appErr := queryShareLinkInfo(token, cookie, "file")
if appErr != nil {
return appErr
}
Expand Down
3 changes: 2 additions & 1 deletion server/access-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,8 @@ access_link_cb(evhtp_request_t *req, void *arg)

token = parts[1];

info = http_tx_manager_query_share_link_info (token, "file");
const char *cookie = evhtp_kv_find (req->headers_in, "Cookie");
info = http_tx_manager_query_share_link_info (token, cookie, "file");
if (!info) {
error_str = "Link token not found\n";
error_code = EVHTP_RES_FORBIDDEN;
Expand Down
10 changes: 8 additions & 2 deletions server/http-tx-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,11 @@ parse_share_link_info (const char *rsp_content, int rsp_size)
}

SeafileShareLinkInfo *
http_tx_manager_query_share_link_info (const char *token, const char *type)
http_tx_manager_query_share_link_info (const char *token, const char *cookie, const char *type)
{
Connection *conn = NULL;
char *token_header;
char *cookie_header;
struct curl_slist *headers = NULL;
int ret = 0;
CURL *curl;
Expand All @@ -609,12 +610,17 @@ http_tx_manager_query_share_link_info (const char *token, const char *type)
curl = conn->curl;
headers = curl_slist_append (headers, "User-Agent: Seafile/"SEAFILE_CLIENT_VERSION" ("USER_AGENT_OS")");
Copy link
Member

Choose a reason for hiding this comment

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

检查一下其他代码,这里的 User-Agent 应该改为 Seafile server 吧。

token_header = g_strdup_printf ("Authorization: Token %s", jwt_token);
if (cookie) {
cookie_header = g_strdup_printf ("Cookie: %s", cookie);
headers = curl_slist_append (headers, cookie_header);
g_free (cookie_header);
}
Copy link
Member

Choose a reason for hiding this comment

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

这个代码的位置往下面移动一下吧,感觉中断了 token_header 的代码。

headers = curl_slist_append (headers, token_header);
headers = curl_slist_append (headers, "Content-Type: application/json");
g_free (token_header);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

url = g_strdup_printf("%s/share-link-info/?token=%s&type=%s", seaf->seahub_url, token, type);
url = g_strdup_printf("%s/check-share-link-access/?token=%s&type=%s", seaf->seahub_url, token, type);
ret = http_get_common (curl, url, jwt_token, &rsp_status,
&rsp_content, &rsp_size, NULL, NULL, TRUE);
if (ret < 0) {
Expand Down
2 changes: 1 addition & 1 deletion server/http-tx-mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ char *
http_tx_manager_get_nickname (const char *modifier);

SeafileShareLinkInfo *
http_tx_manager_query_share_link_info (const char *token, const char *type);
http_tx_manager_query_share_link_info (const char *token, const char *cookie, const char *type);
#endif
Loading