Files
windmill/frontend/src/lib/utils/folderRuleGlob.ts
T
hugocasaandClaude Opus 5.5 fa253bc50b fix: match folder run-as rules as the server does, and preselect them on deploy
Clients matched folder default_permissioned_as globs with minimatch, where `*`
stops at `/`; the server uses globset defaults, where it does not. Now that
clients send the rule they preview, a disagreement stored the wrong identity,
so the frontend and CLI match with the server's semantics.

Compare & Deploy and Deploy workspaces preselect the target folder's rule for
an item new there. A group rule is sent by principal alone, since the address
it falls back to contradicts the principal. A new script or flow keeps its
seeded identity once its first deploy ends creation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 10:01:57 +02:00

62 lines
1.9 KiB
TypeScript

/**
* Whether a folder `default_permissioned_as` glob matches a path relative to its folder, with
* the semantics the server applies (`globset::Glob` defaults): `*` and `?` also match `/`,
* `**` spans any depth, `{a,b}` alternates, `[!x]` negates, and an invalid glob matches nothing.
* The editors send the rule they preview, so a matcher that disagrees with the server stores
* an identity other than the one the folder rule would have given.
*/
export function folderRuleGlobMatches(glob: string, relativePath: string): boolean {
const source = globToRegexSource(glob)
return source !== undefined && new RegExp(`^${source}$`, 's').test(relativePath)
}
function globToRegexSource(glob: string): string | undefined {
let re = ''
for (let i = 0; i < glob.length; i++) {
const c = glob[i]
if (c === '*') {
if (glob[i + 1] !== '*') {
re += '.*'
continue
}
const atSegmentStart = i === 0 || glob[i - 1] === '/'
i++
if (atSegmentStart && glob[i + 1] === '/' && i + 2 < glob.length) {
re += '(?:.*/)?'
i++
} else {
re += '.*'
if (glob[i + 1] === '/') i++
}
} else if (c === '?') {
re += '.'
} else if (c === '[') {
const end = glob.indexOf(']', i + 2)
if (end < 0) return undefined
let body = glob.slice(i + 1, end).replace(/[\\^]/g, '\\$&')
if (body.startsWith('!')) body = '^' + body.slice(1)
re += `[${body}]`
i = end
} else if (c === '{') {
const end = glob.indexOf('}', i)
if (end < 0) return undefined
const alternatives = glob
.slice(i + 1, end)
.split(',')
.map(globToRegexSource)
if (alternatives.some((a) => a === undefined)) return undefined
re += `(?:${alternatives.join('|')})`
i = end
} else if (c === '\\' && i + 1 < glob.length) {
re += escapeRegex(glob[++i])
} else {
re += escapeRegex(c)
}
}
return re
}
function escapeRegex(c: string): string {
return c.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&')
}