Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse and check the checksum as part of the URL when using domains #788

Merged
merged 2 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/agent/javascript/src/canisterId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,28 @@ function getCrc(hex: string): string {
export class CanisterId {
public static fromText(text: string): CanisterId {
if (text.startsWith('ic:')) {
text = text.toUpperCase();
const hex = text.slice(3);
if (hex.length >= 2 && hex.length % 2 === 0 && /^[0-9A-F]+$/.test(hex)) {
const id = hex.slice(0, -2);
const checksum = hex.slice(-2);
if (checksum !== getCrc(id)) {
throw new Error('Illegal CanisterId: ' + text);
}
return this.fromHex(id);
} else {
throw new Error('Cannot parse CanisterId: ' + text);
}
return this.fromHexWithChecksum(text.slice(3));
} else {
throw new Error('CanisterId is not a "ic:" url: ' + text);
}
}

public static fromHexWithChecksum(hexWithChecksum: string): CanisterId {
const hex = hexWithChecksum.toUpperCase();
if (hex.length >= 2 && hex.length % 2 === 0 && /^[0-9A-F]+$/.test(hex)) {
const id = hex.slice(0, -2);
const checksum = hex.slice(-2);
if (checksum !== getCrc(id)) {
throw new Error(`Invalid checksum for CanisterId: "ic:${hexWithChecksum}"`);
}
return new this(id);
} else {
throw new Error('Cannot parse CanisterId: ' + hexWithChecksum);
}
}

public static fromHex(hex: string): CanisterId {
return new this(hex);
return new this(hex.toUpperCase());
}

public static fromBlob(blob: BinaryBlob): CanisterId {
Expand Down
12 changes: 8 additions & 4 deletions src/bootstrap/src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,21 @@ export class SiteInfo {
const subdomain = components.slice(0, -3).join('.');

if (maybeIc0 === 'ic0' && maybeApp === 'app') {
return new SiteInfo(DomainKind.Ic0, CanisterId.fromHex(maybeCId), subdomain);
return new SiteInfo(DomainKind.Ic0, CanisterId.fromHexWithChecksum(maybeCId), subdomain);
} else if (maybeIc0 === 'lvh' && maybeApp === 'me') {
return new SiteInfo(DomainKind.Lvh, CanisterId.fromHex(maybeCId), subdomain);
return new SiteInfo(DomainKind.Lvh, CanisterId.fromHexWithChecksum(maybeCId), subdomain);
} else if (maybeIc0 === 'localhost' && maybeApp === undefined) {
/// Allow subdomain of localhost.
return new SiteInfo(DomainKind.Localhost, CanisterId.fromHex(maybeCId), subdomain);
return new SiteInfo(
DomainKind.Localhost,
CanisterId.fromHexWithChecksum(maybeCId),
subdomain,
);
} else if (maybeApp === 'localhost') {
/// Allow subdomain of localhost, but maybeIc0 is the canister ID.
return new SiteInfo(
DomainKind.Localhost,
CanisterId.fromHex(maybeIc0),
CanisterId.fromHexWithChecksum(maybeIc0),
`${maybeCId}.${subdomain}`,
);
} else {
Expand Down