🐛 [Bug]: kit GitHub loader 编码处理不规范,导致请求截断与路径匹配失败

Abierto
#430 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
74/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
github, typescript
Área
api, backend, testing

Línea de trabajo

Start in packages/kit/src/skills/loader/github.ts, especially the contents URL construction, normalizeRepoPath, and toSkillRelativePath; read packages/kit/src/skills/loader/definition.ts to understand the entry-file lookup. Run the existing GitHub loader tests, then cover encoded paths and #/? paths, including recursive entries. Done means requests preserve encoded path segments, path handling is explicit, and out-of-root paths produce a locating error without breaking the existing weather case.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug
Tiny Robot Version

0.5.1(@opentiny/tiny-robot-kit),develop 分支 ab3999df3

Vue Version

不涉及,kit 包与 Vue 无关;两个问题都在 loader 的路径处理上

Link to minimal reproduction

问题一:contents 请求 URL 未编码(目录名含 # / ?)

const sourcePath = 'skills/a#b' // 仓库中真实存在的目录名
const url = new URL(`https://api.github.com/repos/o/r/contents/${sourcePath}`)
url.searchParams.set('ref', 'main')

console.log(url.pathname) // /repos/o/r/contents/skills/a  ← #b 被当成 fragment 丢弃
console.log(url.hash)     // #b

问题二:路径比较未按编码规范化(目录名含空格 / 中文)

import { loadSkillWithDetails } from '@opentiny/tiny-robot-kit'

// 路径来自 URL 片段,仍是百分号编码形态
await loadSkillWithDetails({ source: 'github', repo: 'owner/repo', ref: 'main', path: 'my%20skill' })
// 抛出:Skill entry file "SKILL.md" is missing.

// 同一个目录用解码后的路径调用则完全正常
await loadSkillWithDetails({ source: 'github', repo: 'owner/repo', ref: 'main', path: 'my skill' }) // OK
Steps to reproduce

问题一:

  1. 在 GitHub 仓库准备目录名包含 # 或 ? 的 Skill,例如 skills/a#b/SKILL.md。
  2. 调用 loadSkillWithDetails({ source: 'github', repo, ref, path: 'skills/a#b' })。
  3. 观察实际发出的 contents 请求路径。

问题二:

  1. 在 GitHub 仓库准备 my skill/SKILL.md(中文目录同样适用)。
  2. 用编码形态的路径 my%20skill 调用 loadSkillWithDetails。
  3. 观察抛出的错误信息。
What is expected
  • contents 请求路径完整保留目录名,#、? 等字符按 URL 规范编码为 %23、%3F;递归遍历子目录时同样保持编码。
  • loader 对“仓库路径”这一入参给出明确契约,并对编码形态做规范化(或直接拒绝)。
  • 路径不在 skill root 下时抛出可定位的错误,而不是退化成无关的入口文件错误。
What is actually happening
  • 问题一:字符串模板拼接导致 # 之后被解析为 fragment、? 之后被解析为 query,请求实际指向父目录;若父目录存在会静默遍历错误的目录(可能导入到另一个 Skill),不存在则返回 404,错误信息与真实原因无关。
  • 问题二:抛出的是 Skill entry file "SKILL.md" is missing.(组件侧再映射为「Skill 包必须包含 SKILL.md 文件」),但目录里明明有 SKILL.md,排查方向被完全带偏。
What is your project name

TinyRobot(组件库 Skill 导入流程,PR #429 评审过程中发现)

Any additional comments (optional)

问题一:contents URL 拼接未编码

根因:packages/kit/src/skills/loader/github.ts:29 用字符串模板拼接 contents/${sourcePath}。sourcePath 既可能来自调用方传入的 options.path,也可能来自第 44 行递归时的 entry.path(GitHub API 返回的是未编码真实路径)。空格会被 URL 自动编码(my skill → my%20skill),但 #、? 不会,会被当作 fragment / query 分隔符。

影响:目录名或文件名含这些字符的 Skill 无法导入;更严重的是可能静默读取到错误目录内容,调用方与用户都无法察觉。

建议修复:构造 contents URL 时逐段编码。

const contentsUrl = (repo: string, path: string) => {
  const url = new URL(`https://api.github.com/repos/${repo}/contents/`)
  url.pathname += path.split('/').map(encodeURIComponent).join('/')
  return url
}

entry.download_url 来自 API,是可直接使用的完整 URL,无需额外处理。

问题二:路径比较未按编码规范化并静默降级

根因链路:

  1. normalizeRepoPath(github.ts:180)只做反斜杠替换与首尾斜杠清理,不处理百分号编码,skillRoot 保持 my%20skill。
  2. 请求 URL 中的 %20 会被 GitHub 正常解码,目录列表能取到,但返回的每项 path 是解码后的 my skill/SKILL.md。
  3. toSkillRelativePath(github.ts:187)用 ${skillRoot}/ 做字面前缀比较,不匹配后走到 github.ts:195 的兜底分支,把整个仓库路径当成相对路径返回。
  4. createSkillDefinition(packages/kit/src/skills/loader/definition.ts:25)按 path === 'SKILL.md' 查找入口文件,找不到,抛出 “entry file ... is missing”。

影响:任何把 URL 片段直接当作 path 传入的调用方都会踩到。组件库已在 PR #429(提交 068ed65e)先对路径做 decodeURIComponent 规避,但 loadSkillWithDetails 是 kit 的公开 API,CLI / 宿主应用仍可能传入编码路径。

建议修复:比较前对 skillRoot 与 entry.path 做统一规范化(例如按段 decodeURIComponent);不在 skill root 下时不再静默降级,直接抛出明确错误(如 Skill file "x" is outside skill root "y");在 GithubSkillLoadOptions.path 的文档注释中明确必须是未编码的仓库内路径。

修复清单

  • 问题一:构造 contents URL 时逐段 encodeURIComponent,递归路径同样处理
  • 问题二:路径比较前做编码规范化,且不在 skill root 下时抛出明确错误
  • 回归测试:entry.path 含 #/? 时请求 pathname 包含 %23/%3F
  • 回归测试:my%20skill / 中文编码路径的行为(规范化成功,或抛出明确的路径错误)
  • 保留现有 contents/skills/weather(未编码、无空格)用例,避免回归

关联

Lenguaje dominante
TypeScript
Estrellas
109
Forks
19
Merge medio
1 d 21 h
PR fusionados (30 d)
25

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de opentiny/tiny-robot

Todos los issues de opentiny/tiny-robot

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.