Skip to content

Commit 5dc5083

Browse files
authored
fix(backend): ULIDを正しく処理できない問題を修正 (#17310)
fix(backend): fix parseUlidFull to correctly handle Crockford Base32 chars W/X/Y/Z
1 parent c9c6ef2 commit 5dc5083

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

packages/backend/src/misc/id/ulid.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55

66
// Crockford's Base32
77
// https://github.com/ulid/spec#encoding
8-
import { parseBigInt32 } from '@/misc/bigint.js';
98

109
const CHARS = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
1110

1211
export const ulidRegExp = /^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$/;
1312

13+
function parseBigIntCrockford(str: string): bigint {
14+
let result = 0n;
15+
for (let i = 0; i < str.length; i++) {
16+
result = result * 32n + BigInt(CHARS.indexOf(str[i]));
17+
}
18+
return result;
19+
}
20+
1421
function parseBase32(timestamp: string) {
1522
let time = 0;
1623
for (let i = 0; i < timestamp.length; i++) {
@@ -26,6 +33,6 @@ export function parseUlid(id: string): { date: Date; } {
2633
export function parseUlidFull(id: string): { date: number; additional: bigint; } {
2734
return {
2835
date: parseBase32(id.slice(0, 10)),
29-
additional: parseBigInt32(id.slice(10, 26)),
36+
additional: parseBigIntCrockford(id.slice(10, 26)),
3037
};
3138
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* SPDX-FileCopyrightText: syuilo and misskey-project
3+
* SPDX-License-Identifier: AGPL-3.0-only
4+
*/
5+
6+
import { describe, expect, test } from '@jest/globals';
7+
import { parseUlidFull } from '@/misc/id/ulid.js';
8+
9+
// Timestamp part "01KPS7S300" encodes 1776816000000ms (2026-04-22T00:00:00.000Z)
10+
// Verified: 1*32^8 + 19*32^7 + 22*32^6 + 25*32^5 + 7*32^4 + 25*32^3 + 3*32^2 = 1776816000000
11+
12+
describe('misc:ulid', () => {
13+
test('parseUlidFull - timestamp is parsed correctly', () => {
14+
// id[10..25] = all zeros (valid Crockford Base32)
15+
// 2026-04-22T00:00:00.000Z
16+
const { date } = parseUlidFull('01KPS7S3000000000000000000');
17+
expect(date).toBe(1776816000000);
18+
});
19+
20+
test('parseUlidFull - W/X/Y/Z at id[10] (chunk 1 head) do not throw', () => {
21+
// id[10] = W
22+
expect(() => parseUlidFull('01KPS7S300W000000000000000')).not.toThrow();
23+
// id[10] = X
24+
expect(() => parseUlidFull('01KPS7S300X000000000000000')).not.toThrow();
25+
// id[10] = Y
26+
expect(() => parseUlidFull('01KPS7S300Y000000000000000')).not.toThrow();
27+
// id[10] = Z
28+
expect(() => parseUlidFull('01KPS7S300Z000000000000000')).not.toThrow();
29+
});
30+
31+
test('parseUlidFull - W/X/Y/Z at id[16] (chunk 2 head) do not throw', () => {
32+
// id[16] = W
33+
expect(() => parseUlidFull('01KPS7S300ABCDEFW000000000')).not.toThrow();
34+
// id[16] = X
35+
expect(() => parseUlidFull('01KPS7S300ABCDEFX000000000')).not.toThrow();
36+
// id[16] = Y
37+
expect(() => parseUlidFull('01KPS7S300ABCDEFY000000000')).not.toThrow();
38+
// id[16] = Z
39+
expect(() => parseUlidFull('01KPS7S300ABCDEFZ000000000')).not.toThrow();
40+
});
41+
});

0 commit comments

Comments
 (0)