{"record":{"id":"d87de393f85f78bd","repo":"iflytek/astron-agent","slug":"67011-permission-bot-belong-wrong-user","errorCode":"67011","errorMessage":"permission.bot.belong.wrong.user","messagePattern":"permission\\.bot\\.belong\\.wrong\\.user","errorType":"error_code","errorClass":"BusinessException","httpStatus":null,"severity":"warning","filePath":"console/backend/hub/src/main/java/com/iflytek/astron/console/hub/util/BotPermissionUtil.java","lineNumber":43,"sourceCode":"\n    public void checkBot(Integer botId) {\n        Long spaceId = SpaceInfoUtil.getSpaceId();\n        String uid = RequestContextUtil.getUID();\n\n        LambdaQueryWrapper<ChatBotBase> query = new LambdaQueryWrapper<>();\n        query.eq(ChatBotBase::getId, botId);\n\n\n        if (spaceId == null) {\n            // spaceId is null, belongs to individual\n            query.eq(ChatBotBase::getUid, uid).isNull(ChatBotBase::getSpaceId);\n        } else {\n            // spaceId is not null, belongs to space\n            query.eq(ChatBotBase::getSpaceId, spaceId);\n        }\n\n        if (!chatBotBaseMapper.exists(query)) {\n            throw new BusinessException(spaceId == null ? ResponseEnum.PERMISSION_BOT_NOT_BELONG_USER : ResponseEnum.PERMISSION_BOT_NOT_BELONG_SPACE);\n        }\n    }\n\n}\n","sourceCodeStart":25,"sourceCodeEnd":48,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/backend/hub/src/main/java/com/iflytek/astron/console/hub/util/BotPermissionUtil.java#L25-L48","documentation":"BotPermissionUtil.checkBot(botId) verifies that the bot exists and belongs to the current user (when no space context) or to the current space (when spaceId is present). If chatBotBaseMapper.exists(query) finds no matching row, it throws BusinessException with PERMISSION_BOT_NOT_BELONG_USER (message 'permission.bot.belong.wrong.user', code 67011) for the personal case, or PERMISSION_BOT_NOT_BELONG_SPACE for the space case. This is an authorization guard, not a data error.","triggerScenarios":"Calling any service that invokes checkBot with a botId the requesting user does not own (uid mismatch or bot has a spaceId while querying personal scope), a botId belonging to another space, or a nonexistent/deleted botId (exists returns false, which is indistinguishable from no-permission here).","commonSituations":"Frontend passing a stale botId after the bot was deleted or moved to another space, a user opening a bot shared by someone else without space context headers (SpaceInfoUtil.getSpaceId() null), copy-pasted bot IDs across environments, or missing space header causing personal-scope lookup on a space bot.","solutions":["Verify the request carries the correct space context headers so SpaceInfoUtil.getSpaceId() resolves to the bot's actual space","Confirm the botId exists in chat_bot_base and its uid/space_id match the requesting user/space","Refresh the frontend bot list to eliminate stale botIds after deletion or space migration","Handle BusinessException code 67011 in the caller with a friendly 'no permission for this bot' message instead of treating it as a system failure"],"exampleFix":"// before\nbotPermissionUtil.checkBot(request.getBotId()); // throws 67011 for foreign bots\n// after\ntry {\n    botPermissionUtil.checkBot(request.getBotId());\n} catch (BusinessException e) {\n    if (ResponseEnum.PERMISSION_BOT_NOT_BELONG_USER.equals(e.getResponseEnum())) {\n        throw new BusinessException(ResponseEnum.PERMISSION_BOT_NOT_BELONG_USER);\n    }\n    throw e;\n}","handlingStrategy":"try-catch","validationCode":"// caller-side: only pass botIds the current user listed\nboolean owned = botListIds.contains(botId); if (!owned) { throw new BusinessException(ResponseEnum.PERMISSION_BOT_NOT_BELONG_USER); }","typeGuard":null,"tryCatchPattern":"try { botPermissionUtil.checkBot(botId); } catch (BusinessException e) { if (e.getCode() == 67011) { throw new NotFoundException(\"Bot not accessible\"); } throw e; }","preventionTips":["Always propagate the space context headers so space scoping resolves correctly","Refresh bot lists in the UI after delete/space-migration to avoid stale botIds","Distinguish 'not found' from 'no permission' if UX requires it","Never trust botIds from client input without the ownership check"],"tags":["authorization","permission","mybatis-plus","bot"],"backgroundTag":"permission-denied","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}