Skip to content

idd-all SLUG derivation: BSD/GNU sed alternation (\| not portable) #6

Description

@kiki830621

Problem

From verification of #1 (P3 finding 16 — security reviewer):
SLUG derivation 用:

SLUG=$(echo "$TITLE" | tr '[:upper:]' '[:lower:]' \
    | sed 's/[^a-z0-9]/-/g' | sed 's/--*/-/g' | sed 's/^-\|-$//g' \
    | cut -c1-40)

最後 sed 's/^-\|-$//g' 用 alternation \| — BSD sed (macOS default) 不支援基本 regex 的 \|,需要 -E flag。GNU sed 才把 \| 當 alternation。

結果: macOS user 可能看到 SLUG 邊緣有殘留 - (-foo-bar 變不掉首 dash)。

此 issue 在 #1 之前就存在 (Step 0.5 PR-mode block 從 v2.40.0 帶過來),只是 verify 順手抓到。

Type

fix / portability

Why P3 (pre-existing)

Recommendation

兩種改法擇一:

A. 切兩段 sed,用 POSIX-portable:

SLUG=$(echo "$TITLE" | tr '[:upper:]' '[:lower:]' \
    | sed 's/[^a-z0-9]/-/g' \
    | sed 's/--*/-/g' \
    | sed -e 's/^-//' -e 's/-$//' \
    | cut -c1-40)

B. 改用 -E + alternation:

SLUG=$(echo "$TITLE" | tr '[:upper:]' '[:lower:]' \
    | sed -E 's/[^a-z0-9]/-/g; s/-+/-/g; s/^-|-$//g' \
    | cut -c1-40)

-E 在 BSD/GNU 都支援。

Source

Related: #1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions